队列调整

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; use app\common\QueueJob;
class ArticleAiCreateContent 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内容生成 // 文章AI内容生成
public function fire(Job $job, $data) public function fire(Job $job, $data)
{ {
// 记录任务开始执行
$sLogPath = ROOT_PATH.'public/queue_log/ArticleAiCreateContent_'.date('Ymd').'.log'; $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 //获取文章ID
$iLogId = 0; $iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
try { //获取方法名
//实例化 $sClassName = get_class($this);
// 检查任务是否已处理(基于业务唯一标识)
$sRedisKey = $sClassName.'/'.$iArticleId;
$sRedisKey = md5($sRedisKey);
//判断Redis是否存在
$oQueueJob = new QueueJob; $oQueueJob = new QueueJob;
$sMsg = '文章AI内容生成成功'; $result = $oQueueJob->setRedisLabel(['redis_key' => $sRedisKey]);
$aJob = empty($job->getRawBody()) ? [] : json_decode($job->getRawBody(), true); if(empty($result)){
$job->delete();
file_put_contents($sLogPath,'-----------Queue job already:'.$sTime.'-----------');
return;
}
//任务数组
$aParam = [ $aParam = [
'job_id' => empty($aJob['id']) ? 'ArticleAiCreateContent'.rand(100, 999) : $aJob['id'], 'job_id' => $sRedisKey,
'job_class' => get_class($this), 'job_class' => $sClassName,
'status' => 0, 'status' => 0,
'create_time' => time(), 'create_time' => time(),
'params' => json_encode($data, JSON_UNESCAPED_UNICODE) 'params' => json_encode($data, JSON_UNESCAPED_UNICODE)
]; ];
$iLogId = $oQueueJob->addLog($aParam); //执行任务
// 步骤1上传素材图片 try {
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id']; // 任务逻辑
if (!empty($iArticleId)) { $sMsg = '文章AI内容生成成功';
//上传素材 $iLogId = $this->addLog($aParam);
//生成内容
$oAiarticle = new Aiarticle; $oAiarticle = new Aiarticle;
$aResult = json_decode($oAiarticle->create($data),true); $aResult = json_decode($oAiarticle->create($data),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status']; $iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
$sMsg = empty($aResult['msg']) ? '文章AI内容生成失败' : $aResult['msg']; $sMsg = empty($aResult['msg']) ? '文章AI内容生成失败' : $aResult['msg'];
}
$job->delete();
//更新任务状态 //更新任务状态
$aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg]; $aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam); $this->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);
$job->delete(); $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 { }finally {
gc_collect_cycles(); // 强制垃圾回收 gc_collect_cycles(); // 强制垃圾回收
} }

View File

