163 lines
6.6 KiB
PHP
163 lines
6.6 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;
|
|
use app\common\service\LLMService;
|
|
|
|
class ReferenceCheckTwo
|
|
{
|
|
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']);
|
|
}
|
|
$sClassName = get_class($this);
|
|
$sRedisKey = "queue_job_two:{$sClassName}:{$checkId}";
|
|
$sRedisValue = uniqid() . '_' . getmypid();
|
|
|
|
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
|
|
return;
|
|
}
|
|
|
|
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']) === ReferenceCheckService::RECORD_COMPLETED) {
|
|
// $job->delete();
|
|
// return;
|
|
// }
|
|
|
|
try {
|
|
$svc = new ReferenceCheckService();
|
|
|
|
$contentA = $svc->resolveMainContentForJob($row);
|
|
$referText = trim((string)(isset($row['refer_text']) ? $row['refer_text'] : ''));
|
|
$refer = null;
|
|
|
|
if (intval($row['p_refer_id']) > 0) {
|
|
$refer = Db::name('production_article_refer')
|
|
->where('p_refer_id', intval($row['p_refer_id']))
|
|
->where('state', 0)
|
|
->find();
|
|
}
|
|
|
|
$payload = $svc->prepareRecheckPayload(is_array($refer) ? $refer : [], $referText);
|
|
$doiBlock = $payload['doi_block'];
|
|
|
|
if ($contentA === '' || $referText === '') {
|
|
$this->markFailed($checkId, 'Missing article_main.content or refer_text');
|
|
$job->delete();
|
|
return;
|
|
}
|
|
$llm = new LLMService();
|
|
$llmResult = $llm->checkReference($contentA, $referText, true, $doiBlock);
|
|
|
|
$requestFailed = !empty($llmResult['request_failed']);
|
|
$canSupport = $svc->parseLlmCanSupport($llmResult);
|
|
$tag = $payload['has_abstract']
|
|
? ('[Crossref复核' . ($payload['doi_used'] !== '' ? ' ' . $payload['doi_used'] : '') . ']')
|
|
: '[Crossref复核-无摘要]';
|
|
$reason = $tag . ' ' . (isset($llmResult['reason']) ? $llmResult['reason'] : '');
|
|
|
|
// LLM 通讯失败:写 status=RECORD_FAILED(3) 并抛异常触发队列重试
|
|
if ($requestFailed) {
|
|
$svc->updateCheckResult($checkId, [
|
|
'confidence' => floatval($llmResult['confidence']),
|
|
'reason' => $reason,
|
|
'status' => ReferenceCheckService::RECORD_FAILED,
|
|
'error_msg' => isset($llmResult['reason']) ? $llmResult['reason'] : 'LLM request failed',
|
|
]);
|
|
throw new \RuntimeException(isset($llmResult['reason']) ? $llmResult['reason'] : 'LLM request failed');
|
|
}
|
|
|
|
$affected = $svc->updateCheckResult($checkId, [
|
|
'can_support' => $canSupport ? 1 : 0,
|
|
'is_match' => $canSupport ? 1 : 0,
|
|
'confidence' => floatval($llmResult['confidence']),
|
|
'reason' => $reason,
|
|
'status' => ReferenceCheckService::RECORD_COMPLETED,
|
|
'error_msg' => '',
|
|
]);
|
|
$this->oQueueJob->log("Crossref复核写入 id={$checkId} affected={$affected} can_support=" . ($canSupport ? 1 : 0) . " confidence=" . floatval($llmResult['confidence']));
|
|
|
|
$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('ReferenceCheckTwo 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' => ReferenceCheckService::RECORD_FAILED,
|
|
'error_msg' => $msg,
|
|
]);
|
|
} catch (\Exception $e) {
|
|
\think\Log::error('ReferenceCheckTwo markFailed: ' . $e->getMessage());
|
|
}
|
|
$amId = empty($row) ? 0 : intval(isset($row['am_id']) ? $row['am_id'] : 0);
|
|
if ($amId > 0) {
|
|
(new ReferenceCheckService())->syncAmRefCheckStatus($amId);
|
|
}
|
|
}
|
|
}
|