-- +goose Up -- Durable jobs that run when they fall due, such as a response deadline. A -- usecase schedules a job in its own business transaction, so the job exists -- exactly when the business change committed. The scheduled job worker -- claims due jobs under a lease and runs each handler in one transaction -- with the completion of its job, so a database-only effect happens once. -- -- Every due and lease time comes from the application clock, never from the -- database clock, so tests can move time without waiting. created_at and -- updated_at are informational database times for operators. -- -- The payload holds identifiers only. Rows are kept after they finish: the -- (kind, dedup_key) pair stays reserved, so a repeated schedule of a job -- that already ran or was canceled does nothing. Retention of finished rows -- is not implemented yet. CREATE TABLE scheduled_jobs ( id bigint GENERATED ALWAYS AS IDENTITY, -- Handler name from the code, for example "invitation.response_deadline". kind text NOT NULL, -- Caller key of the logical job within its kind, for example an -- aggregate ID. It must not contain personal data. dedup_key text NOT NULL, -- Small JSON object of identifiers for the handler. payload jsonb NOT NULL, -- When the job falls due: the deadline it stands for. It never changes, -- so a handler that runs late or on a retry still sees the deadline. due_at timestamptz NOT NULL, -- When a worker may claim the job next: due_at at first, then the -- retry time after a failed attempt. available_at timestamptz NOT NULL, -- pending: waits for available_at and no attempt failed yet; -- failed: an attempt failed and the job waits for its retry at -- available_at; -- running: claimed under a lease; done, dead, cancelled: final. status text NOT NULL, -- Attempts counted so far, including a running one. A claim counts its -- attempt before the handler runs, so a job that crashes the process -- cannot loop forever; a claim whose attempt never started is reused -- by the next claim instead of counted twice. attempts integer NOT NULL, -- NULL means the worker default, JOBS_MAX_ATTEMPTS. max_attempts integer, -- Random ID of the worker instance that holds the lease. lease_owner uuid, lease_expires_at timestamptz, -- When the worker started the handler of the running attempt and -- renewed its lease, by the application clock. NULL while the job is -- claimed but not started, and whenever it is not running. started_at timestamptz, -- Bounded cause of the last failed or abandoned attempt; never text -- from the handler. last_error_class text, created_at timestamptz NOT NULL DEFAULT clock_timestamp(), updated_at timestamptz NOT NULL DEFAULT clock_timestamp(), CONSTRAINT scheduled_jobs_pkey PRIMARY KEY (id), CONSTRAINT scheduled_jobs_kind_dedup_key_key UNIQUE (kind, dedup_key), CONSTRAINT scheduled_jobs_kind_format CHECK ( char_length(kind) <= 64 AND kind ~ '^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)*$' ), CONSTRAINT scheduled_jobs_dedup_key_format CHECK ( char_length(dedup_key) <= 128 AND dedup_key ~ '^[A-Za-z0-9][A-Za-z0-9._:-]*$' ), -- The application allows 2 KiB of compact JSON; the jsonb text form -- adds spaces, so this backstop is wider. CONSTRAINT scheduled_jobs_payload_object CHECK ( jsonb_typeof(payload) = 'object' AND octet_length(payload::text) <= 4096 ), CONSTRAINT scheduled_jobs_status_known CHECK ( status IN ('pending', 'failed', 'running', 'done', 'dead', 'cancelled') ), CONSTRAINT scheduled_jobs_attempts_bounded CHECK ( attempts >= 0 AND (max_attempts IS NULL OR max_attempts BETWEEN 1 AND 100) ), -- Only a running job holds a lease, and a running job always does. CONSTRAINT scheduled_jobs_lease_when_running CHECK ( (status = 'running') = (lease_owner IS NOT NULL AND lease_expires_at IS NOT NULL) AND (lease_owner IS NULL) = (lease_expires_at IS NULL) ), CONSTRAINT scheduled_jobs_running_counted CHECK ( status <> 'running' OR attempts > 0 ), CONSTRAINT scheduled_jobs_started_when_running CHECK ( status = 'running' OR started_at IS NULL ), -- failed differs from pending only by the recorded cause, so an -- operator can tell a first wait from a retry. CONSTRAINT scheduled_jobs_failed_has_cause CHECK ( (status <> 'pending' OR last_error_class IS NULL) AND (status <> 'failed' OR last_error_class IS NOT NULL) ), CONSTRAINT scheduled_jobs_error_class_known CHECK ( last_error_class IN ( 'handler_failed', 'handler_rejected', 'handler_panic', 'timeout', 'conflict', 'effect_rejected', 'lease_expired', 'completion_conflict' ) ) ); -- Serves the claim of due jobs. It covers only waiting rows, so it stays -- small while finished rows accumulate. CREATE INDEX scheduled_jobs_available_idx ON scheduled_jobs (available_at, id) WHERE status IN ('pending', 'failed'); -- Serves the claim of running jobs whose lease expired. CREATE INDEX scheduled_jobs_lease_idx ON scheduled_jobs (lease_expires_at) WHERE status = 'running'; -- Serves the dead job gauge and operator queries; dead jobs are rare. CREATE INDEX scheduled_jobs_dead_idx ON scheduled_jobs (kind, updated_at) WHERE status = 'dead'; -- +goose Down DROP TABLE scheduled_jobs;