56 lines
1.7 KiB
PHP
56 lines
1.7 KiB
PHP
<?php
|
||
|
||
namespace app\api\job;
|
||
|
||
use think\queue\Job;
|
||
use app\common\ExpertFinderService;
|
||
use app\common\QueueJob;
|
||
|
||
/**
|
||
* 专家抓取队列任务。
|
||
* 注意:此任务推送到队列名 "FetchExperts",必须单独启动 worker 才会执行:
|
||
* php think queue:work --queue FetchExperts --sleep=3 --tries=3 --daemon
|
||
*
|
||
* 单条任务可能耗时较长(NCBI 接口翻页 + 写库),常驻 worker 受 wait_timeout 影响,
|
||
* 由 QueueJob 在进程超过 6h 或致命 DB 错误时主动 exit(1) 让 supervisor 拉起新进程。
|
||
*/
|
||
class FetchExperts
|
||
{
|
||
private $oQueueJob;
|
||
|
||
public function __construct()
|
||
{
|
||
$this->oQueueJob = new QueueJob();
|
||
}
|
||
|
||
public function fire(Job $job, $data)
|
||
{
|
||
$this->oQueueJob->init($job);
|
||
|
||
$field = isset($data['field']) ? (string)$data['field'] : '';
|
||
if ($field === '') {
|
||
$this->oQueueJob->log("FetchExperts 无效的 field,删除任务");
|
||
$job->delete();
|
||
return;
|
||
}
|
||
|
||
try {
|
||
$service = new ExpertFinderService();
|
||
$service->doFetchForField(
|
||
$field,
|
||
isset($data['source']) ? $data['source'] : 'pubmed',
|
||
isset($data['per_page']) ? intval($data['per_page']) : 100,
|
||
isset($data['min_year']) ? $data['min_year'] : null
|
||
);
|
||
$this->oQueueJob->log("FetchExperts 完成 | field={$field}");
|
||
$job->delete();
|
||
} catch (\Exception $e) {
|
||
$this->oQueueJob->handleException($e, $job, "field={$field}");
|
||
} catch (\Throwable $e) {
|
||
$this->oQueueJob->handleException($e, $job, "field={$field}");
|
||
} finally {
|
||
$this->oQueueJob->finnal();
|
||
}
|
||
}
|
||
}
|