| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?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;
- 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('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(),
- ]);
- }
- }
|