队列调整

This commit is contained in:
chengxl
2025-07-04 15:05:26 +08:00
parent f9a5f22984
commit 49ed27c6b2
11 changed files with 931 additions and 516 deletions

View File

@@ -5,55 +5,93 @@ use app\api\controller\Aiarticle;
use app\common\QueueJob;
class ArticleAiCreateContent
{
// 最多重试1次
public $tries = 1;
// 任务日志添加
public function addLog($aParam = [])
{
//实例化
$oQueueJob = new QueueJob;
$iLogId = $oQueueJob->addLog($aParam);
return $iLogId;
}
// 任务日志修改
public function updateLog($aParam = [])
{
//实例化
$oQueueJob = new QueueJob;
return $oQueueJob->updateLog($aParam);
}
// 文章AI内容生成
public function fire(Job $job, $data)
{
// 记录任务开始执行
$sLogPath = ROOT_PATH.'public/queue_log/ArticleAiCreateContent_'.date('Ymd').'.log';
file_put_contents($sLogPath,'-----------Queue job started-----------' . json_encode($data)."\n",FILE_APPEND);
$sTime = date('H:i:s');
file_put_contents($sLogPath,'-----------Queue job started:'.$sTime.'-----------');
//获取任务ID
$iLogId = 0;
try {
//实例化
$oQueueJob = new QueueJob;
$sMsg = '文章AI内容生成成功';
$aJob = empty($job->getRawBody()) ? [] : json_decode($job->getRawBody(), true);
$aParam = [
'job_id' => empty($aJob['id']) ? 'ArticleAiCreateContent'.rand(100, 999) : $aJob['id'],
'job_class' => get_class($this),
'status' => 0,
'create_time' => time(),
'params' => json_encode($data, JSON_UNESCAPED_UNICODE)
];
$iLogId = $oQueueJob->addLog($aParam);
// 步骤1上传素材图片
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
if (!empty($iArticleId)) {
//上传素材
$oAiarticle = new Aiarticle;
$aResult = json_decode($oAiarticle->create($data),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
$sMsg = empty($aResult['msg']) ? '文章AI内容生成失败' : $aResult['msg'];
}
//获取文章ID
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
//获取方法名
$sClassName = get_class($this);
// 检查任务是否已处理(基于业务唯一标识)
$sRedisKey = $sClassName.'/'.$iArticleId;
$sRedisKey = md5($sRedisKey);
//判断Redis是否存在
$oQueueJob = new QueueJob;
$result = $oQueueJob->setRedisLabel(['redis_key' => $sRedisKey]);
if(empty($result)){
$job->delete();
file_put_contents($sLogPath,'-----------Queue job already:'.$sTime.'-----------');
return;
}
//任务数组
$aParam = [
'job_id' => $sRedisKey,
'job_class' => $sClassName,
'status' => 0,
'create_time' => time(),
'params' => json_encode($data, JSON_UNESCAPED_UNICODE)
];
//执行任务
try {
// 任务逻辑
$sMsg = '文章AI内容生成成功';
$iLogId = $this->addLog($aParam);
//生成内容
$oAiarticle = new Aiarticle;
$aResult = json_decode($oAiarticle->create($data),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
$sMsg = empty($aResult['msg']) ? '文章AI内容生成失败' : $aResult['msg'];
//更新任务状态
$aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam);
// // 记录日志
file_put_contents($sLogPath,'-----------Queue job end---------'."\n\n\n",FILE_APPEND);
} catch (\Exception $e) {
//实例化
$oQueueJob = new QueueJob;
//更新任务状态
$sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage();
$aParam = ['log_id' => $iLogId,'status' => 2,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam);
$this->updateLog($aParam);
//删除任务
$job->delete();
file_put_contents($sLogPath,'-----------Queue job end:'.$sTime.'-----------');
} catch (\Exception $e) {
// 2. 记录失败日志
$aParam['status'] = 2; // 标记状态为"失败"
$sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage(); // 错误信息
$aParam['error'] = $sMsg;
$this->addLog($aParam); // 调用日志记录方法
if ($job->attempts() > $this->tries) {
//如果任务尝试次数超过最大重试次数
$job->delete(); // 删除任务,不再重试
} else {
// 3. 如果尝试次数未超过最大重试次数,释放任务回队列
$job->release(30); // 30秒后重新尝试执行任务
}
file_put_contents($sLogPath,'-----------Queue job error:'.$sMsg.'-----------'.$sTime);
}finally {
gc_collect_cycles(); // 强制垃圾回收
}