arbiter-core-0.1.0.0
arbiter-core
Safe HaskellNone
LanguageGHC2024

Arbiter.Core.Job.Schema

Description

Internal to the arbiter packages. Not covered by the PVP.

SQL generation functions for job queue schemas. No database execution happens here.

Synopsis

Name Types

type SchemaName = TextSource #

PostgreSQL schema name, e.g. "arbiter".

type TableName = TextSource #

Unqualified table name within a schema, e.g. "email_jobs".

Schema creation

createSchemaSQL :: SchemaName -> TextSource #

Create the schema arbiter's tables live in.

Table creation and column migrations

createJobQueueTableSQL :: SchemaName -> TableName -> TextSource #

Create a queue's main job table, holding its pending and in-progress jobs.

createJobQueueDLQTableSQL :: SchemaName -> TableName -> TextSource #

Create a queue's DLQ table, where failed jobs land as a full snapshot plus their failure metadata.

createJobQueueArchiveTableSQL :: SchemaName -> TableName -> TextSource #

Create the completed-job archive table. The table is logged.

addTraceContextColumnSQL :: SchemaName -> TableName -> TextSource #

Add the W3C trace-context columns to a queue's three job tables.

addClaimSeqColumnSQL :: SchemaName -> TableName -> TextSource #

Add the per-claim token column to a queue's three job tables.

addKindColumnSQL :: SchemaName -> TableName -> TextSource #

Add the payload variant label to a queue's three job tables.

addClaimedByColumnSQL :: SchemaName -> TableName -> TextSource #

Idempotent migration adding the claimed_by column to a queue's job and DLQ tables.

addCancelRequestedAtColumnSQL :: SchemaName -> TableName -> TextSource #

Idempotent migration adding the cancel_requested_at column to a queue's job table, with a partial index backing the reaper's flagged-job sweep.

addArchiveForColumnSQL :: SchemaName -> TableName -> TextSource #

Idempotent migration adding the archive_for column to a queue's job and DLQ tables.

setMaxAttemptsDefaultSQL :: SchemaName -> TableName -> TextSource #

Backfill NULL max_attempts to the default and set the column default. The column stays nullable for rolling deploys.

Index creation SQL

indexSQL :: Text -> Text -> Text -> Maybe Text -> TextSource #

CREATE INDEX IF NOT EXISTS on the named table, partial under a predicate.

migrateUngroupedReadySplitIndexesSQL :: SchemaName -> TableName -> TextSource #

Replace the full ungrouped ranking index with the ready-only ranking index plus the due-finder.

createDLQGroupKeyIndexSQL :: SchemaName -> TableName -> TextSource #

Index on DLQ group_key, for per-group failure listings.

createDLQFailedAtIndexSQL :: SchemaName -> TableName -> TextSource #

Index on DLQ failed_at, for the most-recent-first listing.

createDLQParentIdIndexSQL :: SchemaName -> TableName -> TextSource #

Index on DLQ parent_id, for per-parent child lookups and counts.

createArchiveCompletedAtIndexSQL :: SchemaName -> TableName -> TextSource #

Index on archive completed_at for the most-recent-first history listing.

createArchiveExpiresAtIndexSQL :: SchemaName -> TableName -> TextSource #

Index on archive archive_expires_at. Drives the retention purge sweep.

createArchiveJobIdIndexSQL :: SchemaName -> TableName -> TextSource #

Index on archive job_id for by-id lookups (getArchiveJobById).

createArchiveParentIdIndexSQL :: SchemaName -> TableName -> TextSource #

Index on archive parent_id for per-tree history lookups.

createArchiveGroupKeyIndexSQL :: SchemaName -> TableName -> TextSource #

Index on archive group_key for per-group history lookups.

createDedupKeyIndexSQL :: SchemaName -> TableName -> TextSource #

Unique index on dedup_key. The dedup ON CONFLICT resolves against it.

createParentIdIndexSQL :: SchemaName -> TableName -> TextSource #

Partial index on parent_id, for per-parent child lookups.

NOTIFY trigger SQL

createNotifyFunctionSQL :: SchemaName -> TableName -> TextSource #

SQL for the per-table NOTIFY function, fired once per insert statement. A statement that inserted nothing notifies nothing. Channel name is quoted as a string literal.

createNotifyTriggerSQL :: SchemaName -> TableName -> TextSource #

A table's job-arrival NOTIFY trigger. Statement-level. A batch insert notifies one time.

Event-streaming trigger SQL

createEventStreamingFunctionSQL :: SchemaName -> TextSource #

Event-streaming function that receives the logical queue name and DLQ flag from each trigger. Queue names ending in _dlq stay unambiguous. A DLQ row reports its original job id. A lease-extend update emits no event.

createEventStreamingTriggersSQL :: SchemaName -> TableName -> TextSource #

Install event-streaming triggers with explicit logical queue metadata.

dropEventStreamingFunctionSQL :: SchemaName -> TextSource #

