Worker

Struct Worker 

Source
pub struct Worker { /* private fields */ }
Expand description

Worker for processing jobs from a queue.

Implementations§

Source§

impl Worker

Source

pub fn new(queue: Queue) -> Self

Create a new worker.

Source

pub fn with_config(queue: Queue, config: WorkerConfig) -> Self

Create a worker with custom configuration.

Source

pub fn register_handler<F, Fut>( &mut self, job_type: impl Into<String>, handler: F, )
where F: Fn(Job) -> Fut + Send + Sync + 'static, Fut: Future<Output = QueueResult<()>> + Send + 'static,

Register a job handler.

§Examples
use armature_queue::*;

let queue = Queue::new("redis://localhost:6379", "default").await?;
let mut worker = Worker::new(queue);

worker.register_handler("send_email", |job| {
    Box::pin(async move {
        // Send email logic
        println!("Sending email: {:?}", job.data);
        Ok(())
    })
});
Source

pub async fn start(&mut self) -> QueueResult<()>

Start the worker.

Source

pub async fn process_batch( &self, job_type: &str, max_batch_size: usize, ) -> QueueResult<Vec<JobId>>

Stop the worker. Process multiple jobs of the same type in parallel

This method dequeues and processes multiple jobs of the same type concurrently, providing significant throughput improvements.

§Performance
  • Sequential: O(n * job_time)
  • Parallel: O(max(job_times))
  • Speedup: 3-5x higher throughput
§Examples
// Process up to 10 image processing jobs in parallel
let processed = worker.process_batch("process_image", 10).await?;
println!("Processed {} jobs", processed.len());
Source

pub fn register_cpu_intensive_handler<F>( &mut self, job_type: impl Into<String>, handler: F, )
where F: Fn(Job) -> QueueResult<()> + Send + Sync + 'static,

Register a CPU-intensive handler that runs in blocking thread pool

For CPU-bound operations (image processing, encryption, etc.), use this method to avoid blocking the async runtime.

§Examples
worker.register_cpu_intensive_handler("resize_image", |job| {
    // CPU-intensive work here
    let image_path = job.data["path"].as_str().unwrap();
    // ... resize image ...
    Ok(())
});
Source

pub async fn stop(&mut self) -> QueueResult<()>

Source

pub async fn is_running(&self) -> bool

Check if the worker is running.

Auto Trait Implementations§

§

impl Freeze for Worker

§

impl !RefUnwindSafe for Worker

§

impl Send for Worker

§

impl Sync for Worker

§

impl Unpin for Worker

§

impl !UnwindSafe for Worker

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V