| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace App\Jobs;
- use App\Services\ExamAnalysisService;
- use App\Services\TaskManager;
- 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 ProcessAnalysisReportTaskJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- public int $tries = 3;
- public int $timeout = 300;
- public array $backoff = [5, 10, 20];
- public function __construct(
- public string $taskId,
- public string $paperId,
- public string $studentId,
- public ?string $recordId = null
- ) {
- // 与 PDF 相关重流程统一走 pdf 队列
- $this->onQueue('pdf');
- // 避免事务未提交时 worker 提前消费导致读到旧数据
- $this->afterCommit();
- }
- public function handle(ExamAnalysisService $examAnalysisService): void
- {
- try {
- Log::info('ProcessAnalysisReportTaskJob: 开始处理学情报告任务', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'record_id' => $this->recordId,
- 'attempt' => $this->attempts(),
- ]);
- $examAnalysisService->processReportGenerationTask(
- $this->taskId,
- $this->paperId,
- $this->studentId,
- $this->recordId
- );
- } catch (Throwable $e) {
- Log::error('ProcessAnalysisReportTaskJob: 处理失败,将交由队列重试/失败落库', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'record_id' => $this->recordId,
- 'attempt' => $this->attempts(),
- 'error' => $e->getMessage(),
- ]);
- throw $e;
- }
- }
- public function failed(Throwable $exception): void
- {
- try {
- $taskManager = app(TaskManager::class);
- $taskManager->markTaskFailed(
- $this->taskId,
- '学情报告生成失败:'.$exception->getMessage()
- );
- // 与成功路径保持一致:最终失败也发回调,避免调用方只等待回调而无结果。
- $taskManager->sendCallback($this->taskId);
- } catch (Throwable $innerException) {
- Log::error('ProcessAnalysisReportTaskJob: failed回调更新任务失败', [
- 'task_id' => $this->taskId,
- 'error' => $innerException->getMessage(),
- ]);
- }
- Log::error('ProcessAnalysisReportTaskJob: 队列任务最终失败', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'record_id' => $this->recordId,
- 'error' => $exception->getMessage(),
- ]);
- }
- }
|