pg_taskq/lib.rs
1//! pg-taskq is a simple postgres-based distributed task queue. It is:
2//!
3//! - pluggable: install it under a custom schema with custom table names, easily uninstall it again
4//! - simple: in postgres there is 1 table, 1 view, 2 plpgsql functions. In Rust there is a task and worker interface
5//! - async: using tokio
6//! - two-way: task can easily wait on being processed, producing output
7//! - hierarchical: tasks can have sub-tasks that get automatically processed bottom-up
8//!
9//! I made this to scratch my own itch to have a flexible, persistent and
10//! distributed task queue for various long-running processing tasks without
11//! having to maintain additional services and infrastructure. This thing is
12//! likely not production ready nor is it battle tested — use at your own risk.
13//!
14//! For a worker-producer example see [this project](./examples/producer-worker-example/).
15
16#[macro_use]
17extern crate tracing;
18
19mod error;
20mod helper;
21mod tables;
22mod task;
23mod task_type;
24mod worker;
25
26pub use error::{Error, Result};
27pub use tables::{TaskTableBuilder, TaskTableProvider, TaskTables};
28pub use task::{Task, TaskBuilder};
29pub use task_type::TaskType;
30pub use worker::Worker;
31
32pub use helper::fixup_stale_tasks;
33
34pub async fn connect(
35 db_uri: impl AsRef<str>,
36 task_table_name: impl AsRef<str>,
37) -> sqlx::Result<(sqlx::PgPool, TaskTables)> {
38 // ensure that the tables used for the task queue exist
39 tracing::info!("setup taskq tables");
40 let pool = sqlx::postgres::PgPoolOptions::new()
41 .max_connections(5)
42 .connect(db_uri.as_ref())
43 .await?;
44 let tables = TaskTableBuilder::new()
45 .base_name(task_table_name.as_ref())
46 .build();
47 tables.create(&pool).await?;
48
49 Ok((pool, tables))
50}