更新
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace app\adminapi\controller\crawler;
|
||||
|
||||
use app\adminapi\controller\BaseAdminController;
|
||||
use app\adminapi\lists\crawler\TaskLogLists;
|
||||
use app\common\model\CrawlerTaskLog;
|
||||
|
||||
class TaskLogController extends BaseAdminController
|
||||
{
|
||||
public function lists()
|
||||
{
|
||||
return $this->dataLists(new TaskLogLists());
|
||||
}
|
||||
|
||||
public function detail()
|
||||
{
|
||||
$id = $this->request->get('id/d', 0);
|
||||
$log = CrawlerTaskLog::findOrEmpty($id);
|
||||
if ($log->isEmpty()) {
|
||||
return $this->fail('日志不存在');
|
||||
}
|
||||
return $this->data(TaskLogLists::formatItem($log->toArray()));
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ class KbConfigLists extends BaseAdminDataLists
|
||||
->whereOrLike('name', 'qwen_%')
|
||||
->whereOrLike('name', 'openai_%')
|
||||
->whereOrLike('name', 'mtranserver_%')
|
||||
->whereOrLike('name', 'whisper_asr_%')
|
||||
->whereOrLike('name', 'assistant_%')
|
||||
->whereOr('name', '=', 'post_analysis_prompt');
|
||||
})->limit($this->limitOffset, $this->limitLength)
|
||||
@@ -31,6 +32,7 @@ class KbConfigLists extends BaseAdminDataLists
|
||||
->whereOrLike('name', 'qwen_%')
|
||||
->whereOrLike('name', 'openai_%')
|
||||
->whereOrLike('name', 'mtranserver_%')
|
||||
->whereOrLike('name', 'whisper_asr_%')
|
||||
->whereOrLike('name', 'assistant_%')
|
||||
->whereOr('name', '=', 'post_analysis_prompt');
|
||||
})->count();
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace app\adminapi\lists\crawler;
|
||||
|
||||
use app\adminapi\lists\BaseAdminDataLists;
|
||||
use app\common\lists\ListsSearchInterface;
|
||||
use app\common\model\CrawlerTaskLog;
|
||||
|
||||
class TaskLogLists extends BaseAdminDataLists implements ListsSearchInterface
|
||||
{
|
||||
public function setSearch(): array
|
||||
{
|
||||
return [
|
||||
'=' => ['action', 'status', 'trigger_type', 'source'],
|
||||
'%like%' => ['task_name', 'task_key'],
|
||||
'between_time' => 'create_time',
|
||||
];
|
||||
}
|
||||
|
||||
public function lists(): array
|
||||
{
|
||||
$lists = CrawlerTaskLog::field('id,task_key,task_name,action,cron_expression,source,container_id,status,trigger_type,output,error_message,elapsed,started_at,finished_at,create_time,update_time')
|
||||
->where($this->searchWhere)
|
||||
->limit($this->limitOffset, $this->limitLength)
|
||||
->order('id', 'desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
foreach ($lists as &$item) {
|
||||
$item = self::formatItem($item);
|
||||
}
|
||||
|
||||
return $lists;
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return CrawlerTaskLog::where($this->searchWhere)->count();
|
||||
}
|
||||
|
||||
public static function formatItem(array $item): array
|
||||
{
|
||||
$item['status_text'] = match ((int) ($item['status'] ?? 0)) {
|
||||
0 => '执行中',
|
||||
1 => '成功',
|
||||
2 => '失败',
|
||||
3 => '已跳过',
|
||||
default => '未知',
|
||||
};
|
||||
$item['trigger_type_text'] = ((int) ($item['trigger_type'] ?? 1)) === 2 ? '手动' : '自动';
|
||||
foreach (['started_at', 'finished_at', 'create_time', 'update_time'] as $field) {
|
||||
$value = $item[$field] ?? 0;
|
||||
$item[$field . '_text'] = self::formatTime($value);
|
||||
}
|
||||
$item['output_preview'] = mb_substr((string) ($item['output'] ?? ''), 0, 120);
|
||||
return $item;
|
||||
}
|
||||
|
||||
private static function formatTime($value): string
|
||||
{
|
||||
if (empty($value)) {
|
||||
return '-';
|
||||
}
|
||||
return is_numeric($value) ? date('Y-m-d H:i:s', (int) $value) : (string) $value;
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,14 @@ use Cron\CronExpression;
|
||||
*/
|
||||
class CrontabLogic extends BaseLogic
|
||||
{
|
||||
private const CRAWLER_MIGRATED_MESSAGE = 'crawler任务已迁移到Docker容器调度,请查看管理端爬虫任务日志';
|
||||
|
||||
private const CRAWLER_MIGRATED_COMMANDS = [
|
||||
'crawler',
|
||||
'truth_social',
|
||||
'fifa_worldcup_news',
|
||||
];
|
||||
|
||||
/**
|
||||
* @notes 添加定时任务
|
||||
* @param $params
|
||||
@@ -158,6 +166,13 @@ class CrontabLogic extends BaseLogic
|
||||
throw new \Exception('定时任务不存在');
|
||||
}
|
||||
|
||||
if (in_array($crontab->command, self::CRAWLER_MIGRATED_COMMANDS, true)) {
|
||||
Crontab::where('id', $crontab->id)->update([
|
||||
'error' => self::CRAWLER_MIGRATED_MESSAGE,
|
||||
]);
|
||||
throw new \Exception(self::CRAWLER_MIGRATED_MESSAGE);
|
||||
}
|
||||
|
||||
$log = CrontabLog::create([
|
||||
'crontab_id' => $crontab->id,
|
||||
'name' => $crontab->name,
|
||||
@@ -169,18 +184,6 @@ class CrontabLogic extends BaseLogic
|
||||
'trigger_type' => 2,
|
||||
]);
|
||||
|
||||
if ($crontab->command === 'crawler') {
|
||||
$crawlerDir = app()->getRootPath() . 'public/dongqiudi-crawler';
|
||||
$python = 'python3';
|
||||
$action = $crontab->params ?: 'all';
|
||||
$logId = $log->id;
|
||||
$crontabId = $crontab->id;
|
||||
$cmd = "cd {$crawlerDir} && nohup {$python} main.py {$action} --log-id={$logId} --crontab-id={$crontabId} > /dev/null 2>&1 &";
|
||||
\shell_exec($cmd);
|
||||
|
||||
return ['log_id' => $logId, 'output' => '任务已提交,后台执行中...', 'async' => true];
|
||||
}
|
||||
|
||||
if ($crontab->command === 'truth_social') {
|
||||
$crawlerDir = app()->getRootPath() . 'public/dongqiudi-crawler';
|
||||
$python = 'python3';
|
||||
|
||||
Reference in New Issue
Block a user