@@ -5,47 +5,75 @@ use app\common\QueueJob;
use app\common\Reviewer; use app\common\Reviewer;
class RecommendReviewer class RecommendReviewer
{ {
// 最多重试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);
}
// 推荐审稿人任务 // 推荐审稿人任务
public function fire(Job $job, $data) public function fire(Job $job, $data)
{ {
// 记录任务开始执行
$sLogPath = ROOT_PATH.'public/queue_log/RecommendReviewer_'.date('Ymd').'.log'; $sLogPath = ROOT_PATH.'public/queue_log/RecommendReviewer_'.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 //获取文章ID
$iLogId = 0; $iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
try { //获取方法名
$sClassName = get_class($this);
//实例化 // 检查任务是否已处理(基于业务唯一标识)
$sRedisKey = $sClassName.'/'.$iArticleId;
$sRedisKey = md5($sRedisKey);
//判断Redis是否存在
$oQueueJob = new QueueJob; $oQueueJob = new QueueJob;
$sMsg = '获取推荐审稿人信息成功'; $result = $oQueueJob->setRedisLabel(['redis_key' => $sRedisKey]);
// 记录异常日志 if(empty($result)){
$aJob = empty($job->getRawBody()) ? [] : json_decode($job->getRawBody(), true); $job->delete();
file_put_contents($sLogPath,'-----------Queue job already:'.$sTime.'-----------');
return;
}
//任务数组
$aParam = [ $aParam = [
'job_id' => empty($aJob['id']) ? 'RecommendReviewer'.rand(100, 999) : $aJob['id'], 'job_id' => $sRedisKey,
'job_class' => get_class($this), 'job_class' => $sClassName,
'status' => 0, 'status' => 0,
'create_time' => time(), 'create_time' => time(),
'params' => json_encode($data, JSON_UNESCAPED_UNICODE) 'params' => json_encode($data, JSON_UNESCAPED_UNICODE)
]; ];
$iLogId = $oQueueJob->addLog($aParam); //执行任务
// 步骤1上传素材图片 try {
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id']; //添加任务日志
$sMsg = '推荐审稿人任务处理成功';
$iLogId = $this->addLog($aParam);
if (!empty($iArticleId)) {
$aParam = ['article_id' => $iArticleId,'page' => 1,'size' => empty($data['size']) ? 5 : $data['size']];
//获取推荐审稿人信息 //获取推荐审稿人信息
$aParam = ['article_id' => $iArticleId,'page' => 1,'size' => empty($data['size']) ? 5 : $data['size']];
$oReviewer = new Reviewer; $oReviewer = new Reviewer;
$aResult = json_decode($oReviewer->recommend($aParam),true); $aResult = json_decode($oReviewer->recommend($aParam),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status']; $iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
$sMsg = empty($aResult['msg']) ? '' : $aResult['msg']; $sMsg = empty($aResult['msg']) ? '' : $aResult['msg'];
//数量
$iCount = empty($aResult['data']['total']) ? 0 : $aResult['data']['total']; //处理数据
//推荐数量 $iCount = empty($aResult['data']['total']) ? 0 : $aResult['data']['total'];//数量
$iSize = empty($aResult['data']['size']) ? 0 : $aResult['data']['size']; $iSize = empty($aResult['data']['size']) ? 0 : $aResult['data']['size'];//推荐数量
//判断是否给期刊管理者发邮件【数据库的审稿数量小于推荐数量】 //判断是否给期刊管理者发邮件【数据库的审稿数量小于推荐数量】
if($iCount < $iSize){ if($iCount < $iSize){
$aSendEmailResult = json_decode($oReviewer->emailForEditor($aParam),true); $aSendEmailResult = json_decode($oReviewer->emailForEditor($aParam),true);
@@ -72,25 +100,30 @@ class RecommendReviewer
$sMsg .= empty($aResult['msg']) ? 'Reviewer data insertion failed' : $aResult['msg']; $sMsg .= empty($aResult['msg']) ? 'Reviewer data insertion failed' : $aResult['msg'];
} }
} }
}
$job->delete();
//更新任务状态 //更新任务状态
$aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg]; $aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam); $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);
$job->delete(); $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 { }finally {
gc_collect_cycles(); // 强制垃圾回收 gc_collect_cycles(); // 强制垃圾回收
} }

View File

