Files
archie/internal/infrastructure/postgres/postgresdb/schedules.sql.go
T
sam 5815c383c6 feat(scheduling): schedules and runs in the State Store with grid timing and run history
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.
2026-10-26 00:49:24 +11:00

572 lines
14 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: schedules.sql
package postgresdb
import (
"context"
"time"
"github.com/jackc/pgx/v5/pgtype"
)
const deleteSchedule = `-- name: DeleteSchedule :execrows
DELETE FROM schedules WHERE id = $1
`
func (q *Queries) DeleteSchedule(ctx context.Context, id string) (int64, error) {
result, err := q.db.Exec(ctx, deleteSchedule, id)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const failStaleRuns = `-- name: FailStaleRuns :exec
UPDATE schedule_runs SET result = 'failed', error = 'no result was recorded', finished_at = $1
WHERE result = 'running' AND started_at < $2
`
type FailStaleRunsParams struct {
FinishedAt pgtype.Timestamptz
StartedAt pgtype.Timestamptz
}
func (q *Queries) FailStaleRuns(ctx context.Context, arg FailStaleRunsParams) error {
_, err := q.db.Exec(ctx, failStaleRuns, arg.FinishedAt, arg.StartedAt)
return err
}
const finishScheduleRun = `-- name: FinishScheduleRun :one
UPDATE schedule_runs SET result = $2, error = $3, finished_at = $4
WHERE id = $1 AND result = 'running'
RETURNING schedule_id
`
type FinishScheduleRunParams struct {
ID int64
Result string
Error string
FinishedAt pgtype.Timestamptz
}
func (q *Queries) FinishScheduleRun(ctx context.Context, arg FinishScheduleRunParams) (string, error) {
row := q.db.QueryRow(ctx, finishScheduleRun,
arg.ID,
arg.Result,
arg.Error,
arg.FinishedAt,
)
var schedule_id string
err := row.Scan(&schedule_id)
return schedule_id, err
}
const getSchedule = `-- name: GetSchedule :one
SELECT id, owner_kind, owner_id, name, enabled, built_in, timing_kind, interval_seconds, cron, run_at, anchor, next_run, zone, created_at, updated_at FROM schedules WHERE id = $1
`
func (q *Queries) GetSchedule(ctx context.Context, id string) (Schedule, error) {
row := q.db.QueryRow(ctx, getSchedule, id)
var i Schedule
err := row.Scan(
&i.ID,
&i.OwnerKind,
&i.OwnerID,
&i.Name,
&i.Enabled,
&i.BuiltIn,
&i.TimingKind,
&i.IntervalSeconds,
&i.Cron,
&i.RunAt,
&i.Anchor,
&i.NextRun,
&i.Zone,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const insertBuiltinSchedule = `-- 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
`
type InsertBuiltinScheduleParams struct {
ID string
Name string
IntervalSeconds int64
Anchor time.Time
NextRun pgtype.Timestamptz
Zone string
}
func (q *Queries) InsertBuiltinSchedule(ctx context.Context, arg InsertBuiltinScheduleParams) error {
_, err := q.db.Exec(ctx, insertBuiltinSchedule,
arg.ID,
arg.Name,
arg.IntervalSeconds,
arg.Anchor,
arg.NextRun,
arg.Zone,
)
return err
}
const insertScheduleRun = `-- 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 id, schedule_id, scheduled_for, started_at, finished_at, result, error, capture_id
`
type InsertScheduleRunParams struct {
ScheduleID string
ScheduledFor time.Time
StartedAt pgtype.Timestamptz
Result string
}
func (q *Queries) InsertScheduleRun(ctx context.Context, arg InsertScheduleRunParams) (ScheduleRun, error) {
row := q.db.QueryRow(ctx, insertScheduleRun,
arg.ScheduleID,
arg.ScheduledFor,
arg.StartedAt,
arg.Result,
)
var i ScheduleRun
err := row.Scan(
&i.ID,
&i.ScheduleID,
&i.ScheduledFor,
&i.StartedAt,
&i.FinishedAt,
&i.Result,
&i.Error,
&i.CaptureID,
)
return i, err
}
const latestScheduleRuns = `-- name: LatestScheduleRuns :many
SELECT DISTINCT ON (schedule_id) id, schedule_id, scheduled_for, started_at, finished_at, result, error, capture_id FROM schedule_runs ORDER BY schedule_id, scheduled_for DESC
`
func (q *Queries) LatestScheduleRuns(ctx context.Context) ([]ScheduleRun, error) {
rows, err := q.db.Query(ctx, latestScheduleRuns)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ScheduleRun
for rows.Next() {
var i ScheduleRun
if err := rows.Scan(
&i.ID,
&i.ScheduleID,
&i.ScheduledFor,
&i.StartedAt,
&i.FinishedAt,
&i.Result,
&i.Error,
&i.CaptureID,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listScheduleRuns = `-- name: ListScheduleRuns :many
SELECT id, schedule_id, scheduled_for, started_at, finished_at, result, error, capture_id FROM schedule_runs WHERE schedule_id = $1 ORDER BY scheduled_for DESC LIMIT $2
`
type ListScheduleRunsParams struct {
ScheduleID string
Limit int32
}
func (q *Queries) ListScheduleRuns(ctx context.Context, arg ListScheduleRunsParams) ([]ScheduleRun, error) {
rows, err := q.db.Query(ctx, listScheduleRuns, arg.ScheduleID, arg.Limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ScheduleRun
for rows.Next() {
var i ScheduleRun
if err := rows.Scan(
&i.ID,
&i.ScheduleID,
&i.ScheduledFor,
&i.StartedAt,
&i.FinishedAt,
&i.Result,
&i.Error,
&i.CaptureID,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listSchedules = `-- name: ListSchedules :many
SELECT id, owner_kind, owner_id, name, enabled, built_in, timing_kind, interval_seconds, cron, run_at, anchor, next_run, zone, created_at, updated_at FROM schedules ORDER BY built_in, name, id
`
// Schedules and their runs (internal/domain/scheduling).
func (q *Queries) ListSchedules(ctx context.Context) ([]Schedule, error) {
rows, err := q.db.Query(ctx, listSchedules)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Schedule
for rows.Next() {
var i Schedule
if err := rows.Scan(
&i.ID,
&i.OwnerKind,
&i.OwnerID,
&i.Name,
&i.Enabled,
&i.BuiltIn,
&i.TimingKind,
&i.IntervalSeconds,
&i.Cron,
&i.RunAt,
&i.Anchor,
&i.NextRun,
&i.Zone,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const lockDueSchedules = `-- name: LockDueSchedules :many
SELECT id, owner_kind, owner_id, name, enabled, built_in, timing_kind, interval_seconds, cron, run_at, anchor, next_run, zone, created_at, updated_at FROM schedules
WHERE enabled AND next_run IS NOT NULL AND next_run <= $1
ORDER BY next_run
FOR UPDATE SKIP LOCKED
`
func (q *Queries) LockDueSchedules(ctx context.Context, nextRun pgtype.Timestamptz) ([]Schedule, error) {
rows, err := q.db.Query(ctx, lockDueSchedules, nextRun)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Schedule
for rows.Next() {
var i Schedule
if err := rows.Scan(
&i.ID,
&i.OwnerKind,
&i.OwnerID,
&i.Name,
&i.Enabled,
&i.BuiltIn,
&i.TimingKind,
&i.IntervalSeconds,
&i.Cron,
&i.RunAt,
&i.Anchor,
&i.NextRun,
&i.Zone,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const lockSchedule = `-- name: LockSchedule :one
SELECT id, owner_kind, owner_id, name, enabled, built_in, timing_kind, interval_seconds, cron, run_at, anchor, next_run, zone, created_at, updated_at FROM schedules WHERE id = $1 FOR UPDATE
`
func (q *Queries) LockSchedule(ctx context.Context, id string) (Schedule, error) {
row := q.db.QueryRow(ctx, lockSchedule, id)
var i Schedule
err := row.Scan(
&i.ID,
&i.OwnerKind,
&i.OwnerID,
&i.Name,
&i.Enabled,
&i.BuiltIn,
&i.TimingKind,
&i.IntervalSeconds,
&i.Cron,
&i.RunAt,
&i.Anchor,
&i.NextRun,
&i.Zone,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const lockStaleZoneCronSchedules = `-- name: LockStaleZoneCronSchedules :many
SELECT id, owner_kind, owner_id, name, enabled, built_in, timing_kind, interval_seconds, cron, run_at, anchor, next_run, zone, created_at, updated_at FROM schedules
WHERE enabled AND timing_kind = 'cron' AND zone <> $1
FOR UPDATE SKIP LOCKED
`
func (q *Queries) LockStaleZoneCronSchedules(ctx context.Context, zone string) ([]Schedule, error) {
rows, err := q.db.Query(ctx, lockStaleZoneCronSchedules, zone)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Schedule
for rows.Next() {
var i Schedule
if err := rows.Scan(
&i.ID,
&i.OwnerKind,
&i.OwnerID,
&i.Name,
&i.Enabled,
&i.BuiltIn,
&i.TimingKind,
&i.IntervalSeconds,
&i.Cron,
&i.RunAt,
&i.Anchor,
&i.NextRun,
&i.Zone,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const pruneScheduleRuns = `-- 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 < $1::timestamptz OR position > $2::bigint
)
`
type PruneScheduleRunsParams struct {
Before time.Time
Keep int64
}
func (q *Queries) PruneScheduleRuns(ctx context.Context, arg PruneScheduleRunsParams) (int64, error) {
result, err := q.db.Exec(ctx, pruneScheduleRuns, arg.Before, arg.Keep)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const recentFinishedResults = `-- name: RecentFinishedResults :many
SELECT result FROM schedule_runs
WHERE schedule_id = $1 AND result IN ('succeeded', 'failed')
ORDER BY scheduled_for DESC
LIMIT $2
`
type RecentFinishedResultsParams struct {
ScheduleID string
Limit int32
}
func (q *Queries) RecentFinishedResults(ctx context.Context, arg RecentFinishedResultsParams) ([]string, error) {
rows, err := q.db.Query(ctx, recentFinishedResults, arg.ScheduleID, arg.Limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []string
for rows.Next() {
var result string
if err := rows.Scan(&result); err != nil {
return nil, err
}
items = append(items, result)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const setScheduleNextRun = `-- name: SetScheduleNextRun :exec
UPDATE schedules SET next_run = $2, zone = $3 WHERE id = $1
`
type SetScheduleNextRunParams struct {
ID string
NextRun pgtype.Timestamptz
Zone string
}
func (q *Queries) SetScheduleNextRun(ctx context.Context, arg SetScheduleNextRunParams) error {
_, err := q.db.Exec(ctx, setScheduleNextRun, arg.ID, arg.NextRun, arg.Zone)
return err
}
const setScheduleRunCapture = `-- name: SetScheduleRunCapture :execrows
UPDATE schedule_runs SET capture_id = $2 WHERE id = $1
`
type SetScheduleRunCaptureParams struct {
ID int64
CaptureID string
}
func (q *Queries) SetScheduleRunCapture(ctx context.Context, arg SetScheduleRunCaptureParams) (int64, error) {
result, err := q.db.Exec(ctx, setScheduleRunCapture, arg.ID, arg.CaptureID)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const startRequestedRuns = `-- 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
`
type StartRequestedRunsRow struct {
ID int64
ScheduleID string
ScheduledFor time.Time
Name string
BuiltIn bool
}
func (q *Queries) StartRequestedRuns(ctx context.Context, startedAt pgtype.Timestamptz) ([]StartRequestedRunsRow, error) {
rows, err := q.db.Query(ctx, startRequestedRuns, startedAt)
if err != nil {
return nil, err
}
defer rows.Close()
var items []StartRequestedRunsRow
for rows.Next() {
var i StartRequestedRunsRow
if err := rows.Scan(
&i.ID,
&i.ScheduleID,
&i.ScheduledFor,
&i.Name,
&i.BuiltIn,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const upsertSchedule = `-- 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 id, owner_kind, owner_id, name, enabled, built_in, timing_kind, interval_seconds, cron, run_at, anchor, next_run, zone, created_at, updated_at
`
type UpsertScheduleParams struct {
ID string
OwnerKind string
OwnerID string
Name string
Enabled bool
BuiltIn bool
TimingKind string
IntervalSeconds int64
Cron string
RunAt pgtype.Timestamptz
Anchor time.Time
NextRun pgtype.Timestamptz
Zone string
}
func (q *Queries) UpsertSchedule(ctx context.Context, arg UpsertScheduleParams) (Schedule, error) {
row := q.db.QueryRow(ctx, upsertSchedule,
arg.ID,
arg.OwnerKind,
arg.OwnerID,
arg.Name,
arg.Enabled,
arg.BuiltIn,
arg.TimingKind,
arg.IntervalSeconds,
arg.Cron,
arg.RunAt,
arg.Anchor,
arg.NextRun,
arg.Zone,
)
var i Schedule
err := row.Scan(
&i.ID,
&i.OwnerKind,
&i.OwnerID,
&i.Name,
&i.Enabled,
&i.BuiltIn,
&i.TimingKind,
&i.IntervalSeconds,
&i.Cron,
&i.RunAt,
&i.Anchor,
&i.NextRun,
&i.Zone,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}