postgres_queue

Crates.iopostgres_queue
lib.rspostgres_queue
version0.1.0
sourcesrc
created_at2023-03-23 14:17:49.18598
updated_at2023-03-23 14:17:49.18598
descriptionA library for managing and executing tasks in a PostgreSQL-backed queue
homepage
repositoryhttps://github.com/fcoury/postgres_queue
max_upload_size
id818354
size49,248
Felipe Coury (fcoury)

documentation

README

postgres_queue

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.

Features

  • Define and register task handlers
  • Enqueue tasks with optional scheduling and intervals
  • Concurrent task processing with adjustable worker count
  • Fault-tolerant task execution with error handling

Usage

Add this to your Cargo.toml:

[dependencies]
postgres_queue = "0.1.0"

Example

Here's a basic example demonstrating how to use the postgres_queue crate:

use postgres_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 = postgres_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(())
}

License

This project is licensed under the MIT License.

Commit count: 7

cargo fmt