修改自动推广的相关任务
This commit is contained in:
@@ -152,6 +152,54 @@ class QueueJob
|
||||
$job->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 简化版异常处理(无 Redis 锁场景)。
|
||||
*
|
||||
* 适用于一对一类型的 job:每个 job 处理一个自然唯一的资源(如 log_id/expert_id),
|
||||
* 不需要 Redis 锁也不会重复消费。
|
||||
*
|
||||
* 行为:
|
||||
* - 致命 DB 错误(连接断开等)→ release 任务回队列 + exit(1) 让 supervisor 重启进程
|
||||
* - 普通异常 → 按 attempts 限次重试(默认最多 3 次,超过 delete)
|
||||
*
|
||||
* @param \Exception|\Throwable $e
|
||||
* @param \think\queue\Job $job
|
||||
* @param string $context 日志中显示的上下文标识(如 "log_id=123" / "task_id=45")
|
||||
*/
|
||||
public function handleException($e, $job, $context = '')
|
||||
{
|
||||
$msg = $e->getMessage();
|
||||
$this->log("队列异常 | {$context} | msg={$msg}");
|
||||
|
||||
if ($e instanceof \Exception && $this->isFatalDatabaseError($e)) {
|
||||
$this->log("致命 DB 错误,释放任务并退出进程让 supervisor 重启 | {$context}");
|
||||
$job->release(60);
|
||||
exit(1);
|
||||
}
|
||||
// 兼容 \Throwable(PHP7 fatal 等)
|
||||
if (!($e instanceof \Exception)) {
|
||||
$low = strtolower((string)$msg);
|
||||
$hits = ['mysql server has gone away', 'lost connection to mysql', 'gone away', 'sqlstate[hy000]'];
|
||||
foreach ($hits as $kw) {
|
||||
if (strpos($low, $kw) !== false) {
|
||||
$this->log("致命 DB 错误(Throwable),释放任务并退出进程 | {$context}");
|
||||
$job->release(60);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$attempts = method_exists($job, 'attempts') ? $job->attempts() : 1;
|
||||
if ($attempts >= $this->maxRetries) {
|
||||
$this->log("超过最大重试次数({$this->maxRetries}),删除任务 | {$context}");
|
||||
$job->delete();
|
||||
return;
|
||||
}
|
||||
$delay = $this->getRetryDelay($msg);
|
||||
$this->log("{$delay} 秒后重试({$attempts}/{$this->maxRetries}) | {$context}");
|
||||
$job->release($delay);
|
||||
}
|
||||
|
||||
// 判断是否为需要重启的致命数据库错误
|
||||
private function isFatalDatabaseError(\Exception $e)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user