@@ -5,57 +5,97 @@ use app\common\QueueJob;
use app\common\JournalArticle; use app\common\JournalArticle;
class RelatedArticle class RelatedArticle
{ {
// 最多重试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);
}
// 相关文章发送邮件任务 // 相关文章发送邮件任务
public function fire(Job $job, $data) public function fire(Job $job, $data)
{ {
// 记录任务开始执行
$sLogPath = ROOT_PATH.'public/queue_log/RelatedArticle_'.date('Ymd').'.log'; $sLogPath = ROOT_PATH.'public/queue_log/RelatedArticle_'.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 //文章ID
$iLogId = 0; $iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
try {
//实例化 //获取方法名
$sClassName = get_class($this);
// 检查任务是否已处理(基于业务唯一标识)
$sRedisKey = $sClassName.'/'.$iArticleId;
$sRedisKey = md5($sRedisKey);
//判断Redis是否存在
$oQueueJob = new QueueJob; $oQueueJob = new QueueJob;
$sMsg = '获取相关文章信息成功'; $result = $oQueueJob->setRedisLabel(['redis_key' => $sRedisKey]);
if(empty($result)){
$job->delete();
file_put_contents($sLogPath,'-----------Queue job already:'.$sTime.'-----------');
return;
}
$aJob = empty($job->getRawBody()) ? [] : json_decode($job->getRawBody(), true); //任务数组
$aParam = [ $aParam = [
'job_id' => empty($aJob['id']) ? 'RelatedArticle'.rand(100, 999) : $aJob['id'], 'job_id' => $sRedisKey,
'job_class' => get_class($this), 'job_class' => $sClassName,
'status' => 0, 'status' => 0,
'create_time' => time(), 'create_time' => time(),
'params' => json_encode($data, JSON_UNESCAPED_UNICODE) 'params' => empty($data) ? '暂无参数' : json_encode($data, JSON_UNESCAPED_UNICODE)
]; ];
$iLogId = $oQueueJob->addLog($aParam); //执行任务
// 步骤1上传素材图片 try {
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id']; //添加任务日志
if (!empty($iArticleId)) { $sMsg = '关联文章任务处理成功';
//上传素材 $iLogId = $this->addLog($aParam);
//查询文章所关联的文章
$oJournalArticle = new JournalArticle; $oJournalArticle = new JournalArticle;
$aResult = json_decode(JournalArticle::get($data),true); $aResult = json_decode(JournalArticle::get($data),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status']; $iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
$sMsg = empty($aResult['msg']) ? '获取相关文章信息失败' : $aResult['msg']; $sMsg = empty($aResult['msg']) ? '获取相关文章信息失败' : $aResult['msg'];
}
$job->delete();
//更新任务状态 //更新任务状态
$aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg]; $aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam); $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);
$job->delete(); $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 { }finally {
gc_collect_cycles(); // 强制垃圾回收 gc_collect_cycles(); // 强制垃圾回收
} }

View File

@@ -5,57 +5,95 @@ use app\common\QueueJob;
use app\common\Reviewer; use app\common\Reviewer;
class ReviewerScore class ReviewerScore
{ {
// 最多重试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);
}
// 审稿人评分 // 审稿人评分
public function fire(Job $job, $data) public function fire(Job $job, $data)
{ {
// 记录任务开始执行
$sLogPath = ROOT_PATH.'public/queue_log/ReviewerScore_'.date('Ymd').'.log'; $sLogPath = ROOT_PATH.'public/queue_log/ReviewerScore_'.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; $iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];//文章ID
try { $iReviewerId = empty($data['reviewer_id']) ? 0 : $data['reviewer_id'];//审稿人ID
//实例化 $iArtRevId = empty($data['art_rev_id']) ? 0 : $data['art_rev_id'];//主键ID
//获取方法名
$sClassName = get_class($this);
// 检查任务是否已处理(基于业务唯一标识)
$sRedisKey = $sClassName.'/'.$iArticleId.'/'.$iReviewerId.'/'.$iArtRevId;
$sRedisKey = md5($sRedisKey);
//判断Redis是否存在
$oQueueJob = new QueueJob; $oQueueJob = new QueueJob;
$sMsg = '给审稿人评分处理成功'; $result = $oQueueJob->setRedisLabel(['redis_key' => $sRedisKey]);
if(empty($result)){
$job->delete();
file_put_contents($sLogPath,'-----------Queue job already:'.$sTime.'-----------');
return;
}
$aJob = empty($job->getRawBody()) ? [] : json_decode($job->getRawBody(), true); //任务数组
$aParam = [ $aParam = [
'job_id' => empty($aJob['id']) ? 'ReviewerScore'.rand(100, 999) : $aJob['id'], 'job_id' => $sRedisKey,
'job_class' => get_class($this), 'job_class' => $sClassName,
'status' => 0, 'status' => 0,
'create_time' => time(), 'create_time' => time(),
'params' => json_encode($data, JSON_UNESCAPED_UNICODE) 'params' => json_encode($data, JSON_UNESCAPED_UNICODE)
]; ];
$iLogId = $oQueueJob->addLog($aParam); //执行任务
try {
//添加任务日志
$sMsg = '审稿人评分任务处理成功';
$iLogId = $this->addLog($aParam);
//审稿人评分
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];//文章ID
$iReviewerId = empty($data['reviewer_id']) ? 0 : $data['reviewer_id'];//审稿人ID
$iArtRevId = empty($data['art_rev_id']) ? 0 : $data['art_rev_id'];//主键ID
if (!empty($iArticleId) && !empty($iArtRevId) && !empty($iArtRevId)) {
$aParam = ['article_id' => $iArticleId,'reviewer_id' => $iReviewerId,'art_rev_id' => $iArtRevId]; $aParam = ['article_id' => $iArticleId,'reviewer_id' => $iReviewerId,'art_rev_id' => $iArtRevId];
$oReviewer = new Reviewer; $oReviewer = new Reviewer;
$aResult = json_decode($oReviewer->score($aParam),true); $aResult = json_decode($oReviewer->score($aParam),true);
$sMsg = empty($aResult['msg']) ? '给审稿人评分处理失败' : $aResult['msg']; $sMsg = empty($aResult['msg']) ? '给审稿人评分处理失败' : $aResult['msg'];
}
$job->delete();
//更新任务状态 //更新任务状态
$aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg]; $aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam); $oQueueJob->updateLog($aParam);
// 记录日志
file_put_contents($sLogPath,'-----------Queue job end---------'."\n\n\n",FILE_APPEND); //删除任务
$job->delete();
file_put_contents($sLogPath,'-----------Queue job end:'.$sTime.'-----------');
} catch (\Exception $e) { } catch (\Exception $e) {
//实例化 // 2. 记录失败日志
$oQueueJob = new QueueJob; $aParam['status'] = 2; // 标记状态为"失败"
//更新任务状态 $sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage(); // 错误信息
$sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage(); $aParam['error'] = $sMsg;
$aParam = ['log_id' => $iLogId,'status' => 2,'update_time' => time(),'error' => $sMsg]; $this->addLog($aParam); // 调用日志记录方法
$oQueueJob->updateLog($aParam); if ($job->attempts() > $this->tries) {
$job->delete(); //如果任务尝试次数超过最大重试次数
$job->delete(); // 删除任务,不再重试
} else {
// 3. 如果尝试次数未超过最大重试次数,释放任务回队列
$job->release(30); // 30秒后重新尝试执行任务
}
file_put_contents($sLogPath,'-----------Queue job error:'.$sMsg.'-----------'.$sTime);
}finally { }finally {
gc_collect_cycles(); // 强制垃圾回收 gc_collect_cycles(); // 强制垃圾回收

View File

@@ -5,60 +5,96 @@ use app\common\QueueJob;
use app\common\Reviewer; use app\common\Reviewer;
class RevisionReviewer class RevisionReviewer
{ {
// 最多重试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);
}
// 文章退修任务 // 文章退修任务
public function fire(Job $job, $data) public function fire(Job $job, $data)
{ {
// 记录任务开始执行
$sLogPath = ROOT_PATH.'public/queue_log/RevisionReviewer_'.date('Ymd').'.log'; $sLogPath = ROOT_PATH.'public/queue_log/RevisionReviewer_'.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; $iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];//文章ID
try { //获取方法名
//实例化 $sClassName = get_class($this);
// 检查任务是否已处理(基于业务唯一标识)
$sRedisKey = $sClassName.'/'.$iArticleId;
$sRedisKey = md5($sRedisKey);
//判断Redis是否存在
$oQueueJob = new QueueJob; $oQueueJob = new QueueJob;
$sMsg = '处理审稿人同意审稿但超时未审的数据成功'; $result = $oQueueJob->setRedisLabel(['redis_key' => $sRedisKey]);
if(empty($result)){
$job->delete();
file_put_contents($sLogPath,'-----------Queue job already:'.$sTime.'-----------');
return;
}
$aJob = empty($job->getRawBody()) ? [] : json_decode($job->getRawBody(), true); //任务数组
$aParam = [ $aParam = [
'job_id' => empty($aJob['id']) ? 'RevisionReviewer'.rand(100, 999) : $aJob['id'], 'job_id' => $sRedisKey,
'job_class' => get_class($this), 'job_class' => $sClassName,
'status' => 0, 'status' => 0,
'create_time' => time(), 'create_time' => time(),
'params' => json_encode($data, JSON_UNESCAPED_UNICODE) 'params' => json_encode($data, JSON_UNESCAPED_UNICODE)
]; ];
$iLogId = $oQueueJob->addLog($aParam); //执行任务
try {
//添加任务日志
$sMsg = '文章退修任务处理成功';
$iLogId = $this->addLog($aParam);
//获取符合条件的文章审稿人信息 //获取符合条件的文章审稿人信息
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
if (!empty($iArticleId)) {
$aParam = ['article_id' => $iArticleId]; $aParam = ['article_id' => $iArticleId];
$oReviewer = new Reviewer; $oReviewer = new Reviewer;
$aResult = json_decode($oReviewer->revisionForReviewer($aParam),true); $aResult = json_decode($oReviewer->revisionForReviewer($aParam),true);
$sMsg = empty($aResult['msg']) ? '审稿人同意审稿但超时未审的数据失败' : $aResult['msg']; $sMsg = empty($aResult['msg']) ? '审稿人同意审稿但超时未审的数据失败' : $aResult['msg'];
}
$job->delete();
//更新任务状态 //更新任务状态
$aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg]; $aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam); $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);
$job->delete(); $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 { }finally {
gc_collect_cycles(); // 强制垃圾回收 gc_collect_cycles(); // 强制垃圾回收
} }
} }
} }

