参看文献再次更新

This commit is contained in:
wangjinlei
2026-07-15 13:18:53 +08:00
parent 296294d405
commit 6c43d9b994
3 changed files with 152 additions and 33 deletions

View File

@@ -1932,7 +1932,8 @@ class Production extends Base
} }
$this->referToDoi($data['p_article_id']); $this->referToDoi($data['p_article_id']);
(new ReferenceDispatchService())->dispatchRefersByType($data['p_article_id']); // 每条文献单独入队,接口立即返回;多 worker 可并行处理 200+ 条
(new ReferenceDispatchService())->enqueueRefersByType($data['p_article_id']);
return jsonSuccess([]); return jsonSuccess([]);
} }
@@ -1999,7 +2000,7 @@ class Production extends Base
public function doiTofrag($p_article_id) public function doiTofrag($p_article_id)
{ {
(new ReferenceDispatchService())->dispatchRefersByType($p_article_id); (new ReferenceDispatchService())->enqueueRefersByType($p_article_id);
return jsonSuccess([]); return jsonSuccess([]);
} }

View File

@@ -0,0 +1,70 @@
<?php
namespace app\api\job;
use think\queue\Job;
use app\common\QueueJob;
use app\common\QueueRedis;
use app\common\ReferenceDispatchService;
/**
* 单条参考文献分流journal/book/other异步任务。
* freshRefers 按 p_refer_id 逐条入队,多条文献由多个 worker 并行处理。
*
* Worker
* php think queue:work --queue ReferenceDispatchQueue,ArticleReferDetailQueue
*/
class ReferenceDispatchQueue
{
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'];
$this->oQueueJob->log("-----------队列任务开始-----------");
$this->oQueueJob->log("当前任务ID: {$jobId}, 尝试次数: {$job->attempts()}");
$iPArticleId = empty($data['p_article_id']) ? 0 : intval($data['p_article_id']);
$iPReferId = empty($data['p_refer_id']) ? 0 : intval($data['p_refer_id']);
if (empty($iPArticleId) || empty($iPReferId)) {
$this->oQueueJob->log("无效的 p_article_id 或 p_refer_id删除任务");
$job->delete();
return;
}
try {
$sClassName = get_class($this);
$sRedisKey = "queue_job:{$sClassName}:{$iPArticleId}:{$iPReferId}";
$sRedisValue = uniqid() . '_' . getmypid();
if (!$this->oQueueJob->acquireLock($sRedisKey, $sRedisValue, $job)) {
return;
}
(new ReferenceDispatchService())->dispatchReferByType($iPReferId);
$this->QueueRedis->finishJob($sRedisKey, 'completed', $this->completedExprie, $sRedisValue);
$job->delete();
$this->oQueueJob->log("任务执行成功 | 日志ID: {$sRedisKey} | p_refer_id:{$iPReferId}");
} 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();
}
}
}

View File

