ProcessExamAnswerAnalysisJob.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Jobs;
  3. use App\Services\ExamAnswerAnalysisService;
  4. use App\Services\TaskManager;
  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. use Throwable;
  12. class ProcessExamAnswerAnalysisJob implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. public int $tries = 2;
  16. public int $timeout = 300;
  17. public array $backoff = [5, 15];
  18. /**
  19. * @param array<string,mixed> $examData
  20. */
  21. public function __construct(
  22. public string $taskId,
  23. public array $examData
  24. ) {
  25. // Keep the default on the scaled worker fleet. In production today the
  26. // PDF nodes are the only horizontally scaled queue consumers.
  27. $this->onQueue((string) config('queue.workloads.exam_answer_analysis', 'pdf'));
  28. $this->afterCommit();
  29. }
  30. public function handle(ExamAnswerAnalysisService $analysisService, TaskManager $taskManager): void
  31. {
  32. try {
  33. $taskManager->updateTaskProgress($this->taskId, 10, '正在分析考试答题...');
  34. Log::warning('ProcessExamAnswerAnalysisJob: 开始处理考试答题分析', [
  35. 'task_id' => $this->taskId,
  36. 'paper_id' => $this->examData['paper_id'] ?? null,
  37. 'student_id' => $this->examData['student_id'] ?? null,
  38. 'question_count' => count($this->examData['questions'] ?? []),
  39. 'attempt' => $this->attempts(),
  40. 'memory_mb' => round(memory_get_usage(true) / 1024 / 1024, 2),
  41. ]);
  42. $result = $analysisService->analyzeExamAnswers($this->examData);
  43. $taskManager->markTaskCompleted($this->taskId, [
  44. 'paper_id' => $this->examData['paper_id'] ?? null,
  45. 'student_id' => $this->examData['student_id'] ?? null,
  46. 'analysis_status' => 'completed',
  47. 'pdf_status' => 'queued',
  48. 'analyzed_knowledge_points' => count($result['knowledge_point_analysis'] ?? []),
  49. 'result_url' => isset($this->examData['student_id'], $this->examData['paper_id'])
  50. ? route('api.exam-answer-analysis.result', [
  51. 'student_id' => $this->examData['student_id'],
  52. 'paper_id' => $this->examData['paper_id'],
  53. ])
  54. : null,
  55. 'pdf_lookup_url' => isset($this->examData['paper_id'])
  56. ? route('api.exam-analysis.pdf', ['paper_id' => $this->examData['paper_id']])
  57. : null,
  58. ]);
  59. $taskManager->sendCallback($this->taskId);
  60. Log::warning('ProcessExamAnswerAnalysisJob: 考试答题分析完成', [
  61. 'task_id' => $this->taskId,
  62. 'paper_id' => $this->examData['paper_id'] ?? null,
  63. 'student_id' => $this->examData['student_id'] ?? null,
  64. 'memory_peak_mb' => round(memory_get_peak_usage(true) / 1024 / 1024, 2),
  65. ]);
  66. } catch (Throwable $e) {
  67. $taskManager->markTaskFailed($this->taskId, $e->getMessage());
  68. $taskManager->sendCallback($this->taskId);
  69. Log::error('ProcessExamAnswerAnalysisJob: 考试答题分析失败', [
  70. 'task_id' => $this->taskId,
  71. 'paper_id' => $this->examData['paper_id'] ?? null,
  72. 'student_id' => $this->examData['student_id'] ?? null,
  73. 'error' => $e->getMessage(),
  74. 'exception' => get_class($e),
  75. 'memory_peak_mb' => round(memory_get_peak_usage(true) / 1024 / 1024, 2),
  76. ]);
  77. throw $e;
  78. }
  79. }
  80. public function failed(Throwable $exception): void
  81. {
  82. Log::error('ProcessExamAnswerAnalysisJob: 队列任务最终失败', [
  83. 'task_id' => $this->taskId,
  84. 'paper_id' => $this->examData['paper_id'] ?? null,
  85. 'student_id' => $this->examData['student_id'] ?? null,
  86. 'error' => $exception->getMessage(),
  87. ]);
  88. }
  89. }