Expand description
PostgreSQL backend for asyncq.
§Quick start
use asyncq::{Queue, Worker, Job, Perform, JobContext, JobResult};
use asyncq_postgres::PostgresBackend;
use serde::{Serialize, Deserialize};
#[derive(Job, Serialize, Deserialize)]
#[job(queue = "emails", retries = 3)]
struct SendEmail { to: String }
impl Perform for SendEmail {
async fn perform(self, _ctx: JobContext) -> JobResult {
println!("Sending to {}", self.to);
Ok(())
}
}
#[tokio::main]
async fn main() {
let backend = PostgresBackend::new("postgres://user:pass@localhost/mydb").await.unwrap();
backend.migrate().await.unwrap();
let queue = Queue::new(backend);
queue.enqueue(SendEmail { to: "user@example.com".into() }).await.unwrap();
Worker::new(queue).register::<SendEmail>().run().await;
}§Transactional outbox
PostgreSQL enables the transactional outbox pattern natively: enqueue a job in the same transaction as your business logic.
ⓘ
let mut tx = pool.begin().await?;
sqlx::query!("INSERT INTO orders ...").execute(&mut *tx).await?;
// Job is enqueued in the same transaction — rolls back if the business logic fails
queue.enqueue_in_tx(
"order.created",
serde_json::json!({"order_id": 1}),
&mut tx,
).await?;
tx.commit().await?;Structs§
- Postgres
Backend - PostgreSQL backend for asyncq.