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

@@ -17,7 +17,7 @@ function init(path) {
db.exec('PRAGMA foreign_keys = ON');
db.exec('PRAGMA synchronous = NORMAL');
createSchema();
if (path !== ':memory:') seed();
if (path !== ':memory:') { seed(); seedJobs(); }
}
function createSchema() {
@@ -58,6 +58,26 @@ function createSchema() {
key TEXT PRIMARY KEY,
value TEXT NOT NULL DEFAULT ''
);
CREATE TABLE IF NOT EXISTS jobs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
key TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
enabled INTEGER NOT NULL DEFAULT 0 CHECK(enabled IN (0,1)),
schedule INTEGER NOT NULL DEFAULT 15,
config TEXT NOT NULL DEFAULT '{}'
);
CREATE TABLE IF NOT EXISTS job_runs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
job_id INTEGER NOT NULL,
started_at TEXT NOT NULL DEFAULT (datetime('now')),
ended_at TEXT,
status TEXT NOT NULL DEFAULT 'running' CHECK(status IN ('running','success','error')),
result TEXT NOT NULL DEFAULT ''
);
CREATE INDEX IF NOT EXISTS idx_job_runs_job_id ON job_runs(job_id);
`);
}
@@ -88,6 +108,21 @@ function seed() {
db.exec('COMMIT');
}
function seedJobs() {
const count = db.prepare('SELECT COUNT(*) as n FROM jobs').get().n;
if (count > 0) return;
const apiKey = getConfig('tailscale_api_key');
const tailnet = getConfig('tailscale_tailnet');
const schedule = parseInt(getConfig('tailscale_poll_minutes', '15'), 10) || 15;
const enabled = getConfig('tailscale_enabled') === '1' ? 1 : 0;
db.prepare(`
INSERT INTO jobs (key, name, description, enabled, schedule, config)
VALUES ('tailscale_sync', 'Tailscale Sync',
'Syncs Tailscale device status and IPs to instances by matching hostnames.',
?, ?, ?)
`).run(enabled, schedule, JSON.stringify({ api_key: apiKey, tailnet }));
}
// ── Queries ───────────────────────────────────────────────────────────────────
export function getInstances(filters = {}) {
@@ -204,6 +239,54 @@ export function setConfig(key, value) {
).run(key, String(value));
}
// ── Jobs ──────────────────────────────────────────────────────────────────────
const JOB_WITH_LAST_RUN = `
SELECT j.*,
r.id AS last_run_id,
r.started_at AS last_run_at,
r.status AS last_status,
r.result AS last_result
FROM jobs j
LEFT JOIN job_runs r
ON r.id = (SELECT id FROM job_runs WHERE job_id = j.id ORDER BY id DESC LIMIT 1)
`;
export function getJobs() {
return db.prepare(JOB_WITH_LAST_RUN + ' ORDER BY j.id').all();
}
export function getJob(id) {
return db.prepare(JOB_WITH_LAST_RUN + ' WHERE j.id = ?').get(id) ?? null;
}
export function createJob(data) {
db.prepare(`
INSERT INTO jobs (key, name, description, enabled, schedule, config)
VALUES (@key, @name, @description, @enabled, @schedule, @config)
`).run(data);
}
export function updateJob(id, { enabled, schedule, config }) {
db.prepare(`
UPDATE jobs SET enabled=@enabled, schedule=@schedule, config=@config WHERE id=@id
`).run({ id, enabled, schedule, config });
}
export function createJobRun(jobId) {
return Number(db.prepare('INSERT INTO job_runs (job_id) VALUES (?)').run(jobId).lastInsertRowid);
}
export function completeJobRun(runId, status, result) {
db.prepare(`
UPDATE job_runs SET ended_at=datetime('now'), status=@status, result=@result WHERE id=@id
`).run({ id: runId, status, result });
}
export function getJobRuns(jobId) {
return db.prepare('SELECT * FROM job_runs WHERE job_id = ? ORDER BY id DESC').all(jobId);
}
// ── Test helpers ──────────────────────────────────────────────────────────────
export function _resetForTest() {

View File

@@ -1,63 +1,74 @@
import { getInstances, updateInstance, getConfig, setConfig } from './db.js';
import { getJobs, getJob, getInstances, updateInstance, createJobRun, completeJobRun } from './db.js';
// ── Handlers ──────────────────────────────────────────────────────────────────
const TAILSCALE_API = 'https://api.tailscale.com/api/v2';
let _interval = null;
export async function runTailscaleSync() {
const apiKey = getConfig('tailscale_api_key');
const tailnet = getConfig('tailscale_tailnet');
if (!apiKey || !tailnet) throw new Error('Tailscale not configured');
async function tailscaleSyncHandler(cfg) {
const { api_key, tailnet } = cfg;
if (!api_key || !tailnet) throw new Error('Tailscale not configured — set API key and tailnet');
const res = await fetch(
`${TAILSCALE_API}/tailnet/${encodeURIComponent(tailnet)}/devices`,
{ headers: { Authorization: `Bearer ${apiKey}` } }
{ headers: { Authorization: `Bearer ${api_key}` } }
);
if (!res.ok) throw new Error(`Tailscale API ${res.status}`);
const { devices } = await res.json();
// hostname -> first 100.x.x.x address
const tsMap = new Map(
devices.map(d => [d.hostname, (d.addresses ?? []).find(a => a.startsWith('100.')) ?? ''])
);
const instances = getInstances();
let updated = 0;
for (const inst of instances) {
const tsIp = tsMap.get(inst.name); // undefined = not in Tailscale
const tsIp = tsMap.get(inst.name);
const matched = tsIp !== undefined;
const newTailscale = matched ? 1 : (inst.tailscale === 1 ? 0 : inst.tailscale);
const newIp = matched ? tsIp : (inst.tailscale === 1 ? '' : inst.tailscale_ip);
if (newTailscale !== inst.tailscale || newIp !== inst.tailscale_ip) {
// Strip db-generated columns — node:sqlite rejects unknown named parameters
const { id: _id, created_at: _ca, updated_at: _ua, ...instData } = inst;
updateInstance(inst.vmid, { ...instData, tailscale: newTailscale, tailscale_ip: newIp });
updated++;
}
}
return { updated, total: instances.length };
return { summary: `${updated} updated of ${instances.length}` };
}
// ── Registry ──────────────────────────────────────────────────────────────────
const HANDLERS = {
tailscale_sync: tailscaleSyncHandler,
};
// ── Public API ────────────────────────────────────────────────────────────────
export async function runJob(jobId) {
const job = getJob(jobId);
if (!job) throw new Error('Job not found');
const handler = HANDLERS[job.key];
if (!handler) throw new Error(`No handler for '${job.key}'`);
const cfg = JSON.parse(job.config || '{}');
const runId = createJobRun(jobId);
try {
const result = await handler(cfg);
completeJobRun(runId, 'success', result.summary ?? '');
return result;
} catch (e) {
completeJobRun(runId, 'error', e.message);
throw e;
}
}
const _intervals = new Map();
export function restartJobs() {
if (_interval) { clearInterval(_interval); _interval = null; }
if (getConfig('tailscale_enabled') !== '1') return;
const mins = parseInt(getConfig('tailscale_poll_minutes', '15'), 10);
const ms = Math.max(1, Number.isFinite(mins) ? mins : 15) * 60_000;
_interval = setInterval(async () => {
try {
const r = await runTailscaleSync();
setConfig('tailscale_last_run_at', new Date().toISOString());
setConfig('tailscale_last_result', `ok: ${r.updated} updated of ${r.total}`);
} catch (e) {
setConfig('tailscale_last_run_at', new Date().toISOString());
setConfig('tailscale_last_result', `error: ${e.message}`);
}
}, ms);
for (const iv of _intervals.values()) clearInterval(iv);
_intervals.clear();
for (const job of getJobs()) {
if (!job.enabled) continue;
const ms = Math.max(1, job.schedule || 15) * 60_000;
const id = job.id;
_intervals.set(id, setInterval(() => runJob(id).catch(() => {}), ms));
}
}

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);
}
});