internal/jobs persists jobs in the jobs table, which is also the queue: a
bounded pool of workers claims queued jobs oldest first. Handlers are
registered per kind before Start with typed payloads and results, and can
publish partial results to the row while they run. Only typed results and
*jobs.Failure messages are stored; any other handler error is logged and
stored as "internal error", so dependency error text cannot surface a
secret. Start fails jobs a previous process left running with
"interrupted by restart" before any worker runs.
GET /api/v1/jobs/{id} returns {id, kind, status, result, error} inside the
protected /api/v1 group; an unknown id is a 404 not_found.
Migration 0003_jobs_status rebuilds jobs with status queued, running,
succeeded, failed, mapping legacy pending and done rows.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
33 lines
1.2 KiB
SQL
33 lines
1.2 KiB
SQL
-- 0003_jobs_status: jobs.status becomes queued, running, succeeded, failed,
|
|
-- the states the job runner (internal/jobs) reports. SQLite cannot change a
|
|
-- CHECK in place, so the table is rebuilt: create the new table, copy every
|
|
-- row, drop the old one and rename. Nothing references jobs and it has no
|
|
-- indexes or triggers, so the rebuild touches no other schema object. Rows
|
|
-- written under the 0001 values are mapped (pending -> queued,
|
|
-- done -> succeeded) so the copy cannot fail on an existing database.
|
|
|
|
CREATE TABLE jobs_new (
|
|
id TEXT NOT NULL PRIMARY KEY,
|
|
kind TEXT NOT NULL,
|
|
status TEXT NOT NULL CHECK (status IN ('queued', 'running', 'succeeded', 'failed')),
|
|
payload TEXT NOT NULL, -- JSON
|
|
result TEXT, -- JSON, written progressively
|
|
error TEXT,
|
|
started_at TEXT,
|
|
finished_at TEXT,
|
|
created_at TEXT NOT NULL,
|
|
updated_at TEXT NOT NULL
|
|
);
|
|
|
|
INSERT INTO jobs_new (id, kind, status, payload, result, error, started_at, finished_at, created_at, updated_at)
|
|
SELECT
|
|
id,
|
|
kind,
|
|
CASE status WHEN 'pending' THEN 'queued' WHEN 'done' THEN 'succeeded' ELSE status END,
|
|
payload, result, error, started_at, finished_at, created_at, updated_at
|
|
FROM jobs;
|
|
|
|
DROP TABLE jobs;
|
|
|
|
ALTER TABLE jobs_new RENAME TO jobs;
|