feat(timer): 新增 run_type 调度、host_id 投递和日志清理

T7: TimerBase shouldExecuteTask() - main/auto/all/manual/disabled modes
    with two-phase DB row lock for auto mode
T8: TimerControllerBase - host_id param, logStart/logEnd methods
    TimerServiceBase - inject host_id into site URLs
T9: TimerLogClean command - php think admin:timer:log:clean --days=30
This commit is contained in:
augushong
2026-05-26 02:41:10 +08:00
parent abeac2c3cb
commit 25fab093fa
7 changed files with 215 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
namespace base\common\controller;
use app\admin\model\SystemTimerLog;
use app\common\controller\ToolsController;
use think\facade\Cache;
@@ -13,6 +14,8 @@ class TimerControllerBase extends ToolsController
protected $concurrencyId = 0;
protected $hostId = null;
public function initialize()
{
parent::initialize();
@@ -28,6 +31,8 @@ class TimerControllerBase extends ToolsController
$this->error('concurrency count error');
}
$this->hostId = $this->request->param('host_id', '');
if (is_int($this->frequency)) {
$this->protectVisit($this->frequency);
}
@@ -47,4 +52,35 @@ class TimerControllerBase extends ToolsController
Cache::tag($cache_tag)->set($cache_key, time());
}
public function logStart(string $taskName): int
{
$data = [
'task_name' => $taskName,
'node_id' => $this->hostId,
'run_type' => '',
'start_time' => time(),
'end_time' => 0,
'duration' => 0,
'status' => 'running',
'error_message' => null,
'concurrency_id' => $this->concurrencyId,
'create_time' => time(),
];
$log = SystemTimerLog::create($data);
return $log->id;
}
public function logEnd(int $logId, string $status = 'success', ?string $errorMessage = null): void
{
$log = SystemTimerLog::find($logId);
if ($log) {
$endTime = time();
$log->end_time = $endTime;
$log->duration = ($endTime - $log->start_time) * 1000; // ms
$log->status = $status;
$log->error_message = $errorMessage;
$log->save();
}
}
}