View File

@@ -5,30 +5,35 @@ use app\common\QueueJob;
use app\common\JournalArticle; use app\common\JournalArticle;
class SendRelatedArticleEmail class SendRelatedArticleEmail
{ {
// 相关文章发送邮件任务
// 最多重试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);
}
// 关联文章任务
public function fire(Job $job, $data) public function fire(Job $job, $data)
{ {
// 记录任务开始执行
$sLogPath = ROOT_PATH.'public/queue_log/SendRelatedArticleEmail_'.date('Ymd').'.log'; $sLogPath = ROOT_PATH.'public/queue_log/SendRelatedArticleEmail_'.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 = '发送邮件成功';
$aJob = empty($job->getRawBody()) ? [] : json_decode($job->getRawBody(), true);
$aParam = [
'job_id' => empty($aJob['id']) ? 'SendRelatedArticleEmail'.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);
//文章ID //文章ID
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id']; $iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
@@ -52,13 +57,39 @@ class SendRelatedArticleEmail
$journal_id = empty($data['journal_id']) ? '' : $data['journal_id']; $journal_id = empty($data['journal_id']) ? '' : $data['journal_id'];
//期刊issn //期刊issn
$journal_issn = empty($data['journal_issn']) ? '' : $data['journal_issn']; $journal_issn = empty($data['journal_issn']) ? '' : $data['journal_issn'];
//发送邮件
if (!empty($iArticleId) && !empty($article_author_id) && !empty($related_article_id) && !empty($memail) && !empty($mpassword)) { //获取方法名
$sClassName = get_class($this);
// 检查任务是否已处理(基于业务唯一标识)
$sRedisKey = $sClassName.'/'.$iArticleId.'/'.$related_article_id.'/'.$article_author_id.'/'.$email;
$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' => empty($data) ? '暂无参数' : json_encode($data, JSON_UNESCAPED_UNICODE)
];
//执行任务
try {
//添加任务日志
$sMsg = '关联文章任务处理成功';
$iLogId = $this->addLog($aParam);
//查询是否发送过邮件 //查询是否发送过邮件
$oJournalArticle = new JournalArticle; $oJournalArticle = new JournalArticle;
$aLog = json_decode($oJournalArticle::getLog(['article_id' => $iArticleId,'article_author_id' => $article_author_id,'related_article_id' => $related_article_id,'is_success' => 1]),true); $aLog = json_decode($oJournalArticle::getLog(['article_id' => $iArticleId,'article_author_id' => $article_author_id,'related_article_id' => $related_article_id,'is_success' => 1]),true);
$sMsg = '邮件已发送:'.json_encode($aLog['data']); $sMsg = '邮件已发送';
if(empty($aLog['data'])){ if(empty($aLog['data'])){
$aResult = sendEmail($email,$title,$from_name,$content,$memail,$mpassword); $aResult = sendEmail($email,$title,$from_name,$content,$memail,$mpassword);
$iStatus = empty($aResult['status']) ? 1 : $aResult['status']; $iStatus = empty($aResult['status']) ? 1 : $aResult['status'];
@@ -74,25 +105,31 @@ class SendRelatedArticleEmail
//添加邮件发送日志 //添加邮件发送日志
$iId = JournalArticle::addLog($aEmailLog); $iId = JournalArticle::addLog($aEmailLog);
} }
}
$job->delete();
//更新任务状态 //更新任务状态
$aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg]; $aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam); $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);
$job->delete(); $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 { }finally {
gc_collect_cycles(); // 强制垃圾回收 gc_collect_cycles(); // 强制垃圾回收
} }

