RegeneratePdfJob.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Paper;
  4. use App\Services\ExamPdfExportService;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Support\Facades\Log;
  11. /**
  12. * 单个试卷 PDF 重新生成任务(用于批量重生成 API 的队列)
  13. */
  14. class RegeneratePdfJob implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. public string $paperId;
  18. public int $tries = 2;
  19. public int $timeout = 300;
  20. public function __construct(string $paperId)
  21. {
  22. $this->paperId = $paperId;
  23. $this->onQueue('pdf');
  24. }
  25. public function handle(ExamPdfExportService $pdfExportService): void
  26. {
  27. $paper = Paper::with('questions')->find($this->paperId);
  28. if (! $paper || $paper->questions->isEmpty()) {
  29. Log::warning('RegeneratePdfJob: 跳过无题目试卷', ['paper_id' => $this->paperId]);
  30. return;
  31. }
  32. try {
  33. Log::info('RegeneratePdfJob: 开始', ['paper_id' => $this->paperId]);
  34. $pdfUrl = $pdfExportService->generateUnifiedPdf($this->paperId);
  35. if ($pdfUrl) {
  36. Log::info('RegeneratePdfJob: 成功', ['paper_id' => $this->paperId, 'pdf_url' => $pdfUrl]);
  37. } else {
  38. Log::warning('RegeneratePdfJob: 返回空', ['paper_id' => $this->paperId]);
  39. }
  40. } catch (\Exception $e) {
  41. Log::error('RegeneratePdfJob: 异常', [
  42. 'paper_id' => $this->paperId,
  43. 'error' => $e->getMessage(),
  44. ]);
  45. throw $e;
  46. }
  47. }
  48. }