修改自动推广的相关任务
This commit is contained in:
@@ -1704,8 +1704,13 @@ class EmailClient extends Base
|
||||
}
|
||||
|
||||
/**
|
||||
* 为单个任务预生成邮件(可手动或测试用)
|
||||
* 为单个任务预生成邮件:异步入队
|
||||
* Params: task_id
|
||||
*
|
||||
* 返回:
|
||||
* queued 是否已入队(false 表示当前没有待准备邮件)
|
||||
* task_id 任务ID
|
||||
* pending 需要准备的邮件数(大约值;实际以队列执行为准)
|
||||
*/
|
||||
public function prepareTask()
|
||||
{
|
||||
|
||||
@@ -53,6 +53,8 @@ class PromotionFactory extends Base
|
||||
->page($page, $perPage)
|
||||
->select();
|
||||
|
||||
|
||||
|
||||
foreach ($list as &$item) {
|
||||
$item['ctime_text'] = $item['ctime'] ? date('Y-m-d H:i:s', $item['ctime']) : '';
|
||||
$item['fetch_fields'] = $this->resolveFetchFields($item['fetch_ids']);
|
||||
|
||||
@@ -435,48 +435,50 @@ class PromotionService
|
||||
}
|
||||
|
||||
/**
|
||||
* 兼容旧接口:同步为 task 内所有邮件执行准备(不建议在 HTTP 请求中直接调用,
|
||||
* 大邮件量场景请走 dispatchPrepareEmails)。
|
||||
* 为单个 task 准备邮件:走消息队列异步执行。
|
||||
*
|
||||
* 流程:
|
||||
* 1. 推一条 PromotionPrepareTask 到 promotion 队列(调度层)。
|
||||
* 2. 调度 Job 再拆成 N 条 PromotionPrepareEmail 到 promotion_email 队列(LLM 层)。
|
||||
*
|
||||
* HTTP 调用方拿到的是"已入队"的结果,不再阻塞等待全部邮件准备完毕,
|
||||
* 避免邮件量大时请求超时。task 的 state=5 由最后一封邮件在队列中触发
|
||||
* tryFinalizeTask 时完成。
|
||||
*
|
||||
* @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)
|
||||
{
|
||||
$taskId = intval($taskId);
|
||||
$task = Db::name('promotion_task')->where('task_id', $taskId)->find();
|
||||
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) {
|
||||
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('state', 0)
|
||||
->where('prepared_at', 0)
|
||||
->order('log_id asc')
|
||||
->column('log_id');
|
||||
->count();
|
||||
|
||||
$prepared = 0;
|
||||
$failed = 0;
|
||||
foreach ($logIds as $logId) {
|
||||
$r = $this->prepareSingleEmail(intval($logId));
|
||||
if ($r['code'] === 0) {
|
||||
$prepared++;
|
||||
} else {
|
||||
$failed++;
|
||||
}
|
||||
// 没有待准备邮件时,直接把 task 置为已准备并返回
|
||||
if ($pending == 0) {
|
||||
Db::name('promotion_task')->where('task_id', $taskId)->where('state', 0)->update([
|
||||
'state' => 5,
|
||||
'utime' => time(),
|
||||
]);
|
||||
$this->log("prepareTask task_id={$taskId} no_pending -> state=5");
|
||||
return ['queued' => false, 'task_id' => $taskId, 'pending' => 0, 'error' => null];
|
||||
}
|
||||
|
||||
// 兜底:即使 tryFinalizeTask 已触发,这里再保证 state 置为 5
|
||||
Db::name('promotion_task')->where('task_id', $taskId)->where('state', 0)->update([
|
||||
'state' => 5,
|
||||
'utime' => time(),
|
||||
]);
|
||||
$this->log("prepareTask task_id={$taskId} prepared={$prepared} failed={$failed}");
|
||||
$this->enqueuePrepareTask($taskId);
|
||||
$this->log("prepareTask task_id={$taskId} queued pending={$pending}");
|
||||
|
||||
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)];
|
||||
|
||||
if ($delay > 0) {
|
||||
Queue::later($delay, $jobClass, $data, 'promotionSend');
|
||||
Queue::later($delay, $jobClass, $data, 'promotionPrepareTask');
|
||||
} else {
|
||||
Queue::push($jobClass, $data, 'promotionSend');
|
||||
Queue::push($jobClass, $data, 'promotionPrepareTask');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -537,9 +539,9 @@ class PromotionService
|
||||
$data = ['log_id' => intval($logId)];
|
||||
|
||||
if ($delay > 0) {
|
||||
Queue::later($delay, $jobClass, $data, 'promotion_email');
|
||||
Queue::later($delay, $jobClass, $data, 'promotionPrepareEmail');
|
||||
} else {
|
||||
Queue::push($jobClass, $data, 'promotion_email');
|
||||
Queue::push($jobClass, $data, 'promotionPrepareEmail');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user