feat: add github spider md push and async task logging
This commit is contained in:
124
code/app/task/logic/DomainImportAsyncTaskLogic.php
Normal file
124
code/app/task/logic/DomainImportAsyncTaskLogic.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\task\logic;
|
||||
|
||||
use app\common\helper\DomainImportAsyncJobHelper;
|
||||
use app\common\helper\DomainSpiderMdRunHelper;
|
||||
|
||||
class DomainImportAsyncTaskLogic
|
||||
{
|
||||
public static function handle(array $data = []): void
|
||||
{
|
||||
$jobId = trim((string)($data['job_id'] ?? ''));
|
||||
if ($jobId === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$job = DomainImportAsyncJobHelper::readJob($jobId);
|
||||
if (empty($job)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
DomainImportAsyncJobHelper::updateJob($jobId, [
|
||||
'status' => DomainImportAsyncJobHelper::STATUS_RUNNING,
|
||||
'current_step' => 'starting',
|
||||
'message' => '任务开始执行',
|
||||
]);
|
||||
|
||||
DomainImportAsyncJobHelper::appendLog($jobId, '开始执行任务:' . (string)($job['label'] ?? $job['type'] ?? 'unknown'));
|
||||
DomainImportAsyncJobHelper::updateProgress($jobId, 1, 100, 'prepare', '开始准备执行环境');
|
||||
|
||||
$type = trim((string)($job['type'] ?? ''));
|
||||
$payload = is_array($job['payload'] ?? null) ? (array)$job['payload'] : [];
|
||||
|
||||
switch ($type) {
|
||||
case 'spider_md_generate':
|
||||
$summary = self::runSpiderMdGenerate($jobId, $payload);
|
||||
break;
|
||||
default:
|
||||
throw new \RuntimeException('暂未支持的异步任务类型:' . $type);
|
||||
}
|
||||
|
||||
DomainImportAsyncJobHelper::updateJob($jobId, [
|
||||
'status' => DomainImportAsyncJobHelper::STATUS_SUCCESS,
|
||||
'current_step' => 'completed',
|
||||
'message' => '任务执行完成',
|
||||
'summary' => $summary,
|
||||
]);
|
||||
DomainImportAsyncJobHelper::appendLog($jobId, '任务执行完成');
|
||||
} catch (\Throwable $e) {
|
||||
DomainImportAsyncJobHelper::appendLog($jobId, '任务执行失败:' . $e->getMessage());
|
||||
DomainImportAsyncJobHelper::updateJob($jobId, [
|
||||
'status' => DomainImportAsyncJobHelper::STATUS_FAILED,
|
||||
'current_step' => 'failed',
|
||||
'message' => '任务执行失败:' . $e->getMessage(),
|
||||
'summary' => [
|
||||
'error' => $e->getMessage(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
protected static function runSpiderMdGenerate(string $jobId, array $payload): array
|
||||
{
|
||||
DomainImportAsyncJobHelper::appendLog($jobId, '开始执行蜘蛛池MD生成');
|
||||
DomainImportAsyncJobHelper::updateProgress($jobId, 5, 100, 'running', '开始生成蜘蛛池MD');
|
||||
|
||||
$state = [
|
||||
'current' => 5,
|
||||
'total' => 100,
|
||||
'domain_total' => 0,
|
||||
];
|
||||
|
||||
$logger = static function (string $message) use ($jobId, &$state): void {
|
||||
$message = trim($message);
|
||||
if ($message === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
DomainImportAsyncJobHelper::appendLog($jobId, $message);
|
||||
|
||||
if (preg_match('/已载入\s+(\d+)\s+个域名/u', $message, $matches)) {
|
||||
$state['domain_total'] = max(0, (int)($matches[1] ?? 0));
|
||||
DomainImportAsyncJobHelper::updateProgress($jobId, 10, 100, 'running', $message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (preg_match('/\[(\d+)\/(\d+)\]/', $message, $matches)) {
|
||||
$current = max(0, (int)($matches[1] ?? 0));
|
||||
$total = max(1, (int)($matches[2] ?? 1));
|
||||
$percent = 10 + (int)floor(($current / $total) * 75);
|
||||
$state['current'] = min(95, max($state['current'], $percent));
|
||||
DomainImportAsyncJobHelper::updateProgress($jobId, $state['current'], 100, 'running', $message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (mb_strpos($message, '推送失败') !== false) {
|
||||
DomainImportAsyncJobHelper::updateProgress($jobId, max($state['current'], 90), 100, 'running', $message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (mb_strpos($message, '已推送 GitHub') !== false || mb_strpos($message, '已推送 Gitee') !== false) {
|
||||
$state['current'] = min(98, max($state['current'], $state['current'] + 1));
|
||||
DomainImportAsyncJobHelper::updateProgress($jobId, $state['current'], 100, 'running', $message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (mb_strpos($message, '蜘蛛池 MD 生成完成') !== false) {
|
||||
DomainImportAsyncJobHelper::updateProgress($jobId, 99, 100, 'completed', $message);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
$summary = DomainSpiderMdRunHelper::run($payload, $logger);
|
||||
|
||||
DomainImportAsyncJobHelper::updateProgress($jobId, 100, 100, 'completed', '蜘蛛池MD生成完成');
|
||||
|
||||
return $summary;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user