3 releases (breaking)

Uses old Rust 2015

0.3.0 Apr 8, 2018
0.2.0 Apr 7, 2018
0.1.0 Apr 4, 2018

#33 in #background-jobs

MIT license

36KB
701 lines

This is the readme for the main Robin crate. You can find help at our GitHub repo.


lib.rs:

Robin

Robin lets you run jobs in the background. This could for example be payment processing or sending emails.

If you've used ActiveJob from Ruby on Rails you'll feel right at home.

Getting started

The standard way to use Robin is through the jobs! macro. It takes a comma separated list of job names, and generates all the boilerplate for you. Just you have to define a static method named perform on each of your jobs.

Here is a full example:

#[macro_use]
extern crate robin;
#[macro_use]
extern crate serde_derive;
#
#
#
use robin::prelude::*;

jobs! {
    MyJob,
}

impl MyJob {
    fn perform(args: JobArgs, _con: &WorkerConnection) -> JobResult {
        println!("Job performed with {:?}", args);
        Ok(())
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct JobArgs;

let config = Config::default();

let con = robin_establish_connection!(config)?;

assert_eq!(con.main_queue_size()?, 0);
assert_eq!(con.retry_queue_size()?, 0);

for i in 0..5 {
    MyJob::perform_later(&JobArgs, &con)?;
}

assert_eq!(con.main_queue_size()?, 5);
assert_eq!(con.retry_queue_size()?, 0);

robin_boot_worker!(config);

assert_eq!(con.main_queue_size()?, 0);
assert_eq!(con.retry_queue_size()?, 0);

Normally the code that enqueues jobs and the code the boots the worker would be in separate binaries.

For more details see the robin::macros module documentation.

The prelude

Robin provides a prelude module which exports all the commonly used types and traits. Code using Robin is expected to have:

use robin::prelude::*;

Reexports the most commonly used types and traits from the other modules. As long as you're doing standard things this is the only use you'll need.

Dependencies

~3.5–5MB
~123K SLoC