Expand description
A library for managing and executing tasks in a PostgreSQL-backed queue.
This library provides a simple way to define, enqueue, and process tasks in a concurrent and fault-tolerant manner using a PostgreSQL database as the task queue.
§Example
use my_task_queue::{TaskRegistry, TaskData, TaskError, connect, initialize_database};
use chrono::{Utc, Duration};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let database_url = "postgres://user:password@localhost/dbname";
let pool = connect(database_url).await?;
initialize_database(&pool).await?;
let mut task_registry = TaskRegistry::new();
task_registry.register_task("my_task", my_task_handler);
let task_data = serde_json::json!({ "message": "Hello, world!" });
let run_at = Utc::now() + Duration::seconds(10);
let task_id = my_task_queue::enqueue(&pool, "my_task", task_data.clone(), run_at, None).await?;
task_registry.run(&pool, 4).await?;
Ok(())
}
async fn my_task_handler(task_id: i32, task_data: TaskData) -> Result<(), TaskError> {
println!("Task {}: {:?}", task_id, task_data);
Ok(())
}
Structs§
- Task
- A struct representing a task in the task queue.
- Task
Registry - A struct for managing a registry of task handlers.
Enums§
- Connection
Error - An enumeration of possible errors that can occur while connecting to the database.
- Task
Error - An enumeration of possible errors that can occur while working with tasks.
Functions§
- complete_
task - Marks a task as complete and reschedules it if it has an interval.
- connect
- Connects to the PostgreSQL database using the provided URL.
- dequeue
- Dequeues a task from the task queue.
- enqueue
- Enqueues a task with the specified parameters.
- fail_
task - Marks a task as failed and stores the error message in the task data.
- initialize_
database - Initializes the task queue database schema.
Type Aliases§
- Task
Data - A type alias for Task Data.
- Task
Handler - A type alias for Task Handler.
- TaskId
- A type alias for Task ID.
- Task
Status - A type alias for Task Status.