| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace App\Jobs;
- use App\Services\Analytics\QuestionDifficultyCalibrationService;
- 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 ProcessQuestionDifficultyCalibrationJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- public int $tries = 1;
- public int $timeout = 300;
- /**
- * @param list<array<string,mixed>> $questions
- */
- public function __construct(
- public string $paperId,
- public string $studentId,
- public array $questions
- ) {
- $this->questions = $this->compactQuestions($questions);
- // 放在 pdf 队列,且由调用方在 PDF Job 之后入队,保证报告优先生成。
- $this->onQueue('pdf');
- $this->afterCommit();
- }
- public function handle(QuestionDifficultyCalibrationService $calibrationService): void
- {
- Log::warning('QuestionDifficultyCalibrationJob: 开始异步在线难度校准', [
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'question_count' => count($this->questions),
- 'attempt' => $this->attempts(),
- 'memory_mb' => round(memory_get_usage(true) / 1024 / 1024, 2),
- ]);
- $updatedQuestions = $calibrationService->updateOnlineFromPaper($this->paperId, $this->questions);
- Log::warning('QuestionDifficultyCalibrationJob: 异步在线难度校准完成', [
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'updated_questions' => $updatedQuestions,
- 'memory_peak_mb' => round(memory_get_peak_usage(true) / 1024 / 1024, 2),
- ]);
- }
- public function failed(Throwable $exception): void
- {
- Log::error('QuestionDifficultyCalibrationJob: 异步在线难度校准最终失败', [
- 'paper_id' => $this->paperId,
- 'student_id' => $this->studentId,
- 'question_count' => count($this->questions),
- 'error' => $exception->getMessage(),
- 'exception' => get_class($exception),
- 'memory_peak_mb' => round(memory_get_peak_usage(true) / 1024 / 1024, 2),
- ]);
- }
- /**
- * @param list<array<string,mixed>> $questions
- * @return list<array<string,mixed>>
- */
- private function compactQuestions(array $questions): array
- {
- $compact = [];
- foreach ($questions as $question) {
- if (! is_array($question)) {
- continue;
- }
- $questionId = $question['question_id'] ?? $question['question_bank_id'] ?? null;
- if ($questionId === null || $questionId === '') {
- continue;
- }
- $compact[] = [
- 'question_id' => $questionId,
- 'is_correct' => $question['is_correct'] ?? [],
- ];
- }
- return $compact;
- }
- }
|