fix(timer): 自动记录执行日志并捕获结果

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 <clio-agent@sisyphuslabs.ai>
This commit is contained in:
augushong
2026-05-26 18:28:36 +08:00
parent e1bf94a55e
commit a47bbb2c6a
4 changed files with 34 additions and 2 deletions

View File

@@ -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;
}
}
}