pub struct Worker { /* private fields */ }Expand description
Worker for processing jobs from a queue.
Implementations§
Source§impl Worker
impl Worker
Sourcepub fn with_config(queue: Queue, config: WorkerConfig) -> Self
pub fn with_config(queue: Queue, config: WorkerConfig) -> Self
Create a worker with custom configuration.
Sourcepub fn register_handler<F, Fut>(
&mut self,
job_type: impl Into<String>,
handler: F,
)
pub fn register_handler<F, Fut>( &mut self, job_type: impl Into<String>, handler: F, )
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(())
})
});Sourcepub async fn start(&mut self) -> QueueResult<()>
pub async fn start(&mut self) -> QueueResult<()>
Start the worker.
Sourcepub async fn process_batch(
&self,
job_type: &str,
max_batch_size: usize,
) -> QueueResult<Vec<JobId>>
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());Sourcepub fn register_cpu_intensive_handler<F>(
&mut self,
job_type: impl Into<String>,
handler: F,
)
pub fn register_cpu_intensive_handler<F>( &mut self, job_type: impl Into<String>, handler: F, )
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(())
});pub async fn stop(&mut self) -> QueueResult<()>
Sourcepub async fn is_running(&self) -> bool
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more