GenerateExamPdfJob.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Paper;
  4. use App\Services\ExamPdfExportService;
  5. use App\Services\PaperPayloadService;
  6. use App\Services\QuestionBankService;
  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. use Throwable;
  15. class GenerateExamPdfJob implements ShouldQueue
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. public string $taskId;
  19. public string $paperId;
  20. public int $tries = 3;
  21. public int $timeout = 300;
  22. public function __construct(string $taskId, string $paperId)
  23. {
  24. $this->taskId = $taskId;
  25. $this->paperId = $paperId;
  26. // 指定使用 pdf 队列,由独立的 pdf-worker 容器处理
  27. $this->onQueue('pdf');
  28. // 避免事务未提交时 worker 提前消费导致“试卷不存在”
  29. $this->afterCommit();
  30. }
  31. public function handle(
  32. ExamPdfExportService $pdfExportService,
  33. QuestionBankService $questionBankService,
  34. PaperPayloadService $paperPayloadService,
  35. TaskManager $taskManager
  36. ): void {
  37. try {
  38. Log::info('开始处理PDF生成队列任务', [
  39. 'task_id' => $this->taskId,
  40. 'paper_id' => $this->paperId,
  41. 'attempt' => $this->attempts(),
  42. ]);
  43. // 【修复】首先检查试卷是否存在
  44. // 强制走主库读取,避免读写分离下新建试卷短时不可见导致“试卷不存在”
  45. $paperModel = Paper::query()
  46. ->useWritePdo()
  47. ->with('questions')
  48. ->find($this->paperId);
  49. if (! $paperModel) {
  50. Log::error('PDF生成队列任务失败:试卷不存在', [
  51. 'task_id' => $this->taskId,
  52. 'paper_id' => $this->paperId,
  53. 'attempt' => $this->attempts(),
  54. ]);
  55. // 如果试卷不存在,判断是否需要重试
  56. if ($this->attempts() < $this->tries) {
  57. Log::info('试卷不存在,将在2秒后重试', [
  58. 'task_id' => $this->taskId,
  59. 'paper_id' => $this->paperId,
  60. 'attempt' => $this->attempts(),
  61. 'next_attempt' => $this->attempts() + 1,
  62. ]);
  63. // 延迟2秒后重试(缩短间隔,减少对回调的影响)
  64. $this->release(2);
  65. return;
  66. } else {
  67. Log::error('试卷不存在且已达到最大重试次数,标记任务失败', [
  68. 'task_id' => $this->taskId,
  69. 'paper_id' => $this->paperId,
  70. 'attempts' => $this->attempts(),
  71. ]);
  72. $taskManager->markTaskFailed($this->taskId, "试卷不存在: {$this->paperId}");
  73. return;
  74. }
  75. }
  76. // 检查试卷是否有题目
  77. if ($paperModel->questions->isEmpty()) {
  78. Log::error('PDF生成队列任务失败:试卷没有题目数据', [
  79. 'task_id' => $this->taskId,
  80. 'paper_id' => $this->paperId,
  81. 'question_count' => 0,
  82. ]);
  83. if ($this->attempts() < $this->tries) {
  84. Log::info('试卷没有题目,将在1秒后重试', [
  85. 'task_id' => $this->taskId,
  86. 'paper_id' => $this->paperId,
  87. 'attempt' => $this->attempts(),
  88. ]);
  89. // 延迟1秒后重试(更短间隔)
  90. $this->release(1);
  91. return;
  92. } else {
  93. $taskManager->markTaskFailed($this->taskId, "试卷没有题目数据: {$this->paperId}");
  94. return;
  95. }
  96. }
  97. $taskManager->updateTaskProgress($this->taskId, 10, '开始生成统一PDF(直接合并两个页面,效率最高)...');
  98. // 根据 config 或 env 配置决定是否包含知识点讲解
  99. // 还需要判断如果摸底(paper_type =0)的时候也是不需要插入知识点讲解内容
  100. $includeKpExplain = null;
  101. if($paperModel->paper_type === 0) {
  102. $includeKpExplain = false;
  103. }
  104. info("includekpexplain", [$includeKpExplain, $paperModel->paper_type]);
  105. $unifiedPdfUrl = $pdfExportService->generateUnifiedPdf($this->paperId, $includeKpExplain);
  106. $taskManager->updateTaskProgress($this->taskId, 90, 'PDF生成完成,准备返回结果...');
  107. $examContent = $paperPayloadService->buildExamContent($paperModel);
  108. // 标记任务完成(完整PDF存储到all_pdf_url字段)
  109. $taskManager->markTaskCompleted($this->taskId, [
  110. 'exam_content' => $examContent,
  111. 'pdfs' => [
  112. 'all_pdf' => $unifiedPdfUrl, // 【完整PDF】包含试卷和判卷,存储到all_pdf_url字段
  113. ],
  114. ]);
  115. Log::info('PDF生成队列任务完成(终极优化:直接合并HTML生成一份完整PDF)', [
  116. 'task_id' => $this->taskId,
  117. 'paper_id' => $this->paperId,
  118. 'all_pdf_url' => $unifiedPdfUrl,
  119. 'question_count' => $paperModel->questions->count(),
  120. 'method' => 'generateUnifiedPdf (direct merge, fastest)',
  121. ]);
  122. // 发送回调通知(在合并PDF完成后)
  123. $taskManager->sendCallback($this->taskId);
  124. } catch (\Exception $e) {
  125. Log::error('PDF生成队列任务失败', [
  126. 'task_id' => $this->taskId,
  127. 'paper_id' => $this->paperId,
  128. 'error' => $e->getMessage(),
  129. 'trace' => $e->getTraceAsString(),
  130. ]);
  131. // 如果是第一次失败且试卷可能还在创建中,等待后重试
  132. if ($this->attempts() < $this->tries && strpos($e->getMessage(), '不存在') !== false) {
  133. Log::info('检测到试卷不存在错误,将在2秒后重试', [
  134. 'task_id' => $this->taskId,
  135. 'paper_id' => $this->paperId,
  136. 'attempt' => $this->attempts(),
  137. ]);
  138. $this->release(2);
  139. return;
  140. }
  141. $taskManager->markTaskFailed($this->taskId, $e->getMessage());
  142. }
  143. }
  144. public function failed(Throwable $exception): void
  145. {
  146. try {
  147. app(TaskManager::class)->markTaskFailed($this->taskId, $exception->getMessage());
  148. } catch (Throwable $innerException) {
  149. Log::error('PDF生成队列任务失败回调异常', [
  150. 'task_id' => $this->taskId,
  151. 'paper_id' => $this->paperId,
  152. 'error' => $innerException->getMessage(),
  153. ]);
  154. }
  155. Log::error('PDF生成队列任务最终失败', [
  156. 'task_id' => $this->taskId,
  157. 'paper_id' => $this->paperId,
  158. 'error' => $exception->getMessage(),
  159. ]);
  160. }
  161. }