feat: jobs system with dedicated nav page and run history
All checks were successful
CI / test (pull_request) Successful in 14s
CI / build-dev (pull_request) Has been skipped

Replaces ad-hoc Tailscale config tracking with a proper jobs system.
Jobs get their own nav page (master/detail layout), a dedicated DB
table, and full run history persisted forever. Tailscale connection
settings move from the Settings modal into the Jobs page. Registry
pattern makes adding future jobs straightforward.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-28 19:09:42 -04:00
parent 537d78e71b
commit d7727badb1
9 changed files with 541 additions and 178 deletions

View File

@@ -2,9 +2,9 @@ import { Router } from 'express';
import {
getInstances, getInstance, getDistinctStacks,
createInstance, updateInstance, deleteInstance, importInstances, getInstanceHistory, getAllHistory,
getConfig, setConfig,
getConfig, setConfig, getJobs, getJob, updateJob, getJobRuns,
} from './db.js';
import { runTailscaleSync, restartJobs } from './jobs.js';
import { runJob, restartJobs } from './jobs.js';
export const router = Router();
@@ -14,12 +14,14 @@ const VALID_STATES = ['deployed', 'testing', 'degraded'];
const VALID_STACKS = ['production', 'development'];
const SERVICE_KEYS = ['atlas', 'argus', 'semaphore', 'patchmon', 'tailscale', 'andromeda'];
const CONFIG_KEYS = [
'tailscale_api_key', 'tailscale_tailnet', 'tailscale_poll_minutes',
'tailscale_enabled', 'tailscale_last_run_at', 'tailscale_last_result',
];
const REDACTED = '**REDACTED**';
function maskJob(job) {
const cfg = JSON.parse(job.config || '{}');
if (cfg.api_key) cfg.api_key = REDACTED;
return { ...job, config: cfg };
}
function validate(body) {
const errors = [];
if (!body.name || typeof body.name !== 'string' || !body.name.trim())
@@ -169,37 +171,47 @@ router.delete('/instances/:vmid', (req, res) => {
}
});
// GET /api/config
router.get('/config', (_req, res) => {
const cfg = {};
for (const key of CONFIG_KEYS) {
const val = getConfig(key);
cfg[key] = (key === 'tailscale_api_key' && val) ? REDACTED : val;
}
res.json(cfg);
// GET /api/jobs
router.get('/jobs', (_req, res) => {
res.json(getJobs().map(maskJob));
});
// PUT /api/config
router.put('/config', (req, res) => {
for (const key of CONFIG_KEYS) {
if (!(key in (req.body ?? {}))) continue;
if (key === 'tailscale_api_key' && req.body[key] === REDACTED) continue;
setConfig(key, req.body[key]);
}
try { restartJobs(); } catch (e) { console.error('PUT /api/config restartJobs', e); }
res.json({ ok: true });
// GET /api/jobs/:id
router.get('/jobs/:id', (req, res) => {
const id = parseInt(req.params.id, 10);
if (!id) return res.status(400).json({ error: 'invalid id' });
const job = getJob(id);
if (!job) return res.status(404).json({ error: 'job not found' });
res.json({ ...maskJob(job), runs: getJobRuns(id) });
});
// POST /api/jobs/tailscale/run
router.post('/jobs/tailscale/run', async (req, res) => {
if (!getConfig('tailscale_api_key') || !getConfig('tailscale_tailnet'))
return res.status(400).json({ error: 'Tailscale not configured' });
// PUT /api/jobs/:id
router.put('/jobs/:id', (req, res) => {
const id = parseInt(req.params.id, 10);
if (!id) return res.status(400).json({ error: 'invalid id' });
const job = getJob(id);
if (!job) return res.status(404).json({ error: 'job not found' });
const { enabled, schedule, config: newCfg } = req.body ?? {};
const existingCfg = JSON.parse(job.config || '{}');
const mergedCfg = { ...existingCfg, ...(newCfg ?? {}) };
if (newCfg?.api_key === REDACTED) mergedCfg.api_key = existingCfg.api_key;
updateJob(id, {
enabled: enabled != null ? (enabled ? 1 : 0) : job.enabled,
schedule: schedule != null ? (parseInt(schedule, 10) || 15) : job.schedule,
config: JSON.stringify(mergedCfg),
});
try { restartJobs(); } catch (e) { console.error('PUT /api/jobs/:id restartJobs', e); }
res.json(maskJob(getJob(id)));
});
// POST /api/jobs/:id/run
router.post('/jobs/:id/run', async (req, res) => {
const id = parseInt(req.params.id, 10);
if (!id) return res.status(400).json({ error: 'invalid id' });
if (!getJob(id)) return res.status(404).json({ error: 'job not found' });
try {
const result = await runTailscaleSync();
setConfig('tailscale_last_run_at', new Date().toISOString());
setConfig('tailscale_last_result', `ok: ${result.updated} updated of ${result.total}`);
res.json(result);
res.json(await runJob(id));
} catch (e) {
handleDbError('POST /api/jobs/tailscale/run', e, res);
handleDbError('POST /api/jobs/:id/run', e, res);
}
});