job任务修改
This commit is contained in:
@@ -9,10 +9,7 @@ class ArticleAiCreateContent
|
|||||||
{
|
{
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
private $completedExprie = 3600;
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
@@ -22,22 +19,16 @@ class ArticleAiCreateContent
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
// 检查数据库连接
|
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查Redis连接状态
|
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取文章ID
|
// 获取文章ID
|
||||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||||
@@ -46,42 +37,29 @@ class ArticleAiCreateContent
|
|||||||
$job->delete();
|
$job->delete();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
|
||||||
|
// 生成Redis键并尝试获取锁
|
||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
|
}
|
||||||
|
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
|
||||||
//生成内容
|
//生成内容
|
||||||
$oAiarticle = new Aiarticle;
|
$oAiarticle = new Aiarticle;
|
||||||
$aResult = json_decode($oAiarticle->create($data),true);
|
$response = $oAiarticle->create($data);
|
||||||
$sMsg = empty($aResult['msg']) ? '内容生成失败' : $aResult['msg'];
|
// 验证API响应
|
||||||
|
if (empty($response)) {
|
||||||
|
throw new RuntimeException("OpenAI API返回空结果");
|
||||||
|
}
|
||||||
|
// 检查JSON解析错误
|
||||||
|
$aResult = json_decode($response, true);
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
throw new RuntimeException("解析OpenAI响应失败: " . json_last_error_msg() . " | 原始响应: {$response}");
|
||||||
|
}
|
||||||
|
$sMsg = empty($aResult['msg']) ? 'success' : $aResult['msg'];
|
||||||
//更新完成标识
|
//更新完成标识
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie,$sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie,$sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
@@ -94,9 +72,7 @@ class ArticleAiCreateContent
|
|||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,11 +9,7 @@ class RecommendArticleField
|
|||||||
{
|
{
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
private $completedExprie = 3600; // 完成状态过期时间
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->oQueueJob = new QueueJob;
|
$this->oQueueJob = new QueueJob;
|
||||||
@@ -22,23 +18,20 @@ class RecommendArticleField
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
|
|
||||||
// 检查数据库连接
|
try {
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查Redis连接状态
|
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 验证任务数据完整性
|
||||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||||
if (empty($iArticleId)) {
|
if (empty($iArticleId)) {
|
||||||
$this->oQueueJob->log("无效的article_id,删除任务");
|
$this->oQueueJob->log("无效的article_id,删除任务");
|
||||||
@@ -46,55 +39,41 @@ class RecommendArticleField
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成Redis键并尝试获取锁
|
||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
|
}
|
||||||
|
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
|
||||||
$oArticle = new Article;
|
$oArticle = new Article;
|
||||||
$aResult = json_decode($oArticle->getAiField($data), true);
|
$response = $oArticle->getAiField($data);
|
||||||
$sMsg = empty($aResult['msg']) ? '内容生成成功' : $aResult['msg'];
|
// 验证API响应
|
||||||
|
if (empty($response)) {
|
||||||
|
throw new RuntimeException("OpenAI API返回空结果");
|
||||||
|
}
|
||||||
|
// 检查JSON解析错误
|
||||||
|
$aResult = json_decode($response, true);
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
throw new RuntimeException("解析OpenAI响应失败: " . json_last_error_msg() . " | 原始响应: {$response}");
|
||||||
|
}
|
||||||
|
$sMsg = empty($aResult['msg']) ? 'success' : $aResult['msg'];
|
||||||
|
|
||||||
// 更新完成标识
|
// 更新完成标识
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey}|执行日志:{$sMsg}");
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
||||||
|
|
||||||
} catch (\RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\LogicException $e) {
|
} catch (LogicException $e) {
|
||||||
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,14 +7,9 @@ use app\common\QueueRedis;
|
|||||||
use app\common\Reviewer;
|
use app\common\Reviewer;
|
||||||
class RecommendReviewer
|
class RecommendReviewer
|
||||||
{
|
{
|
||||||
private $logPath;
|
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
private $completedExprie = 3600; // 完成状态过期时间
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->oQueueJob = new QueueJob;
|
$this->oQueueJob = new QueueJob;
|
||||||
@@ -23,24 +18,20 @@ class RecommendReviewer
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
|
|
||||||
// 检查数据库连接
|
try {
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查Redis连接状态
|
// 验证任务数据完整性
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取文章ID
|
|
||||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||||
if (empty($iArticleId)) {
|
if (empty($iArticleId)) {
|
||||||
$this->oQueueJob->log("无效的article_id,删除任务");
|
$this->oQueueJob->log("无效的article_id,删除任务");
|
||||||
@@ -48,43 +39,28 @@ class RecommendReviewer
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成Redis键并尝试获取锁
|
||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// 执行核心任务
|
||||||
|
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
|
||||||
//获取推荐审稿人信息
|
//获取推荐审稿人信息
|
||||||
$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);
|
$response = $oReviewer->recommend($aParam);
|
||||||
|
// 验证API响应
|
||||||
|
if (empty($response)) {
|
||||||
|
throw new RuntimeException("OpenAI API返回空结果");
|
||||||
|
}
|
||||||
|
// 检查JSON解析错误
|
||||||
|
$aResult = json_decode($response, true);
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
throw new RuntimeException("解析OpenAI响应失败: " . json_last_error_msg() . " | 原始响应: {$response}");
|
||||||
|
}
|
||||||
$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'];
|
||||||
//处理数据
|
//处理数据
|
||||||
@@ -116,21 +92,20 @@ class RecommendReviewer
|
|||||||
$sMsg .= empty($aResult['msg']) ? 'Reviewer data insertion failed' : $aResult['msg'];
|
$sMsg .= empty($aResult['msg']) ? 'Reviewer data insertion failed' : $aResult['msg'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新完成标识
|
// 更新完成标识
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
||||||
|
|
||||||
} catch (\RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\LogicException $e) {
|
} catch (LogicException $e) {
|
||||||
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,14 +7,9 @@ use app\common\QueueRedis;
|
|||||||
use app\common\JournalArticle;
|
use app\common\JournalArticle;
|
||||||
class RelatedArticle
|
class RelatedArticle
|
||||||
{
|
{
|
||||||
private $logPath;
|
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
private $completedExprie = 3600; // 完成状态过期时间
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->oQueueJob = new QueueJob;
|
$this->oQueueJob = new QueueJob;
|
||||||
@@ -23,24 +18,20 @@ class RelatedArticle
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
|
|
||||||
// 检查数据库连接
|
try {
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查Redis连接状态
|
// 验证任务数据完整性
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取文章ID
|
|
||||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||||
if (empty($iArticleId)) {
|
if (empty($iArticleId)) {
|
||||||
$this->oQueueJob->log("无效的article_id,删除任务");
|
$this->oQueueJob->log("无效的article_id,删除任务");
|
||||||
@@ -48,60 +39,41 @@ class RelatedArticle
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成Redis键并尝试获取锁
|
||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
|
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//查询文章所关联的文章
|
// 执行核心任务
|
||||||
$oJournalArticle = new JournalArticle;
|
$oJournalArticle = new JournalArticle;
|
||||||
$aResult = json_decode(JournalArticle::get($data),true);
|
$response = JournalArticle::get($data);
|
||||||
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
|
// 验证API响应
|
||||||
|
if (empty($response)) {
|
||||||
|
throw new RuntimeException("OpenAI API返回空结果");
|
||||||
|
}
|
||||||
|
// 检查JSON解析错误
|
||||||
|
$aResult = json_decode($response, true);
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
throw new RuntimeException("解析OpenAI响应失败: " . json_last_error_msg() . " | 原始响应: {$response}");
|
||||||
|
}
|
||||||
$sMsg = empty($aResult['msg']) ? '获取相关文章信息失败' : $aResult['msg'];
|
$sMsg = empty($aResult['msg']) ? '获取相关文章信息失败' : $aResult['msg'];
|
||||||
|
|
||||||
//更新日志
|
// 更新完成标识
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
||||||
|
|
||||||
} catch (\RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\LogicException $e) {
|
} catch (LogicException $e) {
|
||||||
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,14 +7,9 @@ use app\common\QueueRedis;
|
|||||||
use app\common\Reviewer;
|
use app\common\Reviewer;
|
||||||
class ReviewerScore
|
class ReviewerScore
|
||||||
{
|
{
|
||||||
private $logPath;
|
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
private $completedExprie = 3600; // 完成状态过期时间
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->oQueueJob = new QueueJob;
|
$this->oQueueJob = new QueueJob;
|
||||||
@@ -23,22 +18,18 @@ class ReviewerScore
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
|
|
||||||
// 检查数据库连接
|
try {
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查Redis连接状态
|
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取文章ID
|
// 获取文章ID
|
||||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||||
@@ -51,59 +42,43 @@ class ReviewerScore
|
|||||||
$job->delete();
|
$job->delete();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 生成唯一任务标识
|
|
||||||
|
// 生成Redis键并尝试获取锁
|
||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}:{$iReviewerId}:{$iArtRevId}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}:{$iReviewerId}:{$iArtRevId}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 执行核心任务
|
||||||
$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);
|
$response = $oReviewer->score($aParam);
|
||||||
|
// 验证API响应
|
||||||
|
if (empty($response)) {
|
||||||
|
throw new RuntimeException("OpenAI API返回空结果");
|
||||||
|
}
|
||||||
|
// 检查JSON解析错误
|
||||||
|
$aResult = json_decode($response, true);
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
throw new RuntimeException("解析OpenAI响应失败: " . json_last_error_msg() . " | 原始响应: {$response}");
|
||||||
|
}
|
||||||
$sMsg = empty($aResult['msg']) ? '给审稿人评分处理失败' : $aResult['msg'];
|
$sMsg = empty($aResult['msg']) ? '给审稿人评分处理失败' : $aResult['msg'];
|
||||||
|
|
||||||
|
// 更新完成标识
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
||||||
|
|
||||||
} catch (\RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\LogicException $e) {
|
} catch (LogicException $e) {
|
||||||
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,14 +7,9 @@ use app\common\QueueRedis;
|
|||||||
use app\common\Reviewer;
|
use app\common\Reviewer;
|
||||||
class RevisionReviewer
|
class RevisionReviewer
|
||||||
{
|
{
|
||||||
private $logPath;
|
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
private $completedExprie = 3600; // 完成状态过期时间
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->oQueueJob = new QueueJob;
|
$this->oQueueJob = new QueueJob;
|
||||||
@@ -23,84 +18,64 @@ class RevisionReviewer
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
|
|
||||||
// 检查数据库连接
|
try {
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查Redis连接状态
|
// 验证任务数据完整性
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取文章ID
|
|
||||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||||
if (empty($iArticleId)) {
|
if (empty($iArticleId)) {
|
||||||
$this->oQueueJob->log("无效的article_id,删除任务");
|
$this->oQueueJob->log("无效的article_id,删除任务");
|
||||||
$job->delete();
|
$job->delete();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 生成唯一任务标识
|
|
||||||
|
// 生成Redis键并尝试获取锁
|
||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
|
}
|
||||||
|
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
|
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
|
||||||
//获取符合条件的文章审稿人信息
|
//获取符合条件的文章审稿人信息
|
||||||
$aParam = ['article_id' => $iArticleId];
|
$aParam = ['article_id' => $iArticleId];
|
||||||
$oReviewer = new Reviewer;
|
$oReviewer = new Reviewer;
|
||||||
$aResult = json_decode($oReviewer->revisionForReviewer($aParam),true);
|
$response = $oReviewer->revisionForReviewer($aParam);
|
||||||
|
// 验证API响应
|
||||||
|
if (empty($response)) {
|
||||||
|
throw new RuntimeException("OpenAI API返回空结果");
|
||||||
|
}
|
||||||
|
// 检查JSON解析错误
|
||||||
|
$aResult = json_decode($response, true);
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
throw new RuntimeException("解析OpenAI响应失败: " . json_last_error_msg() . " | 原始响应: {$response}");
|
||||||
|
}
|
||||||
$sMsg = empty($aResult['msg']) ? '审稿人同意审稿但超时未审的数据失败' : $aResult['msg'];
|
$sMsg = empty($aResult['msg']) ? '审稿人同意审稿但超时未审的数据失败' : $aResult['msg'];
|
||||||
|
|
||||||
//更新日志
|
// 更新完成标识
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
||||||
|
|
||||||
} catch (\RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\LogicException $e) {
|
} catch (LogicException $e) {
|
||||||
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,14 +7,9 @@ use app\common\QueueRedis;
|
|||||||
use app\common\JournalArticle;
|
use app\common\JournalArticle;
|
||||||
class SendRelatedArticleEmail
|
class SendRelatedArticleEmail
|
||||||
{
|
{
|
||||||
private $logPath;
|
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
private $completedExprie = 3600; // 完成状态过期时间
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->oQueueJob = new QueueJob;
|
$this->oQueueJob = new QueueJob;
|
||||||
@@ -23,22 +18,18 @@ class SendRelatedArticleEmail
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
|
|
||||||
// 检查数据库连接
|
try {
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查Redis连接状态
|
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取文章ID
|
// 获取文章ID
|
||||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||||
@@ -67,39 +58,17 @@ class SendRelatedArticleEmail
|
|||||||
$job->delete();
|
$job->delete();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 生成唯一任务标识
|
|
||||||
|
// 生成Redis键并尝试获取锁
|
||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}:{$related_article_id}:{$article_author_id}:{$email}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}:{$related_article_id}:{$article_author_id}:{$email}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
|
}
|
||||||
|
|
||||||
$isLocked = $this->QueueRedis->startJob($sRedisKey, $sRedisValue, $lockExpire);
|
// 执行核心任务
|
||||||
if (!$isLocked) {
|
$oArticle = new Article;
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
|
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
|
||||||
//查询是否发送过邮件
|
//查询是否发送过邮件
|
||||||
$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);
|
||||||
@@ -120,21 +89,19 @@ class SendRelatedArticleEmail
|
|||||||
$iId = JournalArticle::addLog($aEmailLog);
|
$iId = JournalArticle::addLog($aEmailLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
//更新日志
|
// 更新完成标识
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
||||||
|
|
||||||
} catch (\RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\LogicException $e) {
|
} catch (LogicException $e) {
|
||||||
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,14 +7,9 @@ use app\common\QueueRedis;
|
|||||||
use app\common\Reviewer;
|
use app\common\Reviewer;
|
||||||
class SendReviewEmail
|
class SendReviewEmail
|
||||||
{
|
{
|
||||||
private $logPath;
|
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
private $completedExprie = 3600; // 完成状态过期时间
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->oQueueJob = new QueueJob;
|
$this->oQueueJob = new QueueJob;
|
||||||
@@ -23,23 +18,20 @@ class SendReviewEmail
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
|
|
||||||
// 检查数据库连接
|
try {
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查Redis连接状态
|
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 验证任务数据完整性
|
||||||
// 获取文章ID
|
// 获取文章ID
|
||||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||||
//作者邮箱
|
//作者邮箱
|
||||||
@@ -70,35 +62,11 @@ class SendReviewEmail
|
|||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}:{$art_rev_id}:{$reviewer_id}:{$email}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}:{$art_rev_id}:{$reviewer_id}:{$email}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
|
}
|
||||||
|
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
|
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
|
||||||
//查询是否发送过邮件
|
//查询是否发送过邮件
|
||||||
$oReviewer = new Reviewer;
|
$oReviewer = new Reviewer;
|
||||||
if($type != 3){
|
if($type != 3){
|
||||||
@@ -120,21 +88,20 @@ class SendReviewEmail
|
|||||||
//添加邮件发送日志
|
//添加邮件发送日志
|
||||||
$iId = $oReviewer->addLog($aEmailLog);
|
$iId = $oReviewer->addLog($aEmailLog);
|
||||||
}
|
}
|
||||||
//更新日志
|
|
||||||
|
// 更新完成标识
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
||||||
|
|
||||||
} catch (\RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\LogicException $e) {
|
} catch (LogicException $e) {
|
||||||
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,14 +7,9 @@ use app\common\QueueRedis;
|
|||||||
use app\api\controller\Aiarticle;
|
use app\api\controller\Aiarticle;
|
||||||
class WechatDraft
|
class WechatDraft
|
||||||
{
|
{
|
||||||
private $logPath;
|
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
private $completedExprie = 3600; // 完成状态过期时间
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->oQueueJob = new QueueJob;
|
$this->oQueueJob = new QueueJob;
|
||||||
@@ -23,24 +18,20 @@ class WechatDraft
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
|
|
||||||
// 检查数据库连接
|
try {
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查Redis连接状态
|
// 验证任务数据完整性
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取文章ID
|
|
||||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||||
if (empty($iArticleId)) {
|
if (empty($iArticleId)) {
|
||||||
$this->oQueueJob->log("无效的article_id,删除任务");
|
$this->oQueueJob->log("无效的article_id,删除任务");
|
||||||
@@ -48,59 +39,41 @@ class WechatDraft
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成Redis键并尝试获取锁
|
||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
|
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//上传草稿箱
|
// 执行核心任务
|
||||||
$oAiarticle = new Aiarticle;
|
$oAiarticle = new Aiarticle;
|
||||||
$aResult = json_decode($oAiarticle->syncWechat($data),true);
|
$response = $oAiarticle->syncWechat($data);
|
||||||
|
// 验证API响应
|
||||||
|
if (empty($response)) {
|
||||||
|
throw new RuntimeException("OpenAI API返回空结果");
|
||||||
|
}
|
||||||
|
// 检查JSON解析错误
|
||||||
|
$aResult = json_decode($response, true);
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
throw new RuntimeException("解析OpenAI响应失败: " . json_last_error_msg() . " | 原始响应: {$response}");
|
||||||
|
}
|
||||||
$sMsg = empty($aResult['msg']) ? '上传草稿箱失败' : $aResult['msg'];
|
$sMsg = empty($aResult['msg']) ? '上传草稿箱失败' : $aResult['msg'];
|
||||||
|
|
||||||
//更新日志
|
// 更新完成标识
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
||||||
|
|
||||||
} catch (\RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisKey,$job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\LogicException $e) {
|
} catch (LogicException $e) {
|
||||||
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisKey,$job);
|
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisKey,$job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,14 +7,9 @@ use app\common\QueueRedis;
|
|||||||
use app\api\controller\Aiarticle;
|
use app\api\controller\Aiarticle;
|
||||||
class WechatDraftPublish
|
class WechatDraftPublish
|
||||||
{
|
{
|
||||||
private $logPath;
|
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
private $completedExprie = 3600; // 完成状态过期时间
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->oQueueJob = new QueueJob;
|
$this->oQueueJob = new QueueJob;
|
||||||
@@ -23,24 +18,20 @@ class WechatDraftPublish
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
|
|
||||||
// 检查数据库连接
|
try {
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查Redis连接状态
|
// 验证任务数据完整性
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取文章ID
|
|
||||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||||
if (empty($iArticleId)) {
|
if (empty($iArticleId)) {
|
||||||
$this->oQueueJob->log("无效的article_id,删除任务");
|
$this->oQueueJob->log("无效的article_id,删除任务");
|
||||||
@@ -48,59 +39,41 @@ class WechatDraftPublish
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成Redis键并尝试获取锁
|
||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
|
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//发布草稿箱
|
// 执行核心任务
|
||||||
$oAiarticle = new Aiarticle;
|
$oAiarticle = new Aiarticle;
|
||||||
$aResult = json_decode($oAiarticle->publishDraft($data),true);
|
$response = $oAiarticle->publishDraft($data);
|
||||||
|
// 验证API响应
|
||||||
|
if (empty($response)) {
|
||||||
|
throw new RuntimeException("OpenAI API返回空结果");
|
||||||
|
}
|
||||||
|
// 检查JSON解析错误
|
||||||
|
$aResult = json_decode($response, true);
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
throw new RuntimeException("解析OpenAI响应失败: " . json_last_error_msg() . " | 原始响应: {$response}");
|
||||||
|
}
|
||||||
$sMsg = empty($aResult['msg']) ? '草稿箱发布失败' : $aResult['msg'];
|
$sMsg = empty($aResult['msg']) ? '草稿箱发布失败' : $aResult['msg'];
|
||||||
|
|
||||||
//更新日志
|
// 更新完成标识
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
||||||
|
|
||||||
} catch (\RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisKey,$job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\LogicException $e) {
|
} catch (LogicException $e) {
|
||||||
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisKey,$job);
|
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisKey,$job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,14 +7,9 @@ use app\common\QueueRedis;
|
|||||||
use app\api\controller\Aiarticle;
|
use app\api\controller\Aiarticle;
|
||||||
class WechatMaterial
|
class WechatMaterial
|
||||||
{
|
{
|
||||||
private $logPath;
|
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
private $completedExprie = 3600; // 完成状态过期时间
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->oQueueJob = new QueueJob;
|
$this->oQueueJob = new QueueJob;
|
||||||
@@ -23,24 +18,20 @@ class WechatMaterial
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
|
|
||||||
// 检查数据库连接
|
try {
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查Redis连接状态
|
// 验证任务数据完整性
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取文章ID
|
|
||||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||||
if (empty($iArticleId)) {
|
if (empty($iArticleId)) {
|
||||||
$this->oQueueJob->log("无效的article_id,删除任务");
|
$this->oQueueJob->log("无效的article_id,删除任务");
|
||||||
@@ -48,60 +39,41 @@ class WechatMaterial
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成Redis键并尝试获取锁
|
||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
|
|
||||||
|
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//上传素材
|
// 执行核心任务
|
||||||
$oAiarticle = new Aiarticle;
|
$oAiarticle = new Aiarticle;
|
||||||
$aResult = json_decode($oAiarticle->uploadMaterial($data),true);
|
$response = $oAiarticle->uploadMaterial($data);
|
||||||
|
// 验证API响应
|
||||||
|
if (empty($response)) {
|
||||||
|
throw new RuntimeException("OpenAI API返回空结果");
|
||||||
|
}
|
||||||
|
// 检查JSON解析错误
|
||||||
|
$aResult = json_decode($response, true);
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
throw new RuntimeException("解析OpenAI响应失败: " . json_last_error_msg() . " | 原始响应: {$response}");
|
||||||
|
}
|
||||||
$sMsg = empty($aResult['msg']) ? '上传素材失败' : $aResult['msg'];
|
$sMsg = empty($aResult['msg']) ? '上传素材失败' : $aResult['msg'];
|
||||||
|
|
||||||
//更新日志
|
// 更新完成标识
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
||||||
|
|
||||||
} catch (\RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisKey,$job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\LogicException $e) {
|
} catch (LogicException $e) {
|
||||||
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisKey,$job);
|
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisKey,$job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,14 +7,9 @@ use app\common\QueueRedis;
|
|||||||
use app\api\controller\Aiarticle;
|
use app\api\controller\Aiarticle;
|
||||||
class WechatQueryStatus
|
class WechatQueryStatus
|
||||||
{
|
{
|
||||||
private $logPath;
|
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
private $completedExprie = 3600; // 完成状态过期时间
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->oQueueJob = new QueueJob;
|
$this->oQueueJob = new QueueJob;
|
||||||
@@ -23,24 +18,20 @@ class WechatQueryStatus
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
|
|
||||||
// 检查数据库连接
|
try {
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查Redis连接状态
|
// 验证任务数据完整性
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取文章ID
|
|
||||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||||
if (empty($iArticleId)) {
|
if (empty($iArticleId)) {
|
||||||
$this->oQueueJob->log("无效的article_id,删除任务");
|
$this->oQueueJob->log("无效的article_id,删除任务");
|
||||||
@@ -48,61 +39,41 @@ class WechatQueryStatus
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成Redis键并尝试获取锁
|
||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
|
|
||||||
|
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询状态
|
// 执行核心任务
|
||||||
$oAiarticle = new Aiarticle;
|
$oAiarticle = new Aiarticle;
|
||||||
$aResult = json_decode($oAiarticle->queryStatus($data),true);
|
$response = $oAiarticle->queryStatus($data);
|
||||||
|
// 验证API响应
|
||||||
|
if (empty($response)) {
|
||||||
|
throw new RuntimeException("OpenAI API返回空结果");
|
||||||
|
}
|
||||||
|
// 检查JSON解析错误
|
||||||
|
$aResult = json_decode($response, true);
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
throw new RuntimeException("解析OpenAI响应失败: " . json_last_error_msg() . " | 原始响应: {$response}");
|
||||||
|
}
|
||||||
$sMsg = empty($aResult['msg']) ? '查询草稿箱文章是否发布失败' : $aResult['msg'];
|
$sMsg = empty($aResult['msg']) ? '查询草稿箱文章是否发布失败' : $aResult['msg'];
|
||||||
|
|
||||||
|
// 更新完成标识
|
||||||
//更新日志
|
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
||||||
|
|
||||||
} catch (\RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisKey,$job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\LogicException $e) {
|
} catch (LogicException $e) {
|
||||||
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisKey,$job);
|
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisKey,$job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,15 +6,11 @@ use app\common\OpenAi;
|
|||||||
use app\common\QueueJob;
|
use app\common\QueueJob;
|
||||||
use app\common\QueueRedis;
|
use app\common\QueueRedis;
|
||||||
|
|
||||||
class createFieldForQueue
|
class CreateFieldForQueue
|
||||||
{
|
{
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
private $completedExprie = 3600; // 完成状态过期时间
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->oQueueJob = new QueueJob;
|
$this->oQueueJob = new QueueJob;
|
||||||
@@ -23,22 +19,20 @@ class createFieldForQueue
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
|
|
||||||
// 检查数据库连接
|
try {
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 检查Redis连接状态
|
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 验证任务数据完整性
|
||||||
$iRedisId = empty($data['redis_id']) ? 0 : $data['redis_id'];
|
$iRedisId = empty($data['redis_id']) ? 0 : $data['redis_id'];
|
||||||
$sChunkIndex = empty($data['chunkIndex']) ? 0 : $data['chunkIndex'];
|
$sChunkIndex = empty($data['chunkIndex']) ? 0 : $data['chunkIndex'];
|
||||||
if (empty($iRedisId)) {
|
if (empty($iRedisId)) {
|
||||||
@@ -47,56 +41,41 @@ class createFieldForQueue
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成Redis键并尝试获取锁
|
||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iRedisId}:{$sChunkIndex}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iRedisId}:{$sChunkIndex}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// 执行核心任务
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
|
||||||
$oOpenAi = new OpenAi;
|
$oOpenAi = new OpenAi;
|
||||||
$aResult = json_decode($oOpenAi->createFieldForQueue($data), true);
|
$response = $oOpenAi->createFieldForQueue($data);
|
||||||
|
// 验证API响应
|
||||||
|
if (empty($response)) {
|
||||||
|
throw new RuntimeException("OpenAI API返回空结果");
|
||||||
|
}
|
||||||
|
// 检查JSON解析错误
|
||||||
|
$aResult = json_decode($response, true);
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
throw new RuntimeException("解析OpenAI响应失败: " . json_last_error_msg() . " | 原始响应: {$response}");
|
||||||
|
}
|
||||||
$sMsg = empty($aResult['msg']) ? '内容生成成功' : $aResult['msg'];
|
$sMsg = empty($aResult['msg']) ? '内容生成成功' : $aResult['msg'];
|
||||||
|
|
||||||
// 更新完成标识
|
// 更新完成标识
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
||||||
|
|
||||||
} catch (\RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\LogicException $e) {
|
} catch (LogicException $e) {
|
||||||
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,19 +2,14 @@
|
|||||||
namespace app\api\job;
|
namespace app\api\job;
|
||||||
|
|
||||||
use think\queue\Job;
|
use think\queue\Job;
|
||||||
use app\common\Material;
|
|
||||||
use app\common\QueueJob;
|
use app\common\QueueJob;
|
||||||
use app\common\QueueRedis;
|
use app\common\QueueRedis;
|
||||||
|
use app\common\Material;
|
||||||
class uploadMaterialStep
|
class uploadMaterialStep
|
||||||
{
|
{
|
||||||
private $oQueueJob;
|
private $oQueueJob;
|
||||||
private $QueueRedis;
|
private $QueueRedis;
|
||||||
private $maxRetries = 2;
|
private $completedExprie = 3600; // 完成状态过期时间
|
||||||
private $lockExpire = 1800;
|
|
||||||
private $completedExprie = 3600;
|
|
||||||
const JSON_OPTIONS = JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->oQueueJob = new QueueJob;
|
$this->oQueueJob = new QueueJob;
|
||||||
@@ -23,22 +18,20 @@ class uploadMaterialStep
|
|||||||
|
|
||||||
public function fire(Job $job, $data)
|
public function fire(Job $job, $data)
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
//任务开始判断
|
||||||
|
$this->oQueueJob->init($job);
|
||||||
|
|
||||||
|
// 获取 Redis 任务的原始数据
|
||||||
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
||||||
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
||||||
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
||||||
|
|
||||||
$this->oQueueJob->log("-----------队列任务开始-----------");
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
||||||
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
||||||
|
|
||||||
// 检查数据库连接
|
try {
|
||||||
if (!$this->oQueueJob->checkDbConnection(true)) {
|
|
||||||
$this->oQueueJob->log("数据库连接失败,无法执行任务,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 检查Redis连接状态
|
|
||||||
if (!$this->QueueRedis->getConnectionStatus()) {
|
|
||||||
$this->oQueueJob->log("Redis连接失败,10秒后重试");
|
|
||||||
$job->release(10);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 验证任务数据完整性
|
||||||
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
$iArticleId = empty($data['article_id']) ? 0 : $data['article_id'];
|
||||||
if(empty($iArticleId)){
|
if(empty($iArticleId)){
|
||||||
$iArticleId = empty($data['journal_id']) ? 0 : $data['journal_id'];
|
$iArticleId = empty($data['journal_id']) ? 0 : $data['journal_id'];
|
||||||
@@ -50,56 +43,41 @@ class uploadMaterialStep
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成Redis键并尝试获取锁
|
||||||
$sClassName = get_class($this);
|
$sClassName = get_class($this);
|
||||||
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}:{$sChunkIndex}";
|
$sRedisKey = "queue_job:{$sClassName}:{$iArticleId}:{$sChunkIndex}";
|
||||||
$sRedisValue = uniqid() . '_' . getmypid();
|
$sRedisValue = uniqid() . '_' . getmypid();
|
||||||
$lockExpire = $this->lockExpire;
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
||||||
|
return; // 未获取到锁,已处理
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// 执行核心任务
|
||||||
// 执行核心任务前再次检查连接
|
|
||||||
$result = $this->oQueueJob->checkDbConnection();
|
|
||||||
if (!$result) {
|
|
||||||
throw new \RuntimeException("数据库连接异常,无法执行核心任务");
|
|
||||||
}
|
|
||||||
$oMaterial = new Material;
|
$oMaterial = new Material;
|
||||||
$aResult = json_decode($oMaterial->uploadMaterialStep($data), true);
|
$response = $oMaterial->uploadMaterialStep($data);
|
||||||
|
// 验证API响应
|
||||||
|
if (empty($response)) {
|
||||||
|
throw new RuntimeException("OpenAI API返回空结果");
|
||||||
|
}
|
||||||
|
// 检查JSON解析错误
|
||||||
|
$aResult = json_decode($response, true);
|
||||||
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
|
throw new RuntimeException("解析OpenAI响应失败: " . json_last_error_msg() . " | 原始响应: {$response}");
|
||||||
|
}
|
||||||
$sMsg = empty($aResult['msg']) ? '素材上传失败' : $aResult['msg'];
|
$sMsg = empty($aResult['msg']) ? '素材上传失败' : $aResult['msg'];
|
||||||
|
|
||||||
// 更新完成标识
|
// 更新完成标识
|
||||||
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
||||||
$job->delete();
|
$job->delete();
|
||||||
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | 执行日志:{$sMsg}");
|
||||||
|
|
||||||
} catch (\RuntimeException $e) {
|
} catch (RuntimeException $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\LogicException $e) {
|
} catch (LogicException $e) {
|
||||||
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleNonRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
$this->oQueueJob->handleRetryableException($e,$sRedisKey,$sRedisValue, $job);
|
||||||
} finally {
|
} finally {
|
||||||
$executionTime = microtime(true) - $startTime;
|
$this->oQueueJob->finnal();
|
||||||
$this->oQueueJob->log("任务执行完成,耗时: " . number_format($executionTime, 4) . "秒");
|
|
||||||
gc_collect_cycles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user