Skip to main content

Crate boson_backend_postgres

Crate boson_backend_postgres 

Source
Expand description

PostgreSQL QueueBackend for Boson.

When to use: shared durable state for Mode 1 or Mode 2 (enqueue hosts + worker binaries against the same database). Enable via the boson crate postgres feature.

Mode 2 workers should set a unique worker_id and lease_ttl_secs > 0. See the boson crate Mode 2.

§Entry points

§Mode 2 — Enqueue binary

Shared DATABASE_URL with the worker. No claim loop in this process:

use std::sync::Arc;

use boson_backend_postgres::PostgresQueueBackend;
use boson_core::JsonExecutionContextFactory;
use boson_runtime::{configure, Boson};

let url = std::env::var("DATABASE_URL")
    .unwrap_or_else(|_| "postgres://localhost/boson".into());
let backend = PostgresQueueBackend::connect(&url).await?;
let boson = Boson::builder()
    .queue_backend(Arc::new(backend))
    .execution_context_factory(JsonExecutionContextFactory)
    .auto_registry()
    .without_worker()
    .build()?;
configure(boson);
// MyTask::send_with(...).await?;

§Mode 2 — Worker binary

Same database URL, unique worker_id, and lease_ttl_secs > 0:

use std::sync::Arc;

use boson_backend_postgres::PostgresQueueBackend;
use boson_core::JsonExecutionContextFactory;
use boson_runtime::Boson;

let url = std::env::var("DATABASE_URL")
    .unwrap_or_else(|_| "postgres://localhost/boson".into());
let backend = PostgresQueueBackend::connect(&url).await?;
let _boson = Boson::builder()
    .queue_backend(Arc::new(backend))
    .execution_context_factory(JsonExecutionContextFactory)
    .worker_id(std::env::var("BOSON_WORKER_ID").unwrap_or_else(|_| "worker-1".into()))
    .lease_ttl_secs(30)
    .auto_registry()
    .build()?;

Other Mode 2 backends: SQLite, Redis, NATS.

Structs§

PostgresQueueBackend
PostgreSQL-backed queue backend.

Functions§

install_default_postgres_backend
Install a new PostgresQueueBackend as the process-global default backend.
install_isolated_postgres_backend
Install postgres backend with an isolated schema for test sessions.
postgres_test_url
Resolve postgres URL from env (test preferred, then bench, then default).