GenerateAnalysisPdfJob.php 3.2 KB

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