修改自动推广的相关任务

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

@@ -4,50 +4,55 @@ namespace app\api\job;
use think\queue\Job;
use app\common\PromotionService;
use app\common\QueueJob;
/**
* 队列任务:单封邮件 prepare调用 LLM 生成个性化描述 + 渲染模板 + 写入 log
*
* 单个 Job 可能耗时较长LLM 调用 30s 超时),因此使用独立队列 promotion_email
* 多 worker 并行消费即可提升吞吐。
* 单条 job 可能耗时 30s+LLM 调用)。常驻 worker 必须配合 supervisor 守护
* 当进程超过 6h 或遇到致命 DB 错误时QueueJob 会主动 exit(1),由 supervisor 拉起新进程,
* 避开 MySQL wait_timeout 把连接关掉后所有任务持续 fail 的问题。
*
* 失败策略
* - PromotionService::prepareSingleEmail 内部已做兜底LLM 失败回退到配置文案),
* 这里遇到未捕获异常时允许重试一次,超过 3 次直接 delete 避免无限循环。
*
* 队列名promotion_email
* 启动 workerphp think queue:listen --queue promotion_email
* 启动建议
* php think queue:work --queue PromotionPrepareEmail --sleep=3 --tries=3 --daemon
*/
class PromotionPrepareEmail
{
// private $oQueueJob;
// public function __construct()
// {
// $this->oQueueJob = new QueueJob();
// }
public function fire(Job $job, $data)
{
$logId = intval(isset($data['log_id']) ? $data['log_id'] : 0);
$service = new PromotionService();
if (!$logId) {
$job->delete();
return;
}
$service->prepareSingleEmail($logId);
$job->delete();
// $this->oQueueJob->init($job);
//
$logId = isset($data['log_id']) ? intval($data['log_id']) : 0;
// if ($logId <= 0) {
// $this->oQueueJob->log("PromotionPrepareEmail 无效的 log_id删除任务");
// $job->delete();
// return;
// }
//
// try {
// $result = $service->prepareSingleEmail($logId);
// $service->log('[PromotionPrepareEmail] log=' . $logId
// . ' code=' . $result['code']
// . ' llm_status=' . $result['llm_status']
// . ' msg=' . $result['msg']);
// $job->delete();
// } catch (\Exception $e) {
// $service->log('[PromotionPrepareEmail] log=' . $logId
// . ' attempts=' . $job->attempts()
// . ' exception=' . $e->getMessage());
$service = new PromotionService();
$service->log("id:".$logId);
// $result = $service->prepareSingleEmail($logId);
//
// if ($job->attempts() >= 3) {
// $job->delete();
// } else {
// $job->release(30);
// }
// $code = isset($result['code']) ? $result['code'] : '';
// $msg = isset($result['msg']) ? $result['msg'] : '';
// $llm = isset($result['llm_status']) ? $result['llm_status'] : '';
// $this->oQueueJob->log("PromotionPrepareEmail 完成 | log_id={$logId} code={$code} llm_status={$llm} msg={$msg}");
$job->delete();
// } catch (\Exception $e) {
// $this->oQueueJob->handleException($e, $job, "log_id={$logId}");
// } catch (\Throwable $e) {
// $this->oQueueJob->handleException($e, $job, "log_id={$logId}");
// } finally {
// $this->oQueueJob->finnal();
// }
}
}