修改自动推广的相关任务

This commit is contained in:
wangjinlei
2026-04-24 14:23:52 +08:00
parent e1bf8c0189
commit 7a204961de
3 changed files with 38 additions and 29 deletions

View File

@@ -1704,8 +1704,13 @@ class EmailClient extends Base
} }
/** /**
* 为单个任务预生成邮件(可手动或测试用) * 为单个任务预生成邮件:异步入队
* Params: task_id * Params: task_id
*
* 返回:
* queued 是否已入队false 表示当前没有待准备邮件)
* task_id 任务ID
* pending 需要准备的邮件数(大约值;实际以队列执行为准)
*/ */
public function prepareTask() public function prepareTask()
{ {

View File

@@ -53,6 +53,8 @@ class PromotionFactory extends Base
->page($page, $perPage) ->page($page, $perPage)
->select(); ->select();
foreach ($list as &$item) { foreach ($list as &$item) {
$item['ctime_text'] = $item['ctime'] ? date('Y-m-d H:i:s', $item['ctime']) : ''; $item['ctime_text'] = $item['ctime'] ? date('Y-m-d H:i:s', $item['ctime']) : '';
$item['fetch_fields'] = $this->resolveFetchFields($item['fetch_ids']); $item['fetch_fields'] = $this->resolveFetchFields($item['fetch_ids']);

View File

@@ -435,48 +435,50 @@ class PromotionService
} }
/** /**
* 兼容旧接口:同步为 task 内所有邮件执行准备(不建议在 HTTP 请求中直接调用, * 为单个 task 准备邮件:走消息队列异步执行。
* 大邮件量场景请走 dispatchPrepareEmails *
* 流程:
* 1. 推一条 PromotionPrepareTask 到 promotion 队列(调度层)。
* 2. 调度 Job 再拆成 N 条 PromotionPrepareEmail 到 promotion_email 队列LLM 层)。
*
* HTTP 调用方拿到的是"已入队"的结果,不再阻塞等待全部邮件准备完毕,
* 避免邮件量大时请求超时。task 的 state=5 由最后一封邮件在队列中触发
* tryFinalizeTask 时完成。
* *
* @param int $taskId * @param int $taskId
* @return array ['prepared' => int, 'failed' => int, 'error' => string|null] * @return array ['queued' => bool, 'task_id' => int, 'pending' => int, 'error' => string|null]
*/ */
public function prepareTask($taskId) public function prepareTask($taskId)
{ {
$taskId = intval($taskId);
$task = Db::name('promotion_task')->where('task_id', $taskId)->find(); $task = Db::name('promotion_task')->where('task_id', $taskId)->find();
if (!$task) { if (!$task) {
return ['prepared' => 0, 'failed' => 0, 'error' => 'task_not_found']; return ['queued' => false, 'task_id' => $taskId, 'pending' => 0, 'error' => 'task_not_found'];
} }
if ($task['state'] != 0) { if ($task['state'] != 0) {
return ['prepared' => 0, 'failed' => 0, 'error' => 'task_state_not_draft']; return ['queued' => false, 'task_id' => $taskId, 'pending' => 0, 'error' => 'task_state_not_draft'];
} }
$logIds = Db::name('promotion_email_log') $pending = Db::name('promotion_email_log')
->where('task_id', $taskId) ->where('task_id', $taskId)
->where('state', 0) ->where('state', 0)
->where('prepared_at', 0) ->where('prepared_at', 0)
->order('log_id asc') ->count();
->column('log_id');
$prepared = 0; // 没有待准备邮件时,直接把 task 置为已准备并返回
$failed = 0; if ($pending == 0) {
foreach ($logIds as $logId) { Db::name('promotion_task')->where('task_id', $taskId)->where('state', 0)->update([
$r = $this->prepareSingleEmail(intval($logId)); 'state' => 5,
if ($r['code'] === 0) { 'utime' => time(),
$prepared++; ]);
} else { $this->log("prepareTask task_id={$taskId} no_pending -> state=5");
$failed++; return ['queued' => false, 'task_id' => $taskId, 'pending' => 0, 'error' => null];
}
} }
// 兜底:即使 tryFinalizeTask 已触发,这里再保证 state 置为 5 $this->enqueuePrepareTask($taskId);
Db::name('promotion_task')->where('task_id', $taskId)->where('state', 0)->update([ $this->log("prepareTask task_id={$taskId} queued pending={$pending}");
'state' => 5,
'utime' => time(),
]);
$this->log("prepareTask task_id={$taskId} prepared={$prepared} failed={$failed}");
return ['prepared' => $prepared, 'failed' => $failed, 'error' => null]; return ['queued' => true, 'task_id' => $taskId, 'pending' => $pending, 'error' => null];
} }
/** /**
@@ -519,9 +521,9 @@ class PromotionService
$data = ['task_id' => intval($taskId)]; $data = ['task_id' => intval($taskId)];
if ($delay > 0) { if ($delay > 0) {
Queue::later($delay, $jobClass, $data, 'promotionSend'); Queue::later($delay, $jobClass, $data, 'promotionPrepareTask');
} else { } else {
Queue::push($jobClass, $data, 'promotionSend'); Queue::push($jobClass, $data, 'promotionPrepareTask');
} }
} }
@@ -537,9 +539,9 @@ class PromotionService
$data = ['log_id' => intval($logId)]; $data = ['log_id' => intval($logId)];
if ($delay > 0) { if ($delay > 0) {
Queue::later($delay, $jobClass, $data, 'promotion_email'); Queue::later($delay, $jobClass, $data, 'promotionPrepareEmail');
} else { } else {
Queue::push($jobClass, $data, 'promotion_email'); Queue::push($jobClass, $data, 'promotionPrepareEmail');
} }
} }