ProcessExamAnswerAnalysisJob.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. $this->onQueue('pdf');
  26. $this->afterCommit();
  27. }
  28. public function handle(ExamAnswerAnalysisService $analysisService, TaskManager $taskManager): void
  29. {
  30. try {
  31. $taskManager->updateTaskProgress($this->taskId, 10, '正在分析考试答题...');
  32. Log::warning('ProcessExamAnswerAnalysisJob: 开始处理考试答题分析', [
  33. 'task_id' => $this->taskId,
  34. 'paper_id' => $this->examData['paper_id'] ?? null,
  35. 'student_id' => $this->examData['student_id'] ?? null,
  36. 'question_count' => count($this->examData['questions'] ?? []),
  37. 'attempt' => $this->attempts(),
  38. 'memory_mb' => round(memory_get_usage(true) / 1024 / 1024, 2),
  39. ]);
  40. $result = $analysisService->analyzeExamAnswers($this->examData);
  41. $taskManager->markTaskCompleted($this->taskId, [
  42. 'paper_id' => $this->examData['paper_id'] ?? null,
  43. 'student_id' => $this->examData['student_id'] ?? null,
  44. 'analysis_status' => 'completed',
  45. 'pdf_status' => 'queued',
  46. 'analyzed_knowledge_points' => count($result['knowledge_point_analysis'] ?? []),
  47. 'result_url' => isset($this->examData['student_id'], $this->examData['paper_id'])
  48. ? route('api.exam-answer-analysis.result', [
  49. 'student_id' => $this->examData['student_id'],
  50. 'paper_id' => $this->examData['paper_id'],
  51. ])
  52. : null,
  53. 'pdf_lookup_url' => isset($this->examData['paper_id'])
  54. ? route('api.exam-analysis.pdf', ['paper_id' => $this->examData['paper_id']])
  55. : null,
  56. ]);
  57. $taskManager->sendCallback($this->taskId);
  58. Log::warning('ProcessExamAnswerAnalysisJob: 考试答题分析完成', [
  59. 'task_id' => $this->taskId,
  60. 'paper_id' => $this->examData['paper_id'] ?? null,
  61. 'student_id' => $this->examData['student_id'] ?? null,
  62. 'memory_peak_mb' => round(memory_get_peak_usage(true) / 1024 / 1024, 2),
  63. ]);
  64. } catch (Throwable $e) {
  65. $taskManager->markTaskFailed($this->taskId, $e->getMessage());
  66. $taskManager->sendCallback($this->taskId);
  67. Log::error('ProcessExamAnswerAnalysisJob: 考试答题分析失败', [
  68. 'task_id' => $this->taskId,
  69. 'paper_id' => $this->examData['paper_id'] ?? null,
  70. 'student_id' => $this->examData['student_id'] ?? null,
  71. 'error' => $e->getMessage(),
  72. 'exception' => get_class($e),
  73. 'memory_peak_mb' => round(memory_get_peak_usage(true) / 1024 / 1024, 2),
  74. ]);
  75. throw $e;
  76. }
  77. }
  78. public function failed(Throwable $exception): void
  79. {
  80. Log::error('ProcessExamAnswerAnalysisJob: 队列任务最终失败', [
  81. 'task_id' => $this->taskId,
  82. 'paper_id' => $this->examData['paper_id'] ?? null,
  83. 'student_id' => $this->examData['student_id'] ?? null,
  84. 'error' => $exception->getMessage(),
  85. ]);
  86. }
  87. }