View File

@@ -5,30 +5,35 @@ use app\common\QueueJob;
use app\common\Reviewer; use app\common\Reviewer;
class SendReviewEmail class SendReviewEmail
{ {
// 最多重试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);
}
// 发送审稿邀请邮件任务 // 发送审稿邀请邮件任务
public function fire(Job $job, $data) public function fire(Job $job, $data)
{ {
// 记录任务开始执行
$sLogPath = ROOT_PATH.'public/queue_log/SendReviewEmail_'.date('Ymd').'.log'; $sLogPath = ROOT_PATH.'public/queue_log/SendReviewEmail_'.date('Ymd').'.log';
file_put_contents($sLogPath,'-----------Queue job started-----------'."\n",FILE_APPEND); $sTime = date('H:i:s');
file_put_contents($sLogPath,'-----------Queue job started:'.$sTime.'-----------');
//获取任务ID
$iLogId = 0;
try {
//实例化
$oQueueJob = new QueueJob;
$sMsg = '发送邮件成功';
$aJob = empty($job->getRawBody()) ? [] : json_decode($job->getRawBody(), true);
$aParam = [
'job_id' => empty($aJob['id']) ? 'SendReviewEmail'.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);
//文章ID //文章ID
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id']; $iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
//作者邮箱 //作者邮箱
@@ -49,9 +54,35 @@ class SendReviewEmail
$reviewer_id = empty($data['reviewer_id']) ? 0 : $data['reviewer_id']; $reviewer_id = empty($data['reviewer_id']) ? 0 : $data['reviewer_id'];
//邮件类型 //邮件类型
$type = empty($data['type']) ? 1 : $data['type']; $type = empty($data['type']) ? 1 : $data['type'];
//发送邮件
if (!empty($email) && !empty($memail) && !empty($mpassword)) {
//获取方法名
$sClassName = get_class($this);
// 检查任务是否已处理(基于业务唯一标识)
$sRedisKey = $sClassName.'/'.$iArticleId.'/'.$art_rev_id.'/'.$reviewer_id.'/'.$email;
$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' => empty($data) ? '暂无参数' : json_encode($data, JSON_UNESCAPED_UNICODE)
];
//执行任务
try {
//添加任务日志
$sMsg = '发送审稿邀请邮件任务处理成功';
$iLogId = $this->addLog($aParam);
//发送邮件
//查询是否发送过邮件 //查询是否发送过邮件
$oReviewer = new Reviewer; $oReviewer = new Reviewer;
if($type != 3){ if($type != 3){
@@ -73,24 +104,31 @@ class SendReviewEmail
//添加邮件发送日志 //添加邮件发送日志
$iId = $oReviewer->addLog($aEmailLog); $iId = $oReviewer->addLog($aEmailLog);
} }
}
$job->delete();
//更新任务状态 //更新任务状态
$aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg]; $aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam); $oQueueJob->updateLog($aParam);
// // 记录日志
file_put_contents($sLogPath,'-----------Queue job end---------'."\n\n\n",FILE_APPEND); //删除任务
$job->delete();
file_put_contents($sLogPath,'-----------Queue job end:'.$sTime.'-----------');
} catch (\Exception $e) { } catch (\Exception $e) {
//实例化 // 2. 记录失败日志
$oQueueJob = new QueueJob; $aParam['status'] = 2; // 标记状态为"失败"
//更新任务状态 $sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage(); // 错误信息
$sMsg = empty($e->getMessage()) ? '任务出错' : $e->getMessage(); $aParam['error'] = $sMsg;
$aParam = ['log_id' => $iLogId,'status' => 2,'update_time' => time(),'error' => $sMsg]; $this->addLog($aParam); // 调用日志记录方法
$oQueueJob->updateLog($aParam); if ($job->attempts() > $this->tries) {
$job->delete(); //如果任务尝试次数超过最大重试次数
$job->delete(); // 删除任务,不再重试
} else {
// 3. 如果尝试次数未超过最大重试次数,释放任务回队列
$job->release(30); // 30秒后重新尝试执行任务
}
file_put_contents($sLogPath,'-----------Queue job error:'.$sMsg.'-----------'.$sTime);
}finally { }finally {
gc_collect_cycles(); // 强制垃圾回收 gc_collect_cycles(); // 强制垃圾回收
} }

