Schedules leave the schedules control-plane resource for State Store tables behind a ScheduleService contract. A run is claimed per schedule slot, missed slots fire once, the next run stays on the schedule's grid, cron is read in the schedule-settings time zone, and failed runs are recorded and advance. Three failures in a row raise a schedule.failing alert. A fired run records a schedule-fired capture. Run history is pruned by age and count. Removes the chat and spawn-task runners, the dangerous-payload guard and the old schedules settings page.
88 lines
3.3 KiB
SQL
88 lines
3.3 KiB
SQL
-- Schedules and their runs (internal/domain/scheduling).
|
|
|
|
-- name: ListSchedules :many
|
|
SELECT * FROM schedules ORDER BY built_in, name, id;
|
|
|
|
-- name: LatestScheduleRuns :many
|
|
SELECT DISTINCT ON (schedule_id) * FROM schedule_runs ORDER BY schedule_id, scheduled_for DESC;
|
|
|
|
-- name: GetSchedule :one
|
|
SELECT * FROM schedules WHERE id = $1;
|
|
|
|
-- name: LockSchedule :one
|
|
SELECT * FROM schedules WHERE id = $1 FOR UPDATE;
|
|
|
|
-- name: UpsertSchedule :one
|
|
INSERT INTO schedules (id, owner_kind, owner_id, name, enabled, built_in, timing_kind, interval_seconds, cron, run_at, anchor, next_run, zone)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
|
|
ON CONFLICT (id) DO UPDATE SET
|
|
owner_kind = excluded.owner_kind, owner_id = excluded.owner_id, name = excluded.name,
|
|
enabled = excluded.enabled, timing_kind = excluded.timing_kind, interval_seconds = excluded.interval_seconds,
|
|
cron = excluded.cron, run_at = excluded.run_at, anchor = excluded.anchor, next_run = excluded.next_run,
|
|
zone = excluded.zone, updated_at = now()
|
|
RETURNING *;
|
|
|
|
-- name: InsertBuiltinSchedule :exec
|
|
INSERT INTO schedules (id, owner_kind, owner_id, name, enabled, built_in, timing_kind, interval_seconds, anchor, next_run, zone)
|
|
VALUES ($1, 'instance', '', $2, true, true, 'interval', $3, $4, $5, $6)
|
|
ON CONFLICT (id) DO NOTHING;
|
|
|
|
-- name: DeleteSchedule :execrows
|
|
DELETE FROM schedules WHERE id = $1;
|
|
|
|
-- name: FailStaleRuns :exec
|
|
UPDATE schedule_runs SET result = 'failed', error = 'no result was recorded', finished_at = $1
|
|
WHERE result = 'running' AND started_at < $2;
|
|
|
|
-- name: StartRequestedRuns :many
|
|
UPDATE schedule_runs r SET result = 'running', started_at = $1
|
|
FROM schedules s
|
|
WHERE r.result = 'pending' AND s.id = r.schedule_id
|
|
RETURNING r.id, r.schedule_id, r.scheduled_for, s.name, s.built_in;
|
|
|
|
-- name: LockDueSchedules :many
|
|
SELECT * FROM schedules
|
|
WHERE enabled AND next_run IS NOT NULL AND next_run <= $1
|
|
ORDER BY next_run
|
|
FOR UPDATE SKIP LOCKED;
|
|
|
|
-- name: LockStaleZoneCronSchedules :many
|
|
SELECT * FROM schedules
|
|
WHERE enabled AND timing_kind = 'cron' AND zone <> $1
|
|
FOR UPDATE SKIP LOCKED;
|
|
|
|
-- name: SetScheduleNextRun :exec
|
|
UPDATE schedules SET next_run = $2, zone = $3 WHERE id = $1;
|
|
|
|
-- name: InsertScheduleRun :one
|
|
INSERT INTO schedule_runs (schedule_id, scheduled_for, started_at, result)
|
|
VALUES ($1, $2, $3, $4)
|
|
ON CONFLICT (schedule_id, scheduled_for) DO NOTHING
|
|
RETURNING *;
|
|
|
|
-- name: FinishScheduleRun :one
|
|
UPDATE schedule_runs SET result = $2, error = $3, finished_at = $4
|
|
WHERE id = $1 AND result = 'running'
|
|
RETURNING schedule_id;
|
|
|
|
-- name: RecentFinishedResults :many
|
|
SELECT result FROM schedule_runs
|
|
WHERE schedule_id = $1 AND result IN ('succeeded', 'failed')
|
|
ORDER BY scheduled_for DESC
|
|
LIMIT $2;
|
|
|
|
-- name: SetScheduleRunCapture :execrows
|
|
UPDATE schedule_runs SET capture_id = $2 WHERE id = $1;
|
|
|
|
-- name: ListScheduleRuns :many
|
|
SELECT * FROM schedule_runs WHERE schedule_id = $1 ORDER BY scheduled_for DESC LIMIT $2;
|
|
|
|
-- name: PruneScheduleRuns :execrows
|
|
WITH ranked AS (
|
|
SELECT id AS run_id, finished_at AS ended, row_number() OVER (PARTITION BY schedule_id ORDER BY scheduled_for DESC) AS position
|
|
FROM schedule_runs WHERE result NOT IN ('pending', 'running')
|
|
)
|
|
DELETE FROM schedule_runs WHERE id IN (
|
|
SELECT run_id FROM ranked WHERE ended < sqlc.arg(before)::timestamptz OR position > sqlc.arg(keep)::bigint
|
|
);
|