队列任务调整
This commit is contained in:
@@ -1,104 +1,129 @@
|
||||
<?php
|
||||
namespace app\api\job;
|
||||
|
||||
use think\queue\Job;
|
||||
use app\common\QueueJob;
|
||||
use app\common\QueueRedis;
|
||||
use app\common\JournalArticle;
|
||||
class RelatedArticle
|
||||
{
|
||||
|
||||
private $logPath;
|
||||
private $oQueueJob;
|
||||
private $QueueRedis;
|
||||
private $maxRetries = 2;
|
||||
private $logBuffer = [];
|
||||
private $lastLogTime = 0;
|
||||
private $logMaxSize = 1048576; // 1MB (1*1024*1024) - 修正注释与值匹配
|
||||
private $lockExpire = 1800;
|
||||
private $completedExprie = 3600;
|
||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
||||
|
||||
// 最多重试1次
|
||||
public $tries = 1;
|
||||
|
||||
// 任务日志添加
|
||||
public function addLog($aParam = [])
|
||||
public function __construct()
|
||||
{
|
||||
//实例化
|
||||
$oQueueJob = new QueueJob;
|
||||
$iLogId = $oQueueJob->addLog($aParam);
|
||||
return $iLogId;
|
||||
}
|
||||
|
||||
// 任务日志修改
|
||||
public function updateLog($aParam = [])
|
||||
{
|
||||
//实例化
|
||||
$oQueueJob = new QueueJob;
|
||||
return $oQueueJob->updateLog($aParam);
|
||||
$this->logPath = ROOT_PATH . 'public/queue_log/RelatedArticle_' . date('Ymd') . '.log';
|
||||
$this->oQueueJob = new QueueJob;
|
||||
$this->QueueRedis = QueueRedis::getInstance();
|
||||
$this->lastLogTime = time();
|
||||
// 确保日志目录存在
|
||||
$this->oQueueJob->ensureLogDirExists($this->logPath);
|
||||
}
|
||||
|
||||
// 相关文章发送邮件任务
|
||||
public function fire(Job $job, $data)
|
||||
{
|
||||
$startTime = microtime(true);
|
||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||
|
||||
$sLogPath = ROOT_PATH.'public/queue_log/RelatedArticle_'.date('Ymd').'.log';
|
||||
$sTime = date('H:i:s');
|
||||
file_put_contents($sLogPath,'-----------Queue job started:'.$sTime.'-----------');
|
||||
|
||||
//文章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($result != 1){
|
||||
$job->delete();
|
||||
file_put_contents($sLogPath,'-----------Queue job already:'.$result."===".$sRedisKey.'==='.$iArticleId."===".$sTime.'-----------');
|
||||
// 检查Redis连接状态
|
||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
||||
$job->release(10);
|
||||
$this->oQueueJob->flushLog();
|
||||
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);
|
||||
// 获取文章ID
|
||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||
if (empty($iArticleId)) {
|
||||
$this->oQueueJob->log("无效的article_id,删除任务");
|
||||
$job->delete();
|
||||
return;
|
||||
}
|
||||
|
||||
$sClassName = get_class($this);
|
||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
||||
$sRedisValue = uniqid() . '_' . getmypid();
|
||||
$lockExpire = $this->lockExpire;
|
||||
|
||||
$isLocked = $this->QueueRedis->startJob($sRedisKey, $sRedisValue, $lockExpire);
|
||||
if (!$isLocked) {
|
||||
$jobStatus = $this->QueueRedis->getJobStatus($sRedisKey);
|
||||
if (in_array($jobStatus, ['completed', 'failed'])) {
|
||||
$this->oQueueJob->log("任务已完成或失败,删除任务 | 状态: {$jobStatus}");
|
||||
$job->delete();
|
||||
} else {
|
||||
$attempts = $job->attempts();
|
||||
if ($attempts >= $this->maxRetries) {
|
||||
$this->oQueueJob->log("超过最大重试次数,停止重试");
|
||||
$job->delete();
|
||||
} else {
|
||||
$lockTtl = $this->QueueRedis->getLockTtl($sRedisKey);
|
||||
$delay = $lockTtl > 0 ? $lockTtl + 5 : 30;
|
||||
$this->oQueueJob->log("锁竞争,{$delay}秒后重试({$attempts}/{$this->maxRetries})");
|
||||
$job->release($delay);
|
||||
}
|
||||
}
|
||||
$this->oQueueJob->flushLog();
|
||||
return;
|
||||
}
|
||||
|
||||
$aParam = [
|
||||
'job_id' => $sRedisKey,
|
||||
'job_class' => $sClassName,
|
||||
'status' => 0,
|
||||
'create_time' => time(),
|
||||
'params' => json_encode($data, self::JSON_OPTIONS)
|
||||
];
|
||||
|
||||
$iLogId = $this->oQueueJob->addLog($aParam);
|
||||
if (!$iLogId) {
|
||||
$this->oQueueJob->log("日志创建失败,释放锁并删除任务:".json_encode($data, self::JSON_OPTIONS));
|
||||
$this->QueueRedis->releaseRedisLock($sRedisKey, $sRedisValue);
|
||||
$job->delete();
|
||||
$this->oQueueJob->flushLog();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
//查询文章所关联的文章
|
||||
$oJournalArticle = new JournalArticle;
|
||||
$aResult = json_decode(JournalArticle::get($data),true);
|
||||
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
|
||||
$sMsg = empty($aResult['msg']) ? '获取相关文章信息失败' : $aResult['msg'];
|
||||
|
||||
//更新任务状态
|
||||
$aParam = ['log_id' => $iLogId,'status' => 1,'update_time' => time(),'error' => $sMsg];
|
||||
$oQueueJob->updateLog($aParam);
|
||||
|
||||
//删除任务
|
||||
//更新日志
|
||||
$this->oQueueJob->updateLog([
|
||||
'log_id' => $iLogId,
|
||||
'status' => 1,
|
||||
'update_time' => time(),
|
||||
'error' => $sMsg
|
||||
]);
|
||||
|
||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie);
|
||||
$job->delete();
|
||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$iLogId}");
|
||||
|
||||
file_put_contents($sLogPath,'-----------Queue job end:'.$sTime.'-----------');
|
||||
|
||||
} catch (\RuntimeException $e) {
|
||||
$this->oQueueJob->handleRetryableException($e, $iLogId, $sRedisKey, $job);
|
||||
} catch (\LogicException $e) {
|
||||
$this->oQueueJob->handleNonRetryableException($e, $iLogId, $sRedisKey, $job);
|
||||
} 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(); // 强制垃圾回收
|
||||
$this->oQueueJob->handleRetryableException($e, $iLogId, $sRedisKey, $job);
|
||||
} finally {
|
||||
$executionTime = microtime(true) - $startTime;
|
||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
||||
$this->oQueueJob->flushLog();
|
||||
gc_collect_cycles();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user