simple-job-queue

Crates.iosimple-job-queue
lib.rssimple-job-queue
version0.4.0
sourcesrc
created_at2023-04-06 06:40:06.391066
updated_at2023-04-07 23:56:23.968086
descriptionA simple job queue
homepagehttps://github.com/affanshahid/simple-job-queue
repositoryhttps://github.com/affanshahid/simple-job-queue
max_upload_size
id831963
size33,563
Affan Shahid (affanshahid)

documentation

https://docs.rs/simple-job-queue

README

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;
}
Commit count: 17

cargo fmt