0.12.0: FIFO job retention (job_keep), clear-finished button, vmm-ctl prune
This commit is contained in:
1 parent
5dc6bfb5a2
commit
a1b4e884da
9 files changed
+83
-3
No files matched your search
@@ -185,6 +185,13 @@ switch ($cmd) {
|
||||
echo $d['ok'] ? "cancel requested\n" : ('error: ' . $d['error'] . "\n");
|
||||
});
|
||||
|
||||
case 'prune':
|
||||
$keep = $arg !== '' ? (int)$arg : (int)(vmm_config()['job_keep'] ?? 100);
|
||||
$res = vmm_job_prune($keep);
|
||||
out($res, $json, static function (array $d): void {
|
||||
printf("removed %d, kept %d\n", $d['removed'], $d['kept']);
|
||||
});
|
||||
|
||||
case 'recover':
|
||||
$fixed = vmm_job_recover();
|
||||
out(['ok' => true, 'fixed' => $fixed], $json, static function (array $d): void {
|
||||
|
||||
@@ -269,4 +269,9 @@ joblog($log, sprintf('finished: rc=%d, state=%s', $rc, $stateAfter));
|
||||
flock($lock, LOCK_UN);
|
||||
fclose($lock);
|
||||
fclose($log);
|
||||
|
||||
// FIFO retention: keep only the configured number of finished jobs
|
||||
$conf = vmm_config();
|
||||
vmm_job_prune((int)($conf['job_keep'] ?? 100));
|
||||
|
||||
exit(0);
|
||||
@@ -34,6 +34,7 @@ function vmm_config(): array
|
||||
'enable' => true,
|
||||
'job_timeout' => 900,
|
||||
'after_action' => 'stay', // stay | log
|
||||
'job_keep' => 100, // finished jobs to keep (0 = none)
|
||||
'vm_dir_cache' => '',
|
||||
];
|
||||
if (!is_file(VMM_CONF)) {
|
||||
@@ -635,6 +636,42 @@ function vmm_job_list(int $limit = 50, string $vm = ''): array
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete old finished jobs (FIFO). Queued/running jobs are never touched.
|
||||
* Returns ['removed' => int, 'kept' => int].
|
||||
*/
|
||||
function vmm_job_prune(int $keep = 100, bool $onlyFinished = true): array
|
||||
{
|
||||
vmm_ensure_state_dirs();
|
||||
$files = glob(VMM_JOBDIR . '/*.json') ?: [];
|
||||
rsort($files); // newest first
|
||||
$kept = 0;
|
||||
$removed = 0;
|
||||
foreach ($files as $i => $f) {
|
||||
$job = json_decode((string)file_get_contents($f), true);
|
||||
if (!is_array($job)) {
|
||||
@unlink($f);
|
||||
continue;
|
||||
}
|
||||
$state = (string)($job['state'] ?? '');
|
||||
$running = in_array($state, ['queued', 'running'], true);
|
||||
if ($running) {
|
||||
$kept++;
|
||||
continue;
|
||||
}
|
||||
if ($kept < $keep) {
|
||||
$kept++;
|
||||
continue;
|
||||
}
|
||||
$id = (string)($job['id'] ?? basename($f, '.json'));
|
||||
@unlink($f);
|
||||
@unlink(VMM_JOBDIR . '/' . $id . '.log');
|
||||
@unlink(VMM_JOBDIR . '/' . $id . '.cancel');
|
||||
$removed++;
|
||||
}
|
||||
return ['removed' => $removed, 'kept' => $kept];
|
||||
}
|
||||
|
||||
function vmm_job_log(string $id, int $maxBytes = 65536): string
|
||||
{
|
||||
$f = VMM_JOBDIR . '/' . $id . '.log';
|
||||
|
||||
Reference in new issue
Block a user