9 unstable releases (3 breaking)

0.4.0 Apr 7, 2023
0.3.2 Apr 7, 2023
0.2.3 Apr 6, 2023
0.1.0 Apr 6, 2023

#2253 in Database interfaces

MIT license

17KB
418 lines

Simple Job Queue

WIP

A simple (and probably very ineffecient) async distributed job queue with configurable backends. Built for my own use-case, use at your own peril. Currently only supports Tokio.

Feature Redis
Job submission
Job processing
Distributed workers
Reseliency
Delayed execution
Retries 🟡

Installation

cargo add simple-job-queue

Usage

use std::time::{Duration, SystemTime, UNIX_EPOCH};

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use simple_job_queue::{
    redis::{RedisJobQueueBackend, RedisJobQueueBackendOptions},
    Job, JobError, JobQueue, JobQueueOptions, Processor,
};

#[derive(Serialize, Deserialize)]
pub struct Data {
    field: i32,
}

pub struct DataProcessor;

#[async_trait]
impl Processor<Data> for DataProcessor {
    async fn process(&mut self, job: &Job<Data>) -> Result<(), JobError> {
        println!("{}", job.data.field);

        Ok(())
    }
}

#[tokio::main]
async fn main() {
    let backend = RedisJobQueueBackend::new(
        "redis://:speakfriendandenter@droplet01.affanshahid.dev",
        "queue_name".to_string(),
        RedisJobQueueBackendOptions::default(),
    )
    .unwrap();

    let mut queue: JobQueue<Data, RedisJobQueueBackend> =
        JobQueue::new(backend, JobQueueOptions::default());

    queue.start(DataProcessor).await.unwrap();
    queue.submit(Job::new(Data { field: 1 })).await.unwrap();

    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("Time went backwards")
        .as_millis();

    queue
        .submit(Job::new_delayed(Data { field: 100 }, now + 10_000))
        .await
        .unwrap();

    tokio::time::sleep(Duration::from_secs(15)).await;
}

Dependencies

~7–17MB
~215K SLoC