Struct brokkr::Worker [−][src]
Worker process.
The worker is responsible for consuming a single queue and processing tasks as they become available. It will will register itself with the backend and send regular status update for monitoring.
The worker itself does not take care of concurrency. In order to process multiple tasks on the same machines you need to start multiple worker processes yourself.
Usage
use std::time::Duration; use brokkr::{Brokkr, Worker, Perform}; #[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] struct Task { name: String } #[derive(Serialize, Deserialize, PartialEq, Eq, Debug)] struct TaskResult; impl Perform for Task { type Result = String; type Error = (); type Context = (); fn process(&self, _: &Self::Context) -> Result<Self::Result, Self::Error> { Ok(format!("Done: {}", self.name).to_owned()) } } let brokkr = Brokkr::new("default".into()); let worker: Worker<Task> = Worker::new( &brokkr, (), Duration::from_millis(500), Duration::from_secs(10), ); worker.process_many(Duration::from_secs(1));
Methods
impl<'a, T: Perform + Clone> Worker<'a, T>
[src]
impl<'a, T: Perform + Clone> Worker<'a, T>
pub fn new(
brokkr: &'a Brokkr,
ctx: T::Context,
timeout: Duration,
result_ttl: Duration
) -> Self
[src]
pub fn new(
brokkr: &'a Brokkr,
ctx: T::Context,
timeout: Duration,
result_ttl: Duration
) -> Self
Create a worker process.
Arguments
brokkr
- The broker to use to communicate with the Redis backend.ctx
- Context object. Use this for things which have costly initialization but should be available to all tasks during procssing.timeout
- Job timeout. All tasks taking more than this duration to process will be marked as failed.result_ttl
- Job result ttl.
pub fn process_one(&self)
[src]
pub fn process_one(&self)
Process a single task from the queue and exit. Exits immediatly if the queue is empty.
Use this to build your own processing loop if process_many
doesn't fit.
pub fn process_many(&self, wait_timeout: Duration)
[src]
pub fn process_many(&self, wait_timeout: Duration)
Process tasks in a loop by polling the queue every wait_timeout
ms.
Arguments
wait_timeout
- Timeout in ms to wait between polls to the queue.