GenerateExamPdfJob.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. ]);
  36. $taskManager->updateTaskProgress($this->taskId, 10, '开始生成试卷PDF...');
  37. // 生成试卷PDF
  38. $pdfUrl = $pdfExportService->generateExamPdf($this->paperId)
  39. ?? $questionBankService->exportExamToPdf($this->paperId)
  40. ?? route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $this->paperId, 'answer' => 'false']);
  41. $taskManager->updateTaskProgress($this->taskId, 50, '试卷PDF生成完成,开始生成判卷PDF...');
  42. // 生成判卷PDF
  43. $gradingPdfUrl = $pdfExportService->generateGradingPdf($this->paperId)
  44. ?? route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $this->paperId, 'answer' => 'true']);
  45. // 构建完整的试卷内容
  46. $paperModel = Paper::with('questions')->find($this->paperId);
  47. $examContent = $paperModel
  48. ? $paperPayloadService->buildExamContent($paperModel)
  49. : [];
  50. // 标记任务完成
  51. $taskManager->markTaskCompleted($this->taskId, [
  52. 'exam_content' => $examContent,
  53. 'pdfs' => [
  54. 'exam_paper_pdf' => $pdfUrl,
  55. 'grading_pdf' => $gradingPdfUrl,
  56. ],
  57. ]);
  58. Log::info('PDF生成队列任务完成', [
  59. 'task_id' => $this->taskId,
  60. 'paper_id' => $this->paperId,
  61. 'pdf_url' => $pdfUrl,
  62. 'grading_pdf_url' => $gradingPdfUrl,
  63. ]);
  64. // 发送回调通知
  65. $taskManager->sendCallback($this->taskId);
  66. } catch (\Exception $e) {
  67. Log::error('PDF生成队列任务失败', [
  68. 'task_id' => $this->taskId,
  69. 'paper_id' => $this->paperId,
  70. 'error' => $e->getMessage(),
  71. 'trace' => $e->getTraceAsString(),
  72. ]);
  73. $taskManager->markTaskFailed($this->taskId, $e->getMessage());
  74. }
  75. }
  76. }