Batch
A distributed task queue library written in Rust using RabbitMQ as a message broker.
This library allows you to send a task to a RabbitMQ broker, so that a worker will be able
to pull it and execute the associated handler. It leverages the futures
and tokio-core
crates to provide asynchronous I/O operations.
Usage
Add this to your Cargo.toml
:
[]
= "0.1"
Note: Task serialization depends on
serde
, so you will have to add it to your project's dependencies as well.
Then add this to your crate root:
#[macro_use]
extern crate batch;
Examples are available on GitHub or you can continue and read the Getting Started guide.
Getting started
The first thing you'll want to do once you've installed batch
is connect to a RabbitMQ broker. We'll start by creating a Client
:
extern crate batch;
extern crate tokio_core;
use ClientBuilder;
use Core;
Now, that we're connected to our broker, we'll create our first task. A task is a work of unit that you want to asynchronously, becuse handling synchronously isn't possible or wouldn't be ideal (e.g sending a mail from a web API). The easiest of creating a task, is by declaring a structure, and derive Task
on it:
extern crate batch;
extern crate serde;
extern crate tokio_core;
use ClientBuilder;
use Core;
Note: you can see that in addition to
Task
, we're also derivingserde
'sSerialize
&Deserialize
traits. This is necessary in order to safely send task over the network.
Note: When deriving
Task
we added the (mandatory)task_routing_key
attribute, it is used by RabbitMQ to deliver your message to the right worker.
Now that we have our task, we can send it to our message broker:
extern crate batch;
extern crate futures;
extern crate serde;
extern crate tokio_core;
use ;
use Future;
use Core;
Now that our task has been published to our broker, we'll need to fetch it and assign a function to this task. To do this, we'll create a new program, the worker:
extern crate batch;
extern crate serde;
extern crate tokio_core;
use ;
use Core;
In order to register our task on the worker, we'll need to make it executable by implementing the Perform
trait:
extern crate batch;
extern crate serde;
extern crate tokio_core;
use ;
use Core;
We can now run our worker program and see the Hello Ferris!
message displayed in the terminal.
Features
codegen
(enabled by default): Automatically re-exports the procedurals macros ofbatch-codegen
from thebatch
crate.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.