pub trait JobRunner: Send + Sync {
type Error;
// Required method
fn run(&self, job: Job) -> Result<(), Self::Error>;
}
Expand description
Implementations of this trait can be registered to run jobs in a Consumer
.
§Example
Create a worker with all default options, register a single handler (for the foo
job
type), connect to the Faktory server, and start accepting jobs.
The handler is a struct that implements JobRunner
.
use faktory::{ConsumerBuilder, JobRunner, Job};
use std::io;
struct MyHandler {
config: String,
}
impl JobRunner for MyHandler {
type Error = io::Error;
fn run(&self, job: Job) -> Result<(), Self::Error> {
println!("config: {}", self.config);
println!("job: {:?}", job);
Ok(())
}
}
let mut c = ConsumerBuilder::default();
let handler = MyHandler {
config: "bar".to_string(),
};
c.register_runner("foo", handler);
let mut c = c.connect(None).unwrap();
if let Err(e) = c.run(&["default"]) {
println!("worker failed: {}", e);
}