| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace App\Jobs;
- use App\Services\ExamPdfExportService;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Support\Facades\Log;
- use Throwable;
- class GenerateAnalysisPdfJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- public string $paperId;
- public string $studentId;
- public ?string $recordId;
- public int $tries = 3;
- /**
- * 学情报告 HTML + 数据聚合比「整卷合并 PDF」更重,默认 60s 队列超时容易在 worker 侧被提前杀掉,
- * 表现为「卷子 PDF 正常、分析报告 PDF 一直失败」。与 {@see GenerateExamPdfJob} 对齐。
- */
- public int $timeout = 300;
- public array $backoff = [5, 10, 20];
- public function __construct(string $paperId, string $studentId, ?string $recordId = null)
- {
- $this->paperId = $paperId;
- $this->studentId = $studentId;
- $this->recordId = $recordId;
- // 指定使用 pdf 队列,由独立的 pdf-worker 容器处理
- $this->onQueue((string) config('queue.workloads.pdf', 'pdf'));
- // 避免事务未提交时 worker 提前消费导致读到未提交数据
- $this->afterCommit();
- }
- public function handle(ExamPdfExportService $pdfExportService): void
- {
- try {
- Log::info('开始处理学情分析PDF生成队列任务', [
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'record_id' => $this->recordId,
- 'attempt' => $this->attempts(),
- ]);
- // 生成学情分析PDF
- $pdfUrl = $pdfExportService->generateAnalysisReportPdf(
- $this->paperId,
- $this->studentId,
- $this->recordId
- );
- if (! $pdfUrl) {
- throw new \RuntimeException('学情分析PDF生成失败:返回空URL');
- }
- Log::info('学情分析PDF生成成功', [
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'record_id' => $this->recordId,
- 'pdf_url' => $pdfUrl,
- ]);
- } catch (\Throwable $e) {
- Log::error('学情分析PDF生成队列任务失败', [
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'record_id' => $this->recordId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString(),
- 'attempt' => $this->attempts(),
- ]);
- throw $e;
- }
- }
- public function failed(Throwable $exception): void
- {
- Log::error('学情分析PDF生成队列任务最终失败', [
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'record_id' => $this->recordId,
- 'error' => $exception->getMessage(),
- ]);
- }
- }
|