Expand description
Utilities for building and running workers.
A Worker polls tasks from a backend, executes them using
a service, emits lifecycle events, and handles graceful shutdowns. A worker is typically
constructed using a WorkerBuilder.
§Features
- Pluggable backends for task queues (e.g., in-memory, Redis).
- Middleware support for task processing.
- Stream or future-based worker execution modes.
- Built-in event system for logging or metrics.
- Task tracking and controlled worker readiness.
§Lifecycle
graph TD
A[Start Worker] --> B[Initialize Context & Heartbeat]
B --> C[Poll Backend for Tasks]
C --> D{Task Available?}
D -- Yes --> E[Execute Task via Service Stack]
E --> F[Emit Events]
F --> C
D -- No --> F
F --> G{Shutdown Signal?}
G -- Yes --> H[Graceful Shutdown]
G -- No --> CWorker lifecycle is composed of several stages:
- Initialize context and heartbeat
- Poll backend for tasks
- Execute tasks via service stack
- Emit events (Idle, Success, Error, HeartBeat)
- Graceful shutdown on signal or stop
§Examples
§Run as a future
#[tokio::main]
async fn main() -> Result<(), BoxDynError> {
let mut storage = MemoryStorage::new();
for i in 0..5 {
storage.push(i).await?;
}
async fn handler(task: u32) {
println!("Processing task: {task}");
}
let worker = WorkerBuilder::new("worker-1")
.backend(storage)
.build(handler);
worker.run().await?;
Ok(())
}§Runner as a stream
The stream interface yields worker events (e.g., Success, Error) while running:
let mut stream = worker.stream();
while let Some(evt) = stream.next().await {
println!("Event: {:?}", evt);
}§Test Utilities
The test_worker module includes utilities for unit tests and validation of worker behavior.
Modules§
- builder
- Builder types for composing and building workers.
- call_
all - Utilities for executing all tasks from a stream to a service.
- context
- Worker context and task tracking.
- event
- Event definitions and utility types for worker events
- ext
- Extension traits for building and extending workers
- test_
worker - Provides a worker that allows testing and debugging
Structs§
- Attempt
OnPoll Future - A future that increments the attempt count on the first poll
- Readiness
Service - Service that tracks the readiness of underlying services
- Tracker
Service - Service that tracks a tasks future allowing graceful shutdowns
- Worker
- Core component responsible for task polling, execution, and lifecycle management.