|
@@ -13,6 +13,7 @@ use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
+use Throwable;
|
|
|
|
|
|
|
|
class GenerateExamPdfJob implements ShouldQueue
|
|
class GenerateExamPdfJob implements ShouldQueue
|
|
|
{
|
|
{
|
|
@@ -22,7 +23,9 @@ class GenerateExamPdfJob implements ShouldQueue
|
|
|
|
|
|
|
|
public string $paperId;
|
|
public string $paperId;
|
|
|
|
|
|
|
|
- public int $maxAttempts = 3;
|
|
|
|
|
|
|
+ public int $tries = 3;
|
|
|
|
|
+
|
|
|
|
|
+ public int $timeout = 300;
|
|
|
|
|
|
|
|
public function __construct(string $taskId, string $paperId)
|
|
public function __construct(string $taskId, string $paperId)
|
|
|
{
|
|
{
|
|
@@ -31,6 +34,8 @@ class GenerateExamPdfJob implements ShouldQueue
|
|
|
|
|
|
|
|
// 指定使用 pdf 队列,由独立的 pdf-worker 容器处理
|
|
// 指定使用 pdf 队列,由独立的 pdf-worker 容器处理
|
|
|
$this->onQueue('pdf');
|
|
$this->onQueue('pdf');
|
|
|
|
|
+ // 避免事务未提交时 worker 提前消费导致“试卷不存在”
|
|
|
|
|
+ $this->afterCommit();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function handle(
|
|
public function handle(
|
|
@@ -47,7 +52,11 @@ class GenerateExamPdfJob implements ShouldQueue
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
// 【修复】首先检查试卷是否存在
|
|
// 【修复】首先检查试卷是否存在
|
|
|
- $paperModel = Paper::with('questions')->find($this->paperId);
|
|
|
|
|
|
|
+ // 强制走主库读取,避免读写分离下新建试卷短时不可见导致“试卷不存在”
|
|
|
|
|
+ $paperModel = Paper::query()
|
|
|
|
|
+ ->useWritePdo()
|
|
|
|
|
+ ->with('questions')
|
|
|
|
|
+ ->find($this->paperId);
|
|
|
if (! $paperModel) {
|
|
if (! $paperModel) {
|
|
|
Log::error('PDF生成队列任务失败:试卷不存在', [
|
|
Log::error('PDF生成队列任务失败:试卷不存在', [
|
|
|
'task_id' => $this->taskId,
|
|
'task_id' => $this->taskId,
|
|
@@ -56,7 +65,7 @@ class GenerateExamPdfJob implements ShouldQueue
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
// 如果试卷不存在,判断是否需要重试
|
|
// 如果试卷不存在,判断是否需要重试
|
|
|
- if ($this->attempts() < $this->maxAttempts) {
|
|
|
|
|
|
|
+ if ($this->attempts() < $this->tries) {
|
|
|
Log::info('试卷不存在,将在2秒后重试', [
|
|
Log::info('试卷不存在,将在2秒后重试', [
|
|
|
'task_id' => $this->taskId,
|
|
'task_id' => $this->taskId,
|
|
|
'paper_id' => $this->paperId,
|
|
'paper_id' => $this->paperId,
|
|
@@ -87,7 +96,7 @@ class GenerateExamPdfJob implements ShouldQueue
|
|
|
'question_count' => 0,
|
|
'question_count' => 0,
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- if ($this->attempts() < $this->maxAttempts) {
|
|
|
|
|
|
|
+ if ($this->attempts() < $this->tries) {
|
|
|
Log::info('试卷没有题目,将在1秒后重试', [
|
|
Log::info('试卷没有题目,将在1秒后重试', [
|
|
|
'task_id' => $this->taskId,
|
|
'task_id' => $this->taskId,
|
|
|
'paper_id' => $this->paperId,
|
|
'paper_id' => $this->paperId,
|
|
@@ -147,7 +156,7 @@ class GenerateExamPdfJob implements ShouldQueue
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
// 如果是第一次失败且试卷可能还在创建中,等待后重试
|
|
// 如果是第一次失败且试卷可能还在创建中,等待后重试
|
|
|
- if ($this->attempts() < $this->maxAttempts && strpos($e->getMessage(), '不存在') !== false) {
|
|
|
|
|
|
|
+ if ($this->attempts() < $this->tries && strpos($e->getMessage(), '不存在') !== false) {
|
|
|
Log::info('检测到试卷不存在错误,将在2秒后重试', [
|
|
Log::info('检测到试卷不存在错误,将在2秒后重试', [
|
|
|
'task_id' => $this->taskId,
|
|
'task_id' => $this->taskId,
|
|
|
'paper_id' => $this->paperId,
|
|
'paper_id' => $this->paperId,
|
|
@@ -161,4 +170,23 @@ class GenerateExamPdfJob implements ShouldQueue
|
|
|
$taskManager->markTaskFailed($this->taskId, $e->getMessage());
|
|
$taskManager->markTaskFailed($this->taskId, $e->getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public function failed(Throwable $exception): void
|
|
|
|
|
+ {
|
|
|
|
|
+ try {
|
|
|
|
|
+ app(TaskManager::class)->markTaskFailed($this->taskId, $exception->getMessage());
|
|
|
|
|
+ } catch (Throwable $innerException) {
|
|
|
|
|
+ Log::error('PDF生成队列任务失败回调异常', [
|
|
|
|
|
+ 'task_id' => $this->taskId,
|
|
|
|
|
+ 'paper_id' => $this->paperId,
|
|
|
|
|
+ 'error' => $innerException->getMessage(),
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Log::error('PDF生成队列任务最终失败', [
|
|
|
|
|
+ 'task_id' => $this->taskId,
|
|
|
|
|
+ 'paper_id' => $this->paperId,
|
|
|
|
|
+ 'error' => $exception->getMessage(),
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|