GenerateAnalysisPdfJob.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Jobs;
  3. use App\Services\ExamPdfExportService;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Illuminate\Support\Facades\Log;
  10. class GenerateAnalysisPdfJob implements ShouldQueue
  11. {
  12. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  13. /**
  14. * 指定队列名称 - PDF 生成需要 Chrome,必须在 pdf 队列处理
  15. */
  16. public string $queue = 'pdf';
  17. public string $paperId;
  18. public string $studentId;
  19. public ?string $recordId;
  20. public int $maxAttempts = 3;
  21. public function __construct(string $paperId, string $studentId, ?string $recordId = null)
  22. {
  23. $this->paperId = $paperId;
  24. $this->studentId = $studentId;
  25. $this->recordId = $recordId;
  26. }
  27. public function handle(ExamPdfExportService $pdfExportService): void
  28. {
  29. try {
  30. Log::info('开始处理学情分析PDF生成队列任务', [
  31. 'paper_id' => $this->paperId,
  32. 'student_id' => $this->studentId,
  33. 'record_id' => $this->recordId,
  34. 'attempt' => $this->attempts(),
  35. ]);
  36. // 生成学情分析PDF
  37. $pdfUrl = $pdfExportService->generateAnalysisReportPdf(
  38. $this->paperId,
  39. $this->studentId,
  40. $this->recordId
  41. );
  42. if ($pdfUrl) {
  43. Log::info('学情分析PDF生成成功', [
  44. 'paper_id' => $this->paperId,
  45. 'student_id' => $this->studentId,
  46. 'record_id' => $this->recordId,
  47. 'pdf_url' => $pdfUrl,
  48. ]);
  49. } else {
  50. Log::error('学情分析PDF生成失败', [
  51. 'paper_id' => $this->paperId,
  52. 'student_id' => $this->studentId,
  53. 'record_id' => $this->recordId,
  54. ]);
  55. // 如果失败且还有重试次数,则重试
  56. if ($this->attempts() < $this->maxAttempts) {
  57. Log::info('将在5秒后重试PDF生成', [
  58. 'paper_id' => $this->paperId,
  59. 'student_id' => $this->studentId,
  60. 'attempt' => $this->attempts(),
  61. ]);
  62. $this->release(5);
  63. return;
  64. }
  65. }
  66. } catch (\Exception $e) {
  67. Log::error('学情分析PDF生成队列任务失败', [
  68. 'paper_id' => $this->paperId,
  69. 'student_id' => $this->studentId,
  70. 'record_id' => $this->recordId,
  71. 'error' => $e->getMessage(),
  72. 'trace' => $e->getTraceAsString(),
  73. ]);
  74. // 如果是第一次失败且可能是临时错误,等待后重试
  75. if ($this->attempts() < $this->maxAttempts) {
  76. Log::info('检测到临时错误,将在10秒后重试', [
  77. 'paper_id' => $this->paperId,
  78. 'student_id' => $this->studentId,
  79. 'attempt' => $this->attempts(),
  80. ]);
  81. $this->release(10);
  82. return;
  83. }
  84. }
  85. }
  86. }