GithubHelp home page GithubHelp logo

rjq's Introduction

Redis Job Queue

Simple redis job queue

crates.io Build Status

Documentation

https://docs.rs/rjq/

Enqueue jobs

extern crate rjq;

use std::time::Duration;
use std::thread::sleep;
use rjq::Queue;

fn main() {
    let queue = Queue::new("redis://localhost/", "rjq");
    let mut uuids = Vec::new();

    for _ in 0..10 {
        sleep(Duration::from_millis(100));
        uuids.push(queue.enqueue(vec![], 30).unwrap());
    }

    sleep(Duration::from_millis(10000));

    for uuid in uuids.iter() {
        let status = queue.status(uuid).unwrap();
        let result = queue.result(uuid).unwrap().unwrap();
        println!("{} {:?} {}", uuid, status, result);
    }
}

Queue worker

extern crate rjq;

use std::time::Duration;
use std::thread::sleep;
use std::error::Error;
use rjq::Queue;

fn main() {
    fn process(uuid: String, _: Vec<String>) -> Result<String, Box<Error>> {
        sleep(Duration::from_millis(1000));
        println!("{}", uuid);
        Ok(format!("hi from {}", uuid))
    }

    let queue = Queue::new("redis://localhost/", "rjq");
    queue.work(process, Some(1), Some(5), Some(10), Some(30), Some(false), None).unwrap();
}

Job status

QUEUED - job queued for further processing

RUNNING - job is running by worker

LOST - job has not been finished in time

FINISHED - job has been successfully finished

FAILED - job has been failed due to some errors

Queue methods

Init queue

fn new(url: &str, name: &str) -> Queue;

url - redis URL

name - queue name

Returns queue

Drop queue jobs

fn drop(&self) -> Result<(), Box<Error>>;

Enqueue job

fn enqueue(&self, args: Vec<String>, expire: usize) -> Result<String, Box<Error>>;

args - job arguments

expire - if job has not been started by worker in this time (in seconds), it will expire

Returns job UUID

Get job status

fn status(&self, uuid: &str) -> Result<Status, Box<Error>>;

uuid - job unique identifier

Returns job status

Work on queue

fn work<F: Fn(String, Vec<String>) -> Result<String, Box<Error>> + Send + Sync + 'static>
    (&self,
     fun: F,
     wait: Option<usize>,
     timeout: Option<usize>,
     freq: Option<usize>,
     expire: Option<usize>,
     fall: Option<bool>,
     infinite: Option<bool>)
     -> Result<(), Box<Error>>;

fun - worker function

wait - time to wait until next job will pop

timeout - worker function should finish in timeout (in seconds)

freq - job status check frequency (times per second)

expire - job result will expire in this time (in seconds)

fall - panics to terminate process if the job has been lost

infinite - process jobs infinitely one after another, otherwise only one job will be processed

Get job result

fn result(&self, uuid: &str) -> Result<Option<String>, Box<Error>>;

uuid - job unique identifier

Returns job result

Run tests

cargo test

rjq's People

Contributors

neurophant avatar lbolla avatar sd2k avatar petrochenkov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.