| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace App\Jobs;
- use App\Models\KnowledgeExplanation;
- use App\Services\ExamPdfExportService;
- 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 GenerateKnowledgeExplanationPdfJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- public function __construct(
- public string $taskId,
- public string $knowledgeId,
- public array $knowledgePoints
- ) {
- $this->onQueue('pdf');
- $this->afterCommit();
- }
- public int $tries = 3;
- public int $timeout = 300;
- public function handle(
- ExamPdfExportService $examPdfExportService,
- TaskManager $taskManager
- ): void {
- try {
- $record = KnowledgeExplanation::query()
- ->where('knowledge_id', $this->knowledgeId)
- ->first();
- if (! $record) {
- $taskManager->markTaskFailed($this->taskId, '知识点讲解记录不存在');
- return;
- }
- $taskManager->updateTaskProgress($this->taskId, 70, '开始渲染知识点讲解PDF...');
- $pdfUrl = $examPdfExportService->generateKnowledgeExplanationStandalonePdf($record, $this->knowledgePoints);
- if (! $pdfUrl) {
- $record->update(['status' => 'failed']);
- $taskManager->markTaskFailed($this->taskId, '知识点讲解PDF生成失败');
- return;
- }
- $record->update([
- 'status' => 'completed',
- 'pdf_url' => $pdfUrl,
- 'generated_at' => now(),
- ]);
- $taskManager->markTaskCompleted($this->taskId, [
- 'paper_id' => $this->knowledgeId,
- 'knowledge_id' => $this->knowledgeId,
- 'pdfs' => [
- 'all_pdf' => $pdfUrl,
- ],
- 'exam_content' => [],
- ]);
- $taskManager->sendCallback($this->taskId);
- } catch (\Throwable $e) {
- Log::error('GenerateKnowledgeExplanationPdfJob 失败', [
- 'task_id' => $this->taskId,
- 'knowledge_id' => $this->knowledgeId,
- 'error' => $e->getMessage(),
- ]);
- KnowledgeExplanation::query()
- ->where('knowledge_id', $this->knowledgeId)
- ->update(['status' => 'failed']);
- $taskManager->markTaskFailed($this->taskId, $e->getMessage());
- }
- }
- public function failed(Throwable $exception): void
- {
- KnowledgeExplanation::query()
- ->where('knowledge_id', $this->knowledgeId)
- ->update(['status' => 'failed']);
- app(TaskManager::class)->markTaskFailed($this->taskId, $exception->getMessage());
- }
- }
|