115 lines
3.9 KiB
PHP
115 lines
3.9 KiB
PHP
<?php
|
|
namespace app\api\job;
|
|
|
|
use think\Db;
|
|
use think\queue\Job;
|
|
use app\common\QueueJob;
|
|
use app\common\QueueRedis;
|
|
use app\common\ReferenceCheckService;
|
|
|
|
class ReferenceCheck
|
|
{
|
|
private $oQueueJob;
|
|
private $QueueRedis;
|
|
private $completedExprie = 3600;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->oQueueJob = new QueueJob();
|
|
$this->QueueRedis = QueueRedis::getInstance();
|
|
}
|
|
|
|
public function fire(Job $job, $data)
|
|
{
|
|
$this->oQueueJob->init($job);
|
|
|
|
$rawBody = empty($job->getRawBody()) ? '' : $job->getRawBody();
|
|
$jobData = empty($rawBody) ? [] : json_decode($rawBody, true);
|
|
$jobId = empty($jobData['id']) ? 'unknown' : $jobData['id'];
|
|
|
|
$sRedisKey = '';
|
|
$sRedisValue = '';
|
|
|
|
$this->oQueueJob->log("-----------队列任务开始-----------");
|
|
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
|
|
|
|
try {
|
|
$checkId = intval(isset($data['check_id']) ? $data['check_id'] : 0);
|
|
if ($checkId <= 0 && !empty($jobData['data']['check_id'])) {
|
|
$checkId = intval($jobData['data']['check_id']);
|
|
}
|
|
if ($checkId <= 0) {
|
|
$job->delete();
|
|
return;
|
|
}
|
|
|
|
$row = Db::name('article_reference_check_result')->where('id', $checkId)->find();
|
|
if (empty($row)) {
|
|
$job->delete();
|
|
return;
|
|
}
|
|
|
|
if (intval($row['status']) === 1) {
|
|
$job->delete();
|
|
return;
|
|
}
|
|
|
|
$sClassName = get_class($this);
|
|
$sRedisKey = "queue_job:{$sClassName}:{$checkId}";
|
|
$sRedisValue = uniqid() . '_' . getmypid();
|
|
|
|
$svc = new ReferenceCheckService();
|
|
$svc->clearReferenceCheckQueueLock($checkId);
|
|
|
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$svc->runReferenceCheckOnce($checkId);
|
|
|
|
$amId = intval(isset($row['am_id']) ? $row['am_id'] : 0);
|
|
if ($amId > 0) {
|
|
$svc->syncAmRefCheckStatus($amId);
|
|
}
|
|
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
|
|
$job->delete();
|
|
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey}");
|
|
} catch (\Exception $e) {
|
|
$this->oQueueJob->log('ReferenceCheck error: ' . $e->getMessage());
|
|
if ($job->attempts() >= 3) {
|
|
$this->markFailed($checkId, $e->getMessage());
|
|
$job->delete();
|
|
return;
|
|
}
|
|
$job->release(30);
|
|
}
|
|
} catch (\RuntimeException $e) {
|
|
$this->oQueueJob->handleRetryableException($e, $sRedisKey, $sRedisValue, $job);
|
|
} catch (\LogicException $e) {
|
|
$this->oQueueJob->handleNonRetryableException($e, $sRedisKey, $sRedisValue, $job);
|
|
} catch (\Exception $e) {
|
|
$this->oQueueJob->handleRetryableException($e, $sRedisKey, $sRedisValue, $job);
|
|
} finally {
|
|
$this->oQueueJob->finnal();
|
|
}
|
|
}
|
|
|
|
private function markFailed($checkId, $msg)
|
|
{
|
|
$row = Db::name('article_reference_check_result')->where('id', $checkId)->find();
|
|
try {
|
|
(new ReferenceCheckService())->updateCheckResult($checkId, [
|
|
'status' => 2,
|
|
'error_msg' => $msg,
|
|
]);
|
|
} catch (\Exception $e) {
|
|
\think\Log::error('ReferenceCheck markFailed: ' . $e->getMessage());
|
|
}
|
|
$amId = empty($row) ? 0 : intval(isset($row['am_id']) ? $row['am_id'] : 0);
|
|
if ($amId > 0) {
|
|
(new ReferenceCheckService())->syncAmRefCheckStatus($amId);
|
|
}
|
|
}
|
|
}
|