feat(scheduler): add auto_backup, auto_stop, auto_down, auto_start and delete_after_run one-shot mode (#777)

* feat(scheduler): add auto_backup, auto_stop, auto_down, auto_start actions and delete_after_run one-shot mode

Extends the scheduler with four new stack-targeted actions:

- auto_backup: backs up stack compose files and .env using the existing
  FileSystemService.backupStackFiles primitive
- auto_stop: runs compose stop (containers preserved)
- auto_down: runs compose down (containers removed)
- auto_start: runs compose up -d via deployStack (universal start for both
  stopped and down stacks)

Adds delete_after_run boolean column to scheduled_tasks. When enabled,
the task self-deletes after its first successful execution; failures keep
the task so the user can debug and retry.

All four new actions gate at Admiral tier, consistent with restart/snapshot/prune.
Migration is idempotent (maybeAddCol).

* docs(scheduler): update scheduled-operations doc with new lifecycle actions and delete-after-run

Adds the four new actions (Backup Stack Files, Stop Stack, Take Stack Down,
Start Stack) to the action table. Documents the delete-after-run one-shot mode
with its success-only deletion semantics. Adds the Stack Lifecycle Scheduling
section explaining stop-vs-down semantics and the local-execution boundary.
Adds three troubleshooting entries: auto-start on a missing compose folder,
auto-backup single-slot overwrite by design, and one-shot task disappearing
after successful run.

Updates the timeline description from four to five lanes. Refreshes
screenshots to show the new dialog layout with the Lifecycle lane visible.
This commit is contained in:
Anso
2026-04-25 16:11:13 -04:00
committed by GitHub
parent e0034132b4
commit abee078741
10 changed files with 341 additions and 18 deletions
+6 -3
View File
@@ -264,7 +264,7 @@ export interface ScheduledTask {
target_type: 'stack' | 'fleet' | 'system';
target_id: string | null;
node_id: number | null;
action: 'restart' | 'snapshot' | 'prune' | 'update' | 'scan';
action: 'restart' | 'snapshot' | 'prune' | 'update' | 'scan' | 'auto_backup' | 'auto_stop' | 'auto_down' | 'auto_start';
cron_expression: string;
enabled: number;
created_by: string;
@@ -277,6 +277,7 @@ export interface ScheduledTask {
prune_targets: string | null;
target_services: string | null;
prune_label_filter: string | null;
delete_after_run?: number;
}
export interface ScheduledTaskRun {
@@ -949,6 +950,7 @@ export class DatabaseService {
maybeAddCol('scheduled_tasks', 'prune_targets', 'TEXT DEFAULT NULL');
maybeAddCol('scheduled_tasks', 'target_services', 'TEXT DEFAULT NULL');
maybeAddCol('scheduled_tasks', 'prune_label_filter', 'TEXT DEFAULT NULL');
maybeAddCol('scheduled_tasks', 'delete_after_run', 'INTEGER DEFAULT 0');
// Recreate stack_update_status with composite PK (node_id, stack_name).
// Original table had stack_name as sole PK which breaks when multiple nodes share stack names.
@@ -2471,13 +2473,13 @@ export class DatabaseService {
public createScheduledTask(task: Omit<ScheduledTask, 'id'>): number {
const result = this.db.prepare(
'INSERT INTO scheduled_tasks (name, target_type, target_id, node_id, action, cron_expression, enabled, created_by, created_at, updated_at, last_run_at, next_run_at, last_status, last_error, prune_targets, target_services, prune_label_filter) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
'INSERT INTO scheduled_tasks (name, target_type, target_id, node_id, action, cron_expression, enabled, created_by, created_at, updated_at, last_run_at, next_run_at, last_status, last_error, prune_targets, target_services, prune_label_filter, delete_after_run) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
).run(
task.name, task.target_type, task.target_id, task.node_id,
task.action, task.cron_expression, task.enabled, task.created_by,
task.created_at, task.updated_at, task.last_run_at, task.next_run_at,
task.last_status, task.last_error, task.prune_targets, task.target_services,
task.prune_label_filter
task.prune_label_filter, task.delete_after_run ?? 0
);
return result.lastInsertRowid as number;
}
@@ -2494,6 +2496,7 @@ export class DatabaseService {
last_status: updates.last_status, last_error: updates.last_error,
prune_targets: updates.prune_targets, target_services: updates.target_services,
prune_label_filter: updates.prune_label_filter,
delete_after_run: updates.delete_after_run,
};
for (const [col, val] of Object.entries(map)) {
+56 -6
View File
@@ -289,10 +289,35 @@ export class SchedulerService {
scanFailedCount = result.failed;
break;
}
case 'auto_backup':
output = await this.executeAutoBackup(task);
break;
case 'auto_stop':
output = await this.executeAutoStop(task);
break;
case 'auto_down':
output = await this.executeAutoDown(task);
break;
case 'auto_start':
output = await this.executeAutoStart(task);
break;
}
if (isDebugEnabled()) console.log(`[SchedulerService:debug] Task ${task.id} action completed in ${Date.now() - actionStart}ms`);
db.updateScheduledTaskRun(runId, {
completed_at: Date.now(),
status: 'success',
output,
});
console.log(`[SchedulerService] Task "${task.name}" (id=${task.id}) completed successfully`);
if (task.delete_after_run === 1) {
console.log(`[SchedulerService] Task "${task.name}" (id=${task.id}) self-deleting after successful one-shot run`);
db.deleteScheduledTask(task.id);
return;
}
const nextRun = this.calculateNextRun(task.cron_expression);
db.updateScheduledTask(task.id, {
last_run_at: Date.now(),
@@ -301,12 +326,7 @@ export class SchedulerService {
last_error: null,
updated_at: Date.now(),
});
db.updateScheduledTaskRun(runId, {
completed_at: Date.now(),
status: 'success',
output,
});
console.log(`[SchedulerService] Task "${task.name}" (id=${task.id}) completed successfully`);
if (task.action === 'scan') {
const scanLevel: 'info' | 'warning' = scanFailedCount > 0 ? 'warning' : 'info';
if (isDebugEnabled()) {
@@ -392,6 +412,36 @@ export class SchedulerService {
return `Restarted ${filtered.length} container(s) in stack "${task.target_id}"${servicesSuffix}`;
}
private assertStackTarget(task: ScheduledTask, label: string): asserts task is ScheduledTask & { target_id: string; node_id: number } {
if (!task.target_id || task.node_id == null) {
throw new Error(`${label} requires target_id and node_id`);
}
}
private async executeAutoBackup(task: ScheduledTask): Promise<string> {
this.assertStackTarget(task, 'Auto-backup');
await FileSystemService.getInstance(task.node_id).backupStackFiles(task.target_id);
return `Backed up stack "${task.target_id}" files`;
}
private async executeAutoStop(task: ScheduledTask): Promise<string> {
this.assertStackTarget(task, 'Auto-stop');
await ComposeService.getInstance(task.node_id).runCommand(task.target_id, 'stop');
return `Stopped stack "${task.target_id}" (containers preserved)`;
}
private async executeAutoDown(task: ScheduledTask): Promise<string> {
this.assertStackTarget(task, 'Auto-down');
await ComposeService.getInstance(task.node_id).runCommand(task.target_id, 'down');
return `Took down stack "${task.target_id}" (containers removed)`;
}
private async executeAutoStart(task: ScheduledTask): Promise<string> {
this.assertStackTarget(task, 'Auto-start');
await ComposeService.getInstance(task.node_id).deployStack(task.target_id);
return `Started stack "${task.target_id}"`;
}
private async executeSnapshot(task: ScheduledTask): Promise<string> {
const db = DatabaseService.getInstance();
const nodes = db.getNodes();