Expand description
PostgreSQL backend factory with shared LISTEN/NOTIFY polling.
PostgresStorageFactory creates independent PostgreSQL backends while
sharing a single PostgreSQL notification listener. Each backend is
registered by its queue name and receives task IDs for newly inserted
jobs belonging to that queue.
This avoids creating a separate PgListener for every worker and allows
multiple queue types to share the same database connection pool.
The factory creates PostgresStorage instances configured with
StreamStrategy. PostgreSQL notifications wake the corresponding
backend, which then performs its normal database polling.
§Example
use apalis::prelude::*;
use apalis_postgres::factory::PostgresStorageFactory;
use sqlx::PgPool;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let pool = PgPool::connect(
&std::env::var("DATABASE_URL")?
).await?;
let mut factory = PostgresStorageFactory::new(pool);
let mut backend = factory.create()?;
backend.push(42).await?;
let worker = WorkerBuilder::new("numbers")
.backend(backend)
.build(|task: u64| async move {
println!("processing {task}");
});
worker.run().await?;
Ok(())
}A factory can also create multiple queues. Each queue is independently notified when a matching job is inserted:
ⓘ
let mut factory = PostgresStorageFactory::new(pool);
let emails = factory.create::<Email>()?;
let reports = factory.create::<Report>()?;Structs§
- Postgres
Storage Factory - A factory for creating PostgreSQL-backed task queues.
- Shared
Fetcher - A stream of task IDs received from the shared PostgreSQL notification listener.
Enums§
- Postgres
Factory Error - Errors returned when creating a PostgreSQL backend from a
PostgresStorageFactory.