View File

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

View File

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

View File

@@ -5,59 +5,96 @@ use app\api\controller\Aiarticle;
use app\common\QueueJob; use app\common\QueueJob;
class WechatMaterial class WechatMaterial
{ {
// 最多重试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);
}
// 上传素材任务入口 // 上传素材任务入口
public function fire(Job $job, $data) public function fire(Job $job, $data)
{ {
// 记录任务开始执行
$sLogPath = ROOT_PATH.'public/queue_log/WechatMaterial_'.date('Ymd').'.log'; $sLogPath = ROOT_PATH.'public/queue_log/WechatMaterial_'.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 //文章ID
$iLogId = 0; $iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
try {
//实例化 //获取方法名
$sClassName = get_class($this);
// 检查任务是否已处理(基于业务唯一标识)
$sRedisKey = $sClassName.'/'.$iArticleId;
$sRedisKey = md5($sRedisKey);
//判断Redis是否存在
$oQueueJob = new QueueJob; $oQueueJob = new QueueJob;
$sMsg = '上传素材成功'; $result = $oQueueJob->setRedisLabel(['redis_key' => $sRedisKey]);
if(empty($result)){
$job->delete();
file_put_contents($sLogPath,'-----------Queue job already:'.$sTime.'-----------');
return;
}
$aJob = empty($job->getRawBody()) ? [] : json_decode($job->getRawBody(), true); //任务数组
$aParam = [ $aParam = [
'job_id' => empty($aJob['id']) ? 'WechatMaterial'.rand(100, 999) : $aJob['id'], 'job_id' => $sRedisKey,
'job_class' => get_class($this), 'job_class' => $sClassName,
'status' => 0, 'status' => 0,
'create_time' => time(), 'create_time' => time(),
'params' => json_encode($data, JSON_UNESCAPED_UNICODE) 'params' => empty($data) ? '暂无参数' : json_encode($data, JSON_UNESCAPED_UNICODE)
]; ];
$iLogId = $oQueueJob->addLog($aParam); //执行任务
// 步骤1上传素材图片 try {
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id']; //添加任务日志
if (!empty($iArticleId)) { $sMsg = '上传素材任务处理成功';
$iLogId = $this->addLog($aParam);
//上传素材 //上传素材
$oAiarticle = new Aiarticle; $oAiarticle = new Aiarticle;
$aResult = json_decode($oAiarticle->uploadMaterial($data),true); $aResult = json_decode($oAiarticle->uploadMaterial($data),true);
$iStatus = empty($aResult['status']) ? 0 : $aResult['status']; $iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
$sMsg = empty($aResult['msg']) ? '上传素材失败' : $aResult['msg']; $sMsg = empty($aResult['msg']) ? '上传素材失败' : $aResult['msg'];
}
$job->delete();
//更新任务状态 //更新任务状态
$aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg]; $aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg];
$oQueueJob->updateLog($aParam); $oQueueJob->updateLog($aParam);
// // 记录日志 //删除任务
file_put_contents($sLogPath,'-----------Queue job end-----------' . json_encode($data)."\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);
$job->delete(); $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 { }finally {
gc_collect_cycles(); // 强制垃圾回收 gc_collect_cycles(); // 强制垃圾回收
} }

View File

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