@@ -7,7 +7,8 @@ use think\Env;
use think\Queue; use think\Queue;
/** /**
* 参考文献分流处理:freshRefers 入口之后,按 journal / book / other 三路处理。 * 参考文献分流处理:按 journal / book / other 三路处理。
* 每条文献单独入 ReferenceDispatchQueue由 worker 并行消费。
* *
* - journal有 DOI 入 ArticleReferDetailQueue无 DOI 保留原文 * - journal有 DOI 入 ArticleReferDetailQueue无 DOI 保留原文
* - book填充结构化字段 author/title/joura/dateno/isbn与 Preaccept、References 约定一致) * - book填充结构化字段 author/title/joura/dateno/isbn与 Preaccept、References 约定一致)
@@ -24,9 +25,9 @@ class ReferenceDispatchService
} }
/** /**
* 对某篇生产文章全部参考文献按类型分流处理 * 文章全部有效参考文献逐条入队分流HTTP 内只做 push立即返回
*/ */
public function dispatchRefersByType($pArticleId) public function enqueueRefersByType($pArticleId)
{ {
$pArticleId = intval($pArticleId); $pArticleId = intval($pArticleId);
if ($pArticleId <= 0) { if ($pArticleId <= 0) {
@@ -43,43 +44,90 @@ class ReferenceDispatchService
return; return;
} }
foreach ($refers as $refer) {
Queue::push(
'app\api\job\ReferenceDispatchQueue@fire',
$refer,
'ReferenceDispatchQueue'
);
}
}
/**
* 处理单条参考文献分流(由 ReferenceDispatchQueue 调用)
*/
public function dispatchReferByType($pReferId)
{
$pReferId = intval($pReferId);
if ($pReferId <= 0) {
return;
}
$refer = Db::name('production_article_refer')
->where('p_refer_id', $pReferId)
->where('state', 0)
->find();
if (empty($refer)) {
return;
}
$crossref = new CrossrefService([ $crossref = new CrossrefService([
'mailto' => trim((string)Env::get('crossref_mailto', '')), 'mailto' => trim((string)Env::get('crossref_mailto', '')),
]); ]);
foreach ($refers as $refer) { $summary = null;
$summary = null; $crossrefType = '';
$crossrefType = ''; if (trim((string)$refer['refer_doi']) !== '') {
if (trim((string)$refer['refer_doi']) !== '') { $doiNorm = $this->normalizeDoi($refer['refer_doi']);
$doiNorm = $this->normalizeDoi($refer['refer_doi']); if ($doiNorm !== '') {
if ($doiNorm !== '') { $summary = $crossref->fetchWorkSummary($doiNorm);
$summary = $crossref->fetchWorkSummary($doiNorm); if ($summary && !empty($summary['raw']['type'])) {
if ($summary && !empty($summary['raw']['type'])) { $crossrefType = (string)$summary['raw']['type'];
$crossrefType = (string)$summary['raw']['type'];
}
} }
} }
}
$typeInfo = $this->classifier->classify((string)$refer['refer_content'], $crossrefType); $typeInfo = $this->classifier->classify((string)$refer['refer_content'], $crossrefType);
$dispatchType = $this->classifier->normalizeDispatchType($typeInfo['type']); $dispatchType = $this->classifier->normalizeDispatchType($typeInfo['type']);
Db::name('production_article_refer')->where('p_refer_id', $refer['p_refer_id'])->update([ Db::name('production_article_refer')->where('p_refer_id', $refer['p_refer_id'])->update([
'refer_type' => $dispatchType, 'refer_type' => $dispatchType,
'update_time' => time(), 'update_time' => time(),
]); ]);
$refer['refer_type'] = $dispatchType; $refer['refer_type'] = $dispatchType;
switch ($dispatchType) { switch ($dispatchType) {
case ReferenceTypeClassifier::TYPE_BOOK: case ReferenceTypeClassifier::TYPE_BOOK:
$this->processBookRefer($refer, $summary, $crossref); $this->processBookRefer($refer, $summary, $crossref);
break; break;
case ReferenceTypeClassifier::TYPE_OTHER: case ReferenceTypeClassifier::TYPE_OTHER:
$this->processOtherRefer($refer); $this->processOtherRefer($refer);
break; break;
default: default:
$this->processJournalRefer($refer); $this->processJournalRefer($refer);
break; break;
} }
}
/**
* 同步处理整篇文章(仅供脚本/调试,勿在 HTTP 或单条队列任务中批量调用)
*/
public function dispatchRefersByType($pArticleId)
{
$pArticleId = intval($pArticleId);
if ($pArticleId <= 0) {
return;
}
$refers = Db::name('production_article_refer')
->where('p_article_id', $pArticleId)
->where('state', 0)
->order('index asc, p_refer_id asc')
->column('p_refer_id');
foreach ($refers as $pReferId) {
$this->dispatchReferByType($pReferId);
} }
} }