GenerateExamPdfJob.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Paper;
  4. use App\Services\ExamPdfExportService;
  5. use App\Services\QuestionBankService;
  6. use App\Services\PaperPayloadService;
  7. use App\Services\TaskManager;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Queue\SerializesModels;
  13. use Illuminate\Support\Facades\Log;
  14. class GenerateExamPdfJob implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. public string $taskId;
  18. public string $paperId;
  19. public int $maxAttempts = 3;
  20. public function __construct(string $taskId, string $paperId)
  21. {
  22. $this->taskId = $taskId;
  23. $this->paperId = $paperId;
  24. }
  25. public function handle(
  26. ExamPdfExportService $pdfExportService,
  27. QuestionBankService $questionBankService,
  28. PaperPayloadService $paperPayloadService,
  29. TaskManager $taskManager
  30. ): void {
  31. try {
  32. Log::info('开始处理PDF生成队列任务', [
  33. 'task_id' => $this->taskId,
  34. 'paper_id' => $this->paperId,
  35. 'attempt' => $this->attempts(),
  36. ]);
  37. // 【修复】首先检查试卷是否存在
  38. $paperModel = Paper::with('questions')->find($this->paperId);
  39. if (!$paperModel) {
  40. Log::error('PDF生成队列任务失败:试卷不存在', [
  41. 'task_id' => $this->taskId,
  42. 'paper_id' => $this->paperId,
  43. 'attempt' => $this->attempts(),
  44. ]);
  45. // 如果试卷不存在,判断是否需要重试
  46. if ($this->attempts() < $this->maxAttempts) {
  47. Log::info('试卷不存在,将在2秒后重试', [
  48. 'task_id' => $this->taskId,
  49. 'paper_id' => $this->paperId,
  50. 'attempt' => $this->attempts(),
  51. 'next_attempt' => $this->attempts() + 1,
  52. ]);
  53. // 延迟2秒后重试(缩短间隔,减少对回调的影响)
  54. $this->release(2);
  55. return;
  56. } else {
  57. Log::error('试卷不存在且已达到最大重试次数,标记任务失败', [
  58. 'task_id' => $this->taskId,
  59. 'paper_id' => $this->paperId,
  60. 'attempts' => $this->attempts(),
  61. ]);
  62. $taskManager->markTaskFailed($this->taskId, "试卷不存在: {$this->paperId}");
  63. return;
  64. }
  65. }
  66. // 检查试卷是否有题目
  67. if ($paperModel->questions->isEmpty()) {
  68. Log::error('PDF生成队列任务失败:试卷没有题目数据', [
  69. 'task_id' => $this->taskId,
  70. 'paper_id' => $this->paperId,
  71. 'question_count' => 0,
  72. ]);
  73. if ($this->attempts() < $this->maxAttempts) {
  74. Log::info('试卷没有题目,将在1秒后重试', [
  75. 'task_id' => $this->taskId,
  76. 'paper_id' => $this->paperId,
  77. 'attempt' => $this->attempts(),
  78. ]);
  79. // 延迟1秒后重试(更短间隔)
  80. $this->release(1);
  81. return;
  82. } else {
  83. $taskManager->markTaskFailed($this->taskId, "试卷没有题目数据: {$this->paperId}");
  84. return;
  85. }
  86. }
  87. $taskManager->updateTaskProgress($this->taskId, 10, '开始生成试卷PDF...');
  88. // 生成试卷PDF
  89. $pdfUrl = $pdfExportService->generateExamPdf($this->paperId)
  90. ?? $questionBankService->exportExamToPdf($this->paperId)
  91. ?? route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $this->paperId, 'answer' => 'false']);
  92. $taskManager->updateTaskProgress($this->taskId, 50, '试卷PDF生成完成,开始生成判卷PDF...');
  93. // 生成判卷PDF
  94. $gradingPdfUrl = $pdfExportService->generateGradingPdf($this->paperId)
  95. ?? route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $this->paperId, 'answer' => 'true']);
  96. // 构建完整的试卷内容
  97. $examContent = $paperPayloadService->buildExamContent($paperModel);
  98. // 标记任务完成
  99. $taskManager->markTaskCompleted($this->taskId, [
  100. 'exam_content' => $examContent,
  101. 'pdfs' => [
  102. 'exam_paper_pdf' => $pdfUrl,
  103. 'grading_pdf' => $gradingPdfUrl,
  104. ],
  105. ]);
  106. Log::info('PDF生成队列任务完成', [
  107. 'task_id' => $this->taskId,
  108. 'paper_id' => $this->paperId,
  109. 'pdf_url' => $pdfUrl,
  110. 'grading_pdf_url' => $gradingPdfUrl,
  111. 'question_count' => $paperModel->questions->count(),
  112. ]);
  113. // 发送回调通知
  114. $taskManager->sendCallback($this->taskId);
  115. } catch (\Exception $e) {
  116. Log::error('PDF生成队列任务失败', [
  117. 'task_id' => $this->taskId,
  118. 'paper_id' => $this->paperId,
  119. 'error' => $e->getMessage(),
  120. 'trace' => $e->getTraceAsString(),
  121. ]);
  122. // 如果是第一次失败且试卷可能还在创建中,等待后重试
  123. if ($this->attempts() < $this->maxAttempts && strpos($e->getMessage(), '不存在') !== false) {
  124. Log::info('检测到试卷不存在错误,将在2秒后重试', [
  125. 'task_id' => $this->taskId,
  126. 'paper_id' => $this->paperId,
  127. 'attempt' => $this->attempts(),
  128. ]);
  129. $this->release(2);
  130. return;
  131. }
  132. $taskManager->markTaskFailed($this->taskId, $e->getMessage());
  133. }
  134. }
  135. }