#job-processing #background-task #background-jobs #task-queue #background-processing #job #task

job_queue

A simple, efficient Rust library for handling asynchronous job processing and task queuing

20 releases

0.0.20 Dec 29, 2023
0.0.19 Dec 7, 2023
0.0.15 Nov 25, 2023

#11 in #background-jobs

Download history 4/week @ 2024-07-29 36/week @ 2024-09-23 1/week @ 2024-09-30

239 downloads per month
Used in 3 crates (via adrift_core)

MIT/Apache

31KB
789 lines

job_queue

Setup

cargo add job_queue

Usage

Create a job

use job_queue::{Error, Job, typetag, async_trait, serde};

#[derive(Debug, serde::Deserialize, serde::Serialize)]
#[serde(crate = "job_queue::serde")]
pub struct HelloJob {
    pub message: String,
}

#[async_trait::async_trait]
#[typetag::serde]
impl Job for HelloJob {
    async fn handle(&self) -> Result<(), Error> {
        println!("{}", self.message);
        Ok(())
    }
}

Create a queue and dispatch a job


use job_queue::{Error, Job, Queue};

let queue = Client::builder()
    .connect("mysql://root:@localhost/job_queue") // or postgres://root:@localhost/job_queue
    .await?;

queue
    .dispatch(&HelloJob {
        message: "Hello, world!".to_string(),
    })
    .await?;

Create a worker

use job_queue::{Error, Job, Worker};
use std::time::Duration;

let worker = Worker::builder()
        .max_connections(10)
        .worker_count(10)
        .connect("mysql://root:@localhost/job_queue") // or postgres://root:@localhost/job_queue
        .await?;

worker.start().await?; // blocks forever, or until all workers are stopped (crash or ctrl-c)

TODO:

  • emit events, failing, stopping, before and after processing a job

Dependencies

~14–26MB
~385K SLoC