Files
tougao/application/api/job/PromotionPrepareEmail.php
2026-04-24 13:13:06 +08:00

53 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\api\job;
use think\queue\Job;
use app\common\PromotionService;
/**
* 队列任务:单封邮件 prepare调用 LLM 生成个性化描述 + 渲染模板 + 写入 log
*
* 单个 Job 可能耗时较长LLM 调用 30s 超时),因此使用独立队列 promotion_email
* 多 worker 并行消费即可提升吞吐。
*
* 失败策略:
* - PromotionService::prepareSingleEmail 内部已做兜底LLM 失败回退到配置文案),
* 这里遇到未捕获异常时允许重试一次,超过 3 次直接 delete 避免无限循环。
*
* 队列名promotion_email
* 启动 workerphp think queue:listen --queue promotion_email
*/
class PromotionPrepareEmail
{
public function fire(Job $job, $data)
{
$logId = intval(isset($data['log_id']) ? $data['log_id'] : 0);
$service = new PromotionService();
if (!$logId) {
$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());
if ($job->attempts() >= 3) {
$job->delete();
} else {
$job->release(30);
}
}
}
}