feat: jobs system with dedicated nav page and run history
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:
85
server/db.js
85
server/db.js
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user