|
|
@@ -9,6 +9,7 @@ use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
+use Throwable;
|
|
|
|
|
|
class GenerateAnalysisPdfJob implements ShouldQueue
|
|
|
{
|
|
|
@@ -17,7 +18,10 @@ class GenerateAnalysisPdfJob implements ShouldQueue
|
|
|
public string $paperId;
|
|
|
public string $studentId;
|
|
|
public ?string $recordId;
|
|
|
- public int $maxAttempts = 3;
|
|
|
+ public int $tries = 3;
|
|
|
+ public int $timeout = 300;
|
|
|
+
|
|
|
+ public array $backoff = [5, 10, 20];
|
|
|
|
|
|
public function __construct(string $paperId, string $studentId, ?string $recordId = null)
|
|
|
{
|
|
|
@@ -27,6 +31,8 @@ class GenerateAnalysisPdfJob implements ShouldQueue
|
|
|
|
|
|
// 指定使用 pdf 队列,由独立的 pdf-worker 容器处理
|
|
|
$this->onQueue('pdf');
|
|
|
+ // 避免事务未提交时 worker 提前消费导致读到未提交数据
|
|
|
+ $this->afterCommit();
|
|
|
}
|
|
|
|
|
|
public function handle(ExamPdfExportService $pdfExportService): void
|
|
|
@@ -46,51 +52,37 @@ class GenerateAnalysisPdfJob implements ShouldQueue
|
|
|
$this->recordId
|
|
|
);
|
|
|
|
|
|
- if ($pdfUrl) {
|
|
|
- Log::info('学情分析PDF生成成功', [
|
|
|
- 'paper_id' => $this->paperId,
|
|
|
- 'student_id' => $this->studentId,
|
|
|
- 'record_id' => $this->recordId,
|
|
|
- 'pdf_url' => $pdfUrl,
|
|
|
- ]);
|
|
|
- } else {
|
|
|
- Log::error('学情分析PDF生成失败', [
|
|
|
- 'paper_id' => $this->paperId,
|
|
|
- 'student_id' => $this->studentId,
|
|
|
- 'record_id' => $this->recordId,
|
|
|
- ]);
|
|
|
-
|
|
|
- // 如果失败且还有重试次数,则重试
|
|
|
- if ($this->attempts() < $this->maxAttempts) {
|
|
|
- Log::info('将在5秒后重试PDF生成', [
|
|
|
- 'paper_id' => $this->paperId,
|
|
|
- 'student_id' => $this->studentId,
|
|
|
- 'attempt' => $this->attempts(),
|
|
|
- ]);
|
|
|
- $this->release(5);
|
|
|
- return;
|
|
|
- }
|
|
|
+ if (! $pdfUrl) {
|
|
|
+ throw new \RuntimeException('学情分析PDF生成失败:返回空URL');
|
|
|
}
|
|
|
|
|
|
- } catch (\Exception $e) {
|
|
|
+ Log::info('学情分析PDF生成成功', [
|
|
|
+ 'paper_id' => $this->paperId,
|
|
|
+ 'student_id' => $this->studentId,
|
|
|
+ 'record_id' => $this->recordId,
|
|
|
+ 'pdf_url' => $pdfUrl,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ } catch (\Throwable $e) {
|
|
|
Log::error('学情分析PDF生成队列任务失败', [
|
|
|
'paper_id' => $this->paperId,
|
|
|
'student_id' => $this->studentId,
|
|
|
'record_id' => $this->recordId,
|
|
|
'error' => $e->getMessage(),
|
|
|
'trace' => $e->getTraceAsString(),
|
|
|
+ 'attempt' => $this->attempts(),
|
|
|
]);
|
|
|
-
|
|
|
- // 如果是第一次失败且可能是临时错误,等待后重试
|
|
|
- if ($this->attempts() < $this->maxAttempts) {
|
|
|
- Log::info('检测到临时错误,将在10秒后重试', [
|
|
|
- 'paper_id' => $this->paperId,
|
|
|
- 'student_id' => $this->studentId,
|
|
|
- 'attempt' => $this->attempts(),
|
|
|
- ]);
|
|
|
- $this->release(10);
|
|
|
- return;
|
|
|
- }
|
|
|
+ throw $e;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public function failed(Throwable $exception): void
|
|
|
+ {
|
|
|
+ Log::error('学情分析PDF生成队列任务最终失败', [
|
|
|
+ 'paper_id' => $this->paperId,
|
|
|
+ 'student_id' => $this->studentId,
|
|
|
+ 'record_id' => $this->recordId,
|
|
|
+ 'error' => $exception->getMessage(),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
}
|