Background Task Queues & Cron Scheduler¶
vertebra.framework.queues provides sync and async task queues, retry and dead-letter policies, distributed cron leases, multi-lane worker execution, and pluggable backends for Memory, PostgreSQL, Oracle, Redis, Valkey, and Spanner.
| Component | Purpose |
|---|---|
| Queue Core & Runner | Enqueue typed tasks onto io, cpu, or agent lanes with scoped DI, heartbeats, exponential backoff, and dead-letter retention. |
| Queue Store Protocols | Runtime-checkable persistence contracts implemented by the PostgreSQL, Oracle, and Spanner queue stores. |
| Cron Scheduler | Run recurring cron schedules with slot-keyed distributed leader leases. |
| Storage Backends | Store and claim task envelopes using SKIP LOCKED SQL tables, hash-sharded Spanner transactions, or Redis and Valkey Streams consumer groups. |
Queue Configuration, Store Protocols & Service¶
QueueConfig
¶
Bases: Struct
Configuration for QueueService across memory, Redis, Valkey, PostgreSQL, Oracle, and Spanner backends.
QueueBackendSettings
¶
QueueEntry
¶
SyncQueueStore
¶
AsyncQueueStore
¶
DeadLetterEntry
¶
QueueService
¶
Multi-lane background task queue service supporting pluggable distributed backends.
Attributes:
| Name | Type | Description |
|---|---|---|
config | QueueConfig | QueueBackendSettings | Return the active QueueConfig or QueueBackendSettings. |
backend | QueueBackend | Return the underlying active QueueBackend instance. |
registered_tasks | dict[str, TaskDef] | Return registered task definitions keyed by name. |
container | Container | None | Return the bound Container if configured. |
scheduler | CronScheduler | None | Return the bound CronScheduler if configured. |
enqueue_sync
¶
enqueue_sync(
task_name,
*args,
lane=None,
delay_seconds=None,
scheduled_at=None,
idempotency_key=None,
**kwargs,
)
Synchronously enqueue a task by name with optional scheduling, lane override, and idempotency key.
enqueue
¶
enqueue(
task_name,
*args,
lane=None,
delay_seconds=None,
scheduled_at=None,
idempotency_key=None,
**kwargs,
)
Enqueue a task by name with optional scheduling, lane override, and idempotency key.
enqueue_delayed
¶
Enqueue a task to be executed after delay_seconds elapses.
process_envelope
¶
Execute callable bound to envelope, settling with ack or nack upon completion.
drain_once
¶
Execute ready tasks across selected lanes in FIFO order and return results.
run_next
¶
Pop and execute the next ready task across all lanes, retrying on transient failure.
run_due_schedules
¶
Enqueue and execute all registered cron tasks matching the specified timestamp components.
WorkerRunner
¶
Admission-controlled multi-lane worker runner with QueueService integration and lame-duck drain.
Attributes:
| Name | Type | Description |
|---|---|---|
lanes | dict[str, LaneConfig] | Return configured worker lanes keyed by queue name. |
metrics | dict[str, LaneMetrics] | Return current observability metrics keyed by configured queue name. |
lame_duck | LameDuckController | Return the bound LameDuckController. |
queue_service | QueueService | None | Return the bound QueueService if configured. |
scheduler | CronScheduler | None | Return the bound CronScheduler if configured. |
container | Container | None | Return the bound Container if configured. |
heartbeat_interval_seconds | float | Return the interval in seconds between visibility lease heartbeats. |
reap_interval_seconds | float | Return the interval in seconds between stale-lease reap maintenance sweeps. |
is_stopping | bool | Return whether runner shutdown has commenced. |
submit
¶
Schedule a job onto the specified queue lane bounded by that lane's semaphore.
process_once
¶
Pull and concurrently process a task batch up to configured lane concurrency.
run
¶
Run continuous concurrent pull loop across lanes until stopped.
parse_lanes
¶
Parse comma-separated queue names and concurrency limits into LaneConfig objects.
parse_lane_concurrency
¶
parse_lane_concurrency(
queues, lane_concurrency="", *, default_concurrency=10, drain_timeout_s=30.0
)
Parse comma-separated queue names and lane=limit assignments into LaneConfig objects.
resolve_job
¶
Look up a named job callable on a Litestar or Vertebra app.
resolve_queue_service
¶
Resolve a configured QueueService directly or from target.state['vertebra_queue_service'].
run_oneshot
¶
Enter the application lifespan, run a single named job, and return an exit code.
Cron Scheduling¶
CronSchedule
¶
Bases: Struct
Recurring cron schedule descriptor for background jobs.
Attributes:
| Name | Type | Description |
|---|---|---|
expression | str | |
timezone | str | Default: 'UTC' |
CronScheduler
¶
Distributed lease-elected cron job scheduler dispatching due tasks into QueueService.
Attributes:
| Name | Type | Description |
|---|---|---|
node_id | str | Return the unique scheduler node identifier. |
Queue Backends¶
MemoryQueueBackend
¶
In-memory reference QueueBackend implementation supporting visibility timeout and DLQ.
enqueue_sync
¶
Synchronously enqueue an envelope into scheduled storage or active lane deque.
dequeue
¶
Claim ready tasks across lanes, reclaiming visibility-timed-out unacked tasks first.
nack
¶
Negatively acknowledge task, routing to DLQ when exhausted or re-enqueuing for retry.
promote_scheduled
¶
Promote due scheduled envelopes to active lanes.
acquire_lease
¶
Acquire lease if free, expired, or owned by caller.
renew_lease
¶
Renew lease only if owner_id matches existing holder.
PostgreSQLQueueBackend
¶
RedisQueueBackend
¶
Bases: RESPStreamQueueBackend
Distributed task queue backend powered by Redis Streams and Sorted Sets.
ValkeyQueueBackend
¶
Bases: RESPStreamQueueBackend
Distributed task queue backend powered by Valkey Streams and Sorted Sets.
OracleQueueBackend
¶
SpannerQueueBackend
¶
Bases: _SqlQueueBackendBase
Google Cloud Spanner queue backend over SyncSpannerQueueStore, AsyncSpannerQueueStore, or an executor.