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>
38 lines
1.3 KiB
SQL
38 lines
1.3 KiB
SQL
-- name: CreateJob :exec
|
|
INSERT INTO jobs (id, kind, status, payload, created_at, updated_at)
|
|
VALUES (?, ?, 'queued', ?, ?, ?);
|
|
|
|
-- name: ClaimNextJob :one
|
|
-- Marks the oldest queued job running and returns it. Returns sql.ErrNoRows
|
|
-- when no job is queued.
|
|
UPDATE jobs
|
|
SET status = 'running', started_at = sqlc.arg(now), updated_at = sqlc.arg(now)
|
|
WHERE id = (SELECT id FROM jobs WHERE status = 'queued' ORDER BY id LIMIT 1)
|
|
RETURNING *;
|
|
|
|
-- name: PublishJobResult :execrows
|
|
-- Stores a partial result of a running job.
|
|
UPDATE jobs
|
|
SET result = ?, updated_at = ?
|
|
WHERE id = ? AND status = 'running';
|
|
|
|
-- name: SucceedJob :execrows
|
|
UPDATE jobs
|
|
SET status = 'succeeded', result = sqlc.arg(result), finished_at = sqlc.arg(now), updated_at = sqlc.arg(now)
|
|
WHERE id = sqlc.arg(id) AND status = 'running';
|
|
|
|
-- name: FailJob :execrows
|
|
UPDATE jobs
|
|
SET status = 'failed', error = sqlc.arg(error), finished_at = sqlc.arg(now), updated_at = sqlc.arg(now)
|
|
WHERE id = sqlc.arg(id) AND status = 'running';
|
|
|
|
-- name: FailRunningJobs :execrows
|
|
-- Fails every job left running, at boot before any worker starts.
|
|
UPDATE jobs
|
|
SET status = 'failed', error = sqlc.arg(error), finished_at = sqlc.arg(now), updated_at = sqlc.arg(now)
|
|
WHERE status = 'running';
|
|
|
|
-- name: GetJob :one
|
|
SELECT * FROM jobs
|
|
WHERE id = ?;
|