Drop the schema-wide event-streaming function after its triggers are detached.

Notification channel helpers

notificationChannelForTable :: TableName -> TextSource #

A table's own job-arrival NOTIFY channel: "email_jobs" -> "email_jobs_created".

eventStreamingChannel :: TextSource #

Channel name used by the event streaming (SSE) system.

pauseNotifyChannel :: SchemaName -> Text -> TextSource #

Per-queue NOTIFY channel for pause/resume changes. Workers LISTEN on the channel for their own queue.

pauseNotifyChannelPrefix :: SchemaName -> TextSource #

Prefix for per-queue pause NOTIFY channels. The full channel name appends the queue. SQL templates build the channel from queue_name returned by a CTE.

cancelNotifyChannel :: SchemaName -> Text -> TextSource #

Per-queue NOTIFY channel for force-cancel signals. The payload identifies the target worker and job. Only the matching worker reacts.

cronRunNotifyChannel :: SchemaName -> TextSource #

Per-schema NOTIFY channel for manual cron run-now requests.

Trigger and function name helpers

notifyFunctionName :: TableName -> TextSource #

Per-table NOTIFY trigger function name.

notifyTriggerName :: TableName -> TextSource #

Per-table NOTIFY trigger name.

eventStreamingFunctionName :: TextSource #

Shared event streaming trigger function name (one per schema).

eventStreamingTriggerName :: TableName -> TextSource #

Per-table event streaming trigger name.

eventStreamingDLQTriggerName :: TableName -> TextSource #

Per-table DLQ event streaming trigger name.

legacyEventStreamingTriggers :: [(Text, Bool)]Source #

Event-streaming trigger names the reconcile adopts beside the per-queue names, each paired with whether it sits on the DLQ table.

notifyObjectComment :: TextSource #

Ownership marker stamped on every notify function and trigger arbiter installs. Sweeps match notifyObjectCommentPrefix. A trigger is current when its comment equals this exact value. Bump the version whenever createNotifyTriggerSQL changes.

notifyObjectCommentPrefix :: TextSource #

The marker prefix identifying a notify object as arbiter's, across versions.

notifyAdoptedObjectComment :: TextSource #

Marker the reconcile stamps on an unmarked notify object it adopts. Sweeps match it. An adopted trigger is rebuilt.

eventStreamingObjectComment :: TextSource #

Ownership marker stamped on every event-streaming function and trigger arbiter installs. Bump the version whenever createEventStreamingTriggersSQL changes.

eventStreamingObjectCommentPrefix :: TextSource #

The marker prefix identifying an event-streaming object as arbiter's, across versions.

eventStreamingAdoptedObjectComment :: TextSource #

Marker stamped on event-streaming objects installed before arbiter marked them. See notifyAdoptedObjectComment.

Table name helpers

queueTableNames :: TableName -> [TableName]Source #

A queue's own table and its companions, unqualified and unquoted, for callers that match on pg_catalog relnames.

qualifiedName :: SchemaName -> TableName -> TextSource #

Any schema-qualified name: qualifiedName "arbiter" "arbiter_workers" -> "arbiter"."arbiter_workers"

jobQueueTable :: SchemaName -> TableName -> TextSource #

Qualified table name: jobQueueTable "arbiter" "email_jobs" -> "arbiter"."email_jobs"

jobQueueDLQTable :: SchemaName -> TableName -> TextSource #

Qualified DLQ table name: jobQueueDLQTable "arbiter" "email_jobs" -> "arbiter"."email_jobs_dlq"

jobQueueArchiveTable :: SchemaName -> TableName -> TextSource #

Qualified archive table name: jobQueueArchiveTable "arbiter" "email_jobs" -> "arbiter"."email_jobs_archive"

jobQueueResultsTable :: SchemaName -> TableName -> TextSource #

Qualified results table name: jobQueueResultsTable "arbiter" "email_jobs" -> "arbiter"."email_jobs_results"

jobQueueGroupsTable :: SchemaName -> TableName -> TextSource #

Qualified groups table name: jobQueueGroupsTable "arbiter" "email_jobs" -> "arbiter"."email_jobs_groups"

Results table

createResultsTableSQL :: SchemaName -> TableName -> TextSource #

Create a queue's results table, one row per child keyed by (parent_id, child_id). Its foreign key cascades. Acking the parent clears them.

Maintenance trigger SQL

maintenanceFunctionNames :: SchemaName -> Text -> (Text, Text, Text)Source #

The qualified <baseName>_{insert,delete,update} maintenance-function names.

createMaintenanceTriggersSQL :: SchemaName -> Text -> Text -> TextSource #

The 3 statement-level AFTER triggers (insert/delete/update) wiring a table's maintenance functions, named <baseName>_{insert,delete,update}.

statementTriggerSQL :: SchemaName -> Text -> Text -> Text -> Text -> Text -> TextSource #

One statement-level AFTER trigger. Drops then recreates, wiring the <baseName><suffix> function over tbl with the given event and REFERENCING clause.