修改自动推广的相关任务

This commit is contained in:
wangjinlei
2026-04-29 17:59:13 +08:00
parent f2fabac740
commit 9a9a69333b
10 changed files with 340 additions and 98 deletions

View File

@@ -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);
}
// 兼容 \ThrowablePHP7 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)
{