From a47bbb2c6aa6b05ad02e7c623267d751c572008c Mon Sep 17 00:00:00 2001 From: augushong Date: Tue, 26 May 2026 18:28:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(timer):=20=E8=87=AA=E5=8A=A8=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E6=89=A7=E8=A1=8C=E6=97=A5=E5=BF=97=E5=B9=B6=E6=8D=95?= =?UTF-8?q?=E8=8E=B7=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add execute() wrapper in TimerControllerBase that wraps do() with logStart/logEnd, captures return value to result field. Change site URL routing from /do to /execute with task_name injection. Add result field to system_timer_log scheme. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- app/admin/scheme/SystemTimerLog.php | 4 +++ extend/base/common/command/TimerBase.php | 2 +- .../common/controller/TimerControllerBase.php | 25 ++++++++++++++++++- .../base/common/service/TimerServiceBase.php | 5 ++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/app/admin/scheme/SystemTimerLog.php b/app/admin/scheme/SystemTimerLog.php index 4c7230e..11ec6a9 100644 --- a/app/admin/scheme/SystemTimerLog.php +++ b/app/admin/scheme/SystemTimerLog.php @@ -50,6 +50,10 @@ class SystemTimerLog extends BaseScheme #[Component(type: 'textarea', options: [])] public $error_message; + #[Field(type: 'text', nullable: true, comment: '执行结果(任务返回值)')] + #[Component(type: 'textarea', options: [])] + public $result; + #[Field(type: 'int', length: 11, default: '0', comment: '并发分片ID')] #[Component(type: 'text', options: [])] public $concurrency_id; diff --git a/extend/base/common/command/TimerBase.php b/extend/base/common/command/TimerBase.php index 895d5ac..dfc2632 100644 --- a/extend/base/common/command/TimerBase.php +++ b/extend/base/common/command/TimerBase.php @@ -38,7 +38,7 @@ class TimerBase extends Command ->addOption('quit', null, Option::VALUE_NONE) ->addOption('local', null, Option::VALUE_NONE) ->addOption('local-host', null, Option::VALUE_OPTIONAL, '本地域名', 'http://localhost') - ->addOption('local-port', null, Option::VALUE_OPTIONAL, '本地端口', '80') + ->addOption('local-port', null, Option::VALUE_OPTIONAL, '本地端口', '8000') ->setDescription('内置秒级定时器'); } diff --git a/extend/base/common/controller/TimerControllerBase.php b/extend/base/common/controller/TimerControllerBase.php index 464226e..a60e17a 100644 --- a/extend/base/common/controller/TimerControllerBase.php +++ b/extend/base/common/controller/TimerControllerBase.php @@ -64,6 +64,7 @@ class TimerControllerBase extends ToolsController 'duration' => 0, 'status' => 'running', 'error_message' => null, + 'result' => null, 'concurrency_id' => $this->concurrencyId, 'create_time' => time(), ]; @@ -71,7 +72,7 @@ class TimerControllerBase extends ToolsController return $log->id; } - public function logEnd(int $logId, string $status = 'success', ?string $errorMessage = null): void + public function logEnd(int $logId, string $status = 'success', ?string $errorMessage = null, ?string $result = null): void { $log = SystemTimerLog::find($logId); if ($log) { @@ -80,7 +81,29 @@ class TimerControllerBase extends ToolsController $log->duration = ($endTime - $log->start_time) * 1000; // ms $log->status = $status; $log->error_message = $errorMessage; + $log->result = $result; $log->save(); } } + + /** + * 日志包裹入口:自动记录 do() 的执行过程和返回值. + * 子类仍实现 do(),URL 路由从 /do 改为 /execute 触发此方法. + */ + public function execute() + { + $taskName = $this->request->param('task_name', ''); + $logId = $this->logStart($taskName); + + try { + $result = $this->do(); + $resultStr = is_string($result) ? $result : json_encode($result, JSON_UNESCAPED_UNICODE); + $this->logEnd($logId, 'success', null, $resultStr); + + return $result; + } catch (\Throwable $e) { + $this->logEnd($logId, 'error', $e->getMessage()); + throw $e; + } + } } diff --git a/extend/base/common/service/TimerServiceBase.php b/extend/base/common/service/TimerServiceBase.php index 5b2fd7c..4e8a715 100644 --- a/extend/base/common/service/TimerServiceBase.php +++ b/extend/base/common/service/TimerServiceBase.php @@ -119,10 +119,15 @@ class TimerServiceBase 'concurrency_id' => $i, 'concurrency_count' => $concurrency, 'host_id' => HostService::getNodeId(), + 'task_name' => $config_item['name'], ]; // 处理target if ($config_item['type'] == 'site') { $target_info = parse_url($target); + // 将 /do 替换为 /execute,用于日志包裹 + if (isset($target_info['path'])) { + $target_info['path'] = preg_replace('#/do$#', '/execute', $target_info['path']); + } $query_params = []; if (isset($target_info['query'])) { parse_str($target_info['query'], $query_params);