修改自动推广的相关任务

This commit is contained in:
wangjinlei
2026-05-13 12:26:28 +08:00
parent c36eba77b1
commit fa878334cd
7 changed files with 289 additions and 29 deletions

View File

@@ -34,6 +34,13 @@ class PlagiarismService
*/
const MAX_POLL_ATTEMPTS = 60;
private $logFile;
public function __construct()
{
$this->logFile = ROOT_PATH . 'runtime' . DS . 'plagiarism_task.log';
}
// ---------- 顶层入口 ----------
/**
@@ -55,6 +62,7 @@ class PlagiarismService
->where('article_id', $articleId)
->value('journal_id');
$now = time();
$checkId = Db::name('plagiarism_check')->insertGetId([
'article_id' => $articleId,
@@ -67,12 +75,12 @@ class PlagiarismService
'ctime' => $now,
'utime' => $now,
]);
$this->log("submit service act");
// 入队执行:上传 + 触发 similarity
Queue::push(
'app\\api\\job\\PlagiarismRun',
['check_id' => $checkId, 'file_path' => $filePath],
'plagiarism'
'PlagiarismRun'
);
return (int)$checkId;
@@ -84,7 +92,7 @@ class PlagiarismService
public function runUploadAndTrigger($checkId, $filePath)
{
$check = $this->mustGetCheck($checkId);
$this->log("runUploadAndTrigger is act0");
try {
$tii = new TurnitinService();
@@ -95,7 +103,7 @@ class PlagiarismService
if ($articleTitle === '') {
$articleTitle = 'Article #' . $check['article_id'];
}
$this->log("runUploadAndTrigger is act1");
$createResp = $tii->createSubmission([
'title' => mb_substr($articleTitle, 0, 250),
'owner' => 'editor_' . $check['triggered_by'],
@@ -114,7 +122,7 @@ class PlagiarismService
'tii_submission_id' => $submissionId,
'raw_response' => json_encode($createResp, JSON_UNESCAPED_UNICODE),
]);
$this->log("runUploadAndTrigger is act2");
// 2. 上传文件
$tii->uploadFile($submissionId, $filePath, basename($filePath));
@@ -127,12 +135,14 @@ class PlagiarismService
'raw_response' => json_encode($simResp, JSON_UNESCAPED_UNICODE),
]);
$this->log("runUploadAndTrigger is act3");
// 4. 排队首次轮询(晚一点开始,让 Turnitin 先处理)
Queue::later(
self::POLL_INTERVAL,
'app\\api\\job\\PlagiarismPoll',
['check_id' => $checkId, 'attempt' => 1],
'plagiarism'
'PlagiarismPoll'
);
} catch (\Throwable $e) {
$this->markFailed($checkId, '[upload] ' . $e->getMessage());
@@ -320,6 +330,7 @@ class PlagiarismService
private function markFailed($checkId, $errMsg)
{
$this->log("markFailed act");
$this->updateCheck($checkId, [
'state' => 4,
'error_msg' => mb_substr($errMsg, 0, 1000),
@@ -337,7 +348,7 @@ class PlagiarismService
$row = Db::name('article_file')
->where('article_id', $articleId)
->where('type_name', 'manuscirpt') // 历史拼写
->order('article_file_id desc')
->order('file_id desc')
->find();
if (!$row || empty($row['file_url'])) {
throw new Exception("article #{$articleId} has no manuscirpt file");
@@ -420,4 +431,10 @@ class PlagiarismService
{
return Db::name('plagiarism_check')->where('check_id', $checkId)->find();
}
public function log($msg)
{
$line = date('Y-m-d H:i:s') . ' ' . $msg . PHP_EOL;
@file_put_contents($this->logFile, $line, FILE_APPEND);
}
}