| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- namespace App\Jobs;
- use App\Models\Paper;
- use App\Services\ExamPdfExportService;
- use App\Services\QuestionBankService;
- use App\Services\PaperPayloadService;
- 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;
- class GenerateExamPdfJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- public string $taskId;
- public string $paperId;
- /**
- * 【优化】增加任务超时时间,给Chrome充足渲染时间
- */
- public $timeout = 180; // 从90秒增加到180秒(3分钟)
- /**
- * 【保持】重试次数,配合并发控制
- */
- public $tries = 3;
- public function __construct(string $taskId, string $paperId)
- {
- $this->taskId = $taskId;
- $this->paperId = $paperId;
- $this->onQueue('pdf_generation');
- }
- public function handle(
- ExamPdfExportService $pdfExportService,
- QuestionBankService $questionBankService,
- PaperPayloadService $paperPayloadService,
- TaskManager $taskManager
- ): void {
- $jobId = $this->uuid ?? 'unknown';
- $startTime = microtime(true);
- try {
- Log::info('开始处理PDF生成队列任务', [
- 'job_id' => $jobId,
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'attempt' => $this->attempts(),
- ]);
- // 【并发控制】检查当前并发数量
- if (!$this->acquireLock()) {
- $waitTime = rand(5, 15); // 等待5-15秒后重试
- Log::warning('GenerateExamPdfJob: 并发限制,等待后重试', [
- 'job_id' => $jobId,
- 'wait_time' => $waitTime,
- 'current_concurrent' => $this->getConcurrentCount()
- ]);
- // 释放队列槽位,等待后重试
- $this->release($waitTime);
- return;
- }
- Log::info('GenerateExamPdfJob: 获得执行权限', [
- 'job_id' => $jobId,
- 'concurrent_count' => $this->getConcurrentCount()
- ]);
- // 【新增】快速检查:如果任务已完成,直接跳过
- $task = $taskManager->getTaskStatus($this->taskId);
- if ($task && $task['status'] === 'completed') {
- Log::info('【跳过执行】任务已完成,无需重复生成PDF', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'status' => $task['status']
- ]);
- return;
- }
- // 【修复】首先检查试卷是否存在
- $paperModel = Paper::with('questions')->find($this->paperId);
- if (!$paperModel) {
- Log::error('PDF生成队列任务失败:试卷不存在', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'attempt' => $this->attempts(),
- ]);
- // 如果试卷不存在,判断是否需要重试
- if ($this->attempts() < $this->tries) {
- Log::info('试卷不存在,将在2秒后重试', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'attempt' => $this->attempts(),
- 'next_attempt' => $this->attempts() + 1,
- ]);
- // 延迟2秒后重试(缩短间隔,减少对回调的影响)
- $this->release(2);
- return;
- } else {
- Log::error('试卷不存在且已达到最大重试次数,标记任务失败', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'attempts' => $this->attempts(),
- ]);
- $taskManager->markTaskFailed($this->taskId, "试卷不存在: {$this->paperId}");
- return;
- }
- }
- // 检查试卷是否有题目
- if ($paperModel->questions->isEmpty()) {
- Log::error('PDF生成队列任务失败:试卷没有题目数据', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'question_count' => 0,
- ]);
- if ($this->attempts() < $this->tries) {
- Log::info('试卷没有题目,将在1秒后重试', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'attempt' => $this->attempts(),
- ]);
- // 延迟1秒后重试(更短间隔)
- $this->release(1);
- return;
- } else {
- $taskManager->markTaskFailed($this->taskId, "试卷没有题目数据: {$this->paperId}");
- return;
- }
- }
- $taskManager->updateTaskProgress($this->taskId, 10, '开始生成试卷PDF...');
- // 生成试卷PDF
- $pdfUrl = $pdfExportService->generateExamPdf($this->paperId)
- ?? $questionBankService->exportExamToPdf($this->paperId)
- ?? route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $this->paperId, 'answer' => 'false']);
- $taskManager->updateTaskProgress($this->taskId, 50, '试卷PDF生成完成,开始生成判卷PDF...');
- // 生成判卷PDF
- $gradingPdfUrl = $pdfExportService->generateGradingPdf($this->paperId)
- ?? route('filament.admin.auth.intelligent-exam.pdf', ['paper_id' => $this->paperId, 'answer' => 'true']);
- $taskManager->updateTaskProgress($this->taskId, 70, '判卷PDF生成完成,开始合并PDF...');
- // 【优化】生成合并PDF(试卷 + 判卷) - 使用快速合并模式
- $mergedPdfUrl = $pdfExportService->generateMergedPdf($this->paperId, function($percentage, $message) use ($taskManager) {
- // 进度更新:70% 开始,最高到 95%
- $progress = 70 + ($percentage / 100) * 25;
- $taskManager->updateTaskProgress($this->taskId, round($progress, 0), $message);
- });
- // 【新增】验证合并后的PDF URL
- if (!$mergedPdfUrl) {
- Log::error('PDF生成队列任务失败:合并PDF失败', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'attempt' => $this->attempts(),
- ]);
- if ($this->attempts() < $this->tries) {
- Log::info('合并PDF失败,将在3秒后重试', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'attempt' => $this->attempts(),
- 'next_attempt' => $this->attempts() + 1,
- ]);
- // 延迟3秒后重试
- $this->release(3);
- return;
- } else {
- Log::error('合并PDF失败且已达到最大重试次数,标记任务失败', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'attempts' => $this->attempts(),
- ]);
- $taskManager->markTaskFailed($this->taskId, "合并PDF失败: {$this->paperId}");
- return;
- }
- }
- Log::info('PDF合并成功验证', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'merged_pdf_url' => $mergedPdfUrl,
- 'url_length' => strlen($mergedPdfUrl)
- ]);
- // 构建完整的试卷内容
- $examContent = $paperPayloadService->buildExamContent($paperModel);
- // 标记任务完成(包含合并后的PDF URL)
- $taskManager->markTaskCompleted($this->taskId, [
- 'exam_content' => $examContent,
- 'pdfs' => [
- 'exam_paper_pdf' => $pdfUrl,
- 'grading_pdf' => $gradingPdfUrl,
- 'all_pdf' => $mergedPdfUrl, // 【新增】合并后的完整PDF
- ],
- ]);
- Log::info('PDF生成队列任务完成', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'pdf_url' => $pdfUrl,
- 'grading_pdf_url' => $gradingPdfUrl,
- 'merged_pdf_url' => $mergedPdfUrl,
- 'question_count' => $paperModel->questions->count(),
- ]);
- // 发送回调通知(在合并PDF完成后)
- $taskManager->sendCallback($this->taskId);
- } catch (\Exception $e) {
- $elapsed = microtime(true) - $startTime;
- Log::error('PDF生成队列任务失败', [
- 'job_id' => $jobId,
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'attempt' => $this->attempts(),
- 'max_attempts' => $this->tries,
- 'elapsed' => round($elapsed, 2) . 's',
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString(),
- ]);
- // 如果是第一次失败且试卷可能还在创建中,等待后重试
- if ($this->attempts() < $this->tries && strpos($e->getMessage(), '不存在') !== false) {
- Log::info('检测到试卷不存在错误,将在2秒后重试', [
- 'task_id' => $this->taskId,
- 'paper_id' => $this->paperId,
- 'attempt' => $this->attempts(),
- ]);
- $this->release(2);
- return;
- }
- $taskManager->markTaskFailed($this->taskId, $e->getMessage());
- } finally {
- // 【并发控制】释放锁
- $this->releaseLock();
- $currentCount = $this->getConcurrentCount();
- Log::info('GenerateExamPdfJob: 任务完成', [
- 'job_id' => $jobId,
- 'total_elapsed' => round(microtime(true) - $startTime, 2) . 's',
- 'remaining_concurrent' => $currentCount
- ]);
- }
- }
- /**
- * 【并发控制】获取执行锁
- */
- private function acquireLock(): bool
- {
- $currentCount = $this->getConcurrentCount();
- if ($currentCount >= self::MAX_CONCURRENT_JOBS) {
- return false;
- }
- // 增加计数器
- Redis::incr(self::REDIS_KEY_CONCURRENT);
- // 设置锁过期时间(2小时)
- $lockKey = self::REDIS_KEY_LOCK_PREFIX . $this->taskId;
- Redis::setex($lockKey, 7200, 1);
- return true;
- }
- /**
- * 【并发控制】释放执行锁
- */
- private function releaseLock(): void
- {
- // 减少计数器
- $currentCount = Redis::decr(self::REDIS_KEY_CONCURRENT);
- if ($currentCount < 0) {
- Redis::set(self::REDIS_KEY_CONCURRENT, 0);
- }
- // 删除锁
- $lockKey = self::REDIS_KEY_LOCK_PREFIX . $this->taskId;
- Redis::del($lockKey);
- }
- /**
- * 【并发控制】获取当前并发数量
- */
- private function getConcurrentCount(): int
- {
- $count = Redis::get(self::REDIS_KEY_CONCURRENT);
- return $count ? (int)$count : 0;
- }
- }
|