| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?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;
- class GenerateAnalysisPdfJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- /**
- * 指定队列名称 - PDF 生成需要 Chrome,必须在 pdf 队列处理
- */
- public string $queue = 'pdf';
- public string $paperId;
- public string $studentId;
- public ?string $recordId;
- public int $maxAttempts = 3;
- public function __construct(string $paperId, string $studentId, ?string $recordId = null)
- {
- $this->paperId = $paperId;
- $this->studentId = $studentId;
- $this->recordId = $recordId;
- }
- 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) {
- Log::info('学情分析PDF生成成功', [
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'record_id' => $this->recordId,
- 'pdf_url' => $pdfUrl,
- ]);
- } else {
- Log::error('学情分析PDF生成失败', [
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'record_id' => $this->recordId,
- ]);
- // 如果失败且还有重试次数,则重试
- if ($this->attempts() < $this->maxAttempts) {
- Log::info('将在5秒后重试PDF生成', [
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'attempt' => $this->attempts(),
- ]);
- $this->release(5);
- return;
- }
- }
- } catch (\Exception $e) {
- Log::error('学情分析PDF生成队列任务失败', [
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'record_id' => $this->recordId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString(),
- ]);
- // 如果是第一次失败且可能是临时错误,等待后重试
- if ($this->attempts() < $this->maxAttempts) {
- Log::info('检测到临时错误,将在10秒后重试', [
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'attempt' => $this->attempts(),
- ]);
- $this->release(10);
- return;
- }
- }
- }
- }
|