varaverk: replace User Scripts dependency with native job tracking

run_job.sh: new wrapper called by scheduler cron for every job.
  Writes /var/log/varaverk/<id>.json (status/start/end/exit/pid)
  and /var/log/varaverk/<id>.log (latest run output, overwritten).
  Maps exit 0→ok, 1→warn, 2+→error so watchdog "took action" runs
  show warn not error.

scheduler: cron now calls bash run_job.sh instead of inline bash -c.
  Added vv_job_stat_path(). LOG_DIR moved to config.php (shared).

monitor: vv_scripts_status() now reads /var/log/varaverk/*.json and
  */*.json — no User Scripts tmpScripts dependency at all.
  Stale running detection via /proc/<pid> check.
  Returns error_count in addition to running/ok/warn counts.

pages/monitor.php: added error pill (red ✗), error icon/color in
  list, duration shown next to age for each script entry.
This commit is contained in:
Gmer4Lfe
2026-05-24 17:51:31 -04:00
parent ea9f39a803
commit 57d55c647c
5 changed files with 79 additions and 36 deletions
@@ -648,37 +648,35 @@ function vv_log_tail(string $path, int $lines): string {
}
function vv_scripts_status(): array {
$tmpBase = '/tmp/user.scripts/tmpScripts';
$runDir = '/tmp/user.scripts/running';
$running = [];
foreach (glob($runDir . '/*') ?: [] as $f) {
$running[basename($f)] = true;
}
$logDir = LOG_DIR;
$statFiles = array_merge(
glob("$logDir/*.json") ?: [],
glob("$logDir/*/*.json") ?: []
);
$scripts = [];
foreach (glob($tmpBase . '/*/') ?: [] as $dir) {
$name = basename(rtrim($dir, '/'));
$logPath = $dir . 'log.txt';
$ts = @filemtime($logPath);
if (!$ts) continue;
foreach ($statFiles as $statFile) {
$stat = json_decode(@file_get_contents($statFile) ?: '{}', true) ?: [];
$status = $stat['status'] ?? 'unknown';
$isRunning = isset($running[$name]);
if ($isRunning) {
$status = 'running';
} else {
$tail = vv_log_tail($logPath, 15);
$finished = stripos($tail, 'Script Finished') !== false;
if ($finished) {
$hasWarn = (bool)preg_match('/permission denied|command not found|no such file|: error[\s:]/i', $tail);
$status = $hasWarn ? 'warn' : 'ok';
} else {
$status = 'unknown';
}
// Stale running — PID gone (crash or reboot with no cleanup)
if ($status === 'running' && !empty($stat['pid'])) {
if (!file_exists("/proc/{$stat['pid']}")) $status = 'error';
}
$scripts[] = ['name' => $name, 'last_ts' => $ts, 'status' => $status, 'running' => $isRunning];
$id = $stat['id'] ?? basename($statFile, '.json');
$name = basename(preg_replace('/\.sh$/', '', $id));
$ts = (int)($stat['end'] ?? $stat['start'] ?? @filemtime($statFile) ?: 0);
$scripts[] = [
'name' => $name,
'last_ts' => $ts,
'status' => $status,
'running' => $status === 'running',
'exit' => $stat['exit'] ?? null,
'duration' => isset($stat['start'], $stat['end'])
? (int)$stat['end'] - (int)$stat['start'] : null,
];
}
usort($scripts, fn($a, $b) => ($b['last_ts'] ?? 0) <=> ($a['last_ts'] ?? 0));
@@ -689,5 +687,6 @@ function vv_scripts_status(): array {
'running_count' => count(array_filter($scripts, fn($s) => $s['status'] === 'running')),
'ok_count' => count(array_filter($scripts, fn($s) => $s['status'] === 'ok')),
'warn_count' => count(array_filter($scripts, fn($s) => $s['status'] === 'warn')),
'error_count' => count(array_filter($scripts, fn($s) => $s['status'] === 'error')),
];
}