修改自动推广的相关任务
This commit is contained in:
@@ -4,26 +4,52 @@ namespace app\api\job;
|
||||
|
||||
use think\queue\Job;
|
||||
use app\common\ExpertFinderService;
|
||||
use app\common\QueueJob;
|
||||
|
||||
/**
|
||||
* 专家抓取队列任务
|
||||
* 专家抓取队列任务。
|
||||
* 注意:此任务推送到队列名 "FetchExperts",必须单独启动 worker 才会执行:
|
||||
* php think queue:listen --queue FetchExperts
|
||||
* 若只运行 queue:listen 不指定队列,默认只消费 "mail",本任务不会被执行。
|
||||
* 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)
|
||||
{
|
||||
$field = isset($data['field']) ? $data['field'] : '';
|
||||
$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->init($job);
|
||||
|
||||
$job->delete();
|
||||
$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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user