GenerateAnalysisPdfJob.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. public string $paperId;
  14. public string $studentId;
  15. public ?string $recordId;
  16. public int $maxAttempts = 3;
  17. public function __construct(string $paperId, string $studentId, ?string $recordId = null)
  18. {
  19. $this->paperId = $paperId;
  20. $this->studentId = $studentId;
  21. $this->recordId = $recordId;
  22. // 指定使用 pdf 队列,由独立的 pdf-worker 容器处理
  23. $this->onQueue('pdf');
  24. }
  25. public function handle(ExamPdfExportService $pdfExportService): void
  26. {
  27. try {
  28. Log::info('开始处理学情分析PDF生成队列任务', [
  29. 'paper_id' => $this->paperId,
  30. 'student_id' => $this->studentId,
  31. 'record_id' => $this->recordId,
  32. 'attempt' => $this->attempts(),
  33. ]);
  34. // 生成学情分析PDF
  35. $pdfUrl = $pdfExportService->generateAnalysisReportPdf(
  36. $this->paperId,
  37. $this->studentId,
  38. $this->recordId
  39. );
  40. if ($pdfUrl) {
  41. Log::info('学情分析PDF生成成功', [
  42. 'paper_id' => $this->paperId,
  43. 'student_id' => $this->studentId,
  44. 'record_id' => $this->recordId,
  45. 'pdf_url' => $pdfUrl,
  46. ]);
  47. } else {
  48. Log::error('学情分析PDF生成失败', [
  49. 'paper_id' => $this->paperId,
  50. 'student_id' => $this->studentId,
  51. 'record_id' => $this->recordId,
  52. ]);
  53. // 如果失败且还有重试次数,则重试
  54. if ($this->attempts() < $this->maxAttempts) {
  55. Log::info('将在5秒后重试PDF生成', [
  56. 'paper_id' => $this->paperId,
  57. 'student_id' => $this->studentId,
  58. 'attempt' => $this->attempts(),
  59. ]);
  60. $this->release(5);
  61. return;
  62. }
  63. }
  64. } catch (\Exception $e) {
  65. Log::error('学情分析PDF生成队列任务失败', [
  66. 'paper_id' => $this->paperId,
  67. 'student_id' => $this->studentId,
  68. 'record_id' => $this->recordId,
  69. 'error' => $e->getMessage(),
  70. 'trace' => $e->getTraceAsString(),
  71. ]);
  72. // 如果是第一次失败且可能是临时错误,等待后重试
  73. if ($this->attempts() < $this->maxAttempts) {
  74. Log::info('检测到临时错误,将在10秒后重试', [
  75. 'paper_id' => $this->paperId,
  76. 'student_id' => $this->studentId,
  77. 'attempt' => $this->attempts(),
  78. ]);
  79. $this->release(10);
  80. return;
  81. }
  82. }
  83. }
  84. }