pub struct Worker<Args, Backend, Svc, Middleware> { /* private fields */ }Expand description
A worker polls a backend and processes tasks using a service.
Its the core component responsible for task polling, execution, and lifecycle management.
§Example
Basic example:
#[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(())
}See module level documentation for more details.
Implementations§
Source§impl<Args, B, Svc, M> Worker<Args, B, Svc, M>
impl<Args, B, Svc, M> Worker<Args, B, Svc, M>
Sourcepub fn new(context: WorkerContext, backend: B, service: Svc, layers: M) -> Self
pub fn new(context: WorkerContext, backend: B, service: Svc, layers: M) -> Self
Build a worker that is ready for execution
Source§impl<Args, S, M, FB> Worker<Args, FB, S, M>where
FB: BackendConfig + Backend<Task = Task<FB::Args>> + Send + Unpin,
S: Service<Task<FB::Args>> + Send + 'static,
FB::Args: Send + 'static,
Args: Send + 'static,
FB::Error: Into<BoxDynError> + Send + 'static,
M: Layer<LifecycleService<S>>,
FB::Layer: Layer<<M as Layer<LifecycleService<S>>>::Service>,
<FB::Layer as Layer<<M as Layer<LifecycleService<S>>>::Service>>::Service: Service<Task<FB::Args>> + Send + 'static,
<<FB::Layer as Layer<<M as Layer<LifecycleService<S>>>::Service>>::Service as Service<Task<<FB as BackendConfig>::Args>>>::Future: Send,
<<FB::Layer as Layer<<M as Layer<LifecycleService<S>>>::Service>>::Service as Service<Task<<FB as BackendConfig>::Args>>>::Error: Into<BoxDynError> + Send + Sync + 'static,
<<FB::Layer as Layer<<M as Layer<LifecycleService<S>>>::Service>>::Service as Service<Task<<FB as BackendConfig>::Args>>>::Response: Send + Sync + 'static,
impl<Args, S, M, FB> Worker<Args, FB, S, M>where
FB: BackendConfig + Backend<Task = Task<FB::Args>> + Send + Unpin,
S: Service<Task<FB::Args>> + Send + 'static,
FB::Args: Send + 'static,
Args: Send + 'static,
FB::Error: Into<BoxDynError> + Send + 'static,
M: Layer<LifecycleService<S>>,
FB::Layer: Layer<<M as Layer<LifecycleService<S>>>::Service>,
<FB::Layer as Layer<<M as Layer<LifecycleService<S>>>::Service>>::Service: Service<Task<FB::Args>> + Send + 'static,
<<FB::Layer as Layer<<M as Layer<LifecycleService<S>>>::Service>>::Service as Service<Task<<FB as BackendConfig>::Args>>>::Future: Send,
<<FB::Layer as Layer<<M as Layer<LifecycleService<S>>>::Service>>::Service as Service<Task<<FB as BackendConfig>::Args>>>::Error: Into<BoxDynError> + Send + Sync + 'static,
<<FB::Layer as Layer<<M as Layer<LifecycleService<S>>>::Service>>::Service as Service<Task<<FB as BackendConfig>::Args>>>::Response: Send + Sync + 'static,
Sourcepub async fn run(self) -> Result<(), WorkerError>
pub async fn run(self) -> Result<(), WorkerError>
Run the worker until completion
§Example
#[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(())
}Sourcepub async fn run_until<Fut, Err>(self, signal: Fut) -> Result<(), WorkerError>
pub async fn run_until<Fut, Err>(self, signal: Fut) -> Result<(), WorkerError>
Run the worker until a shutdown signal future is complete.
Sourcepub async fn run_with_ctx<F, Fut>(self, fut: F) -> Result<(), WorkerError>where
F: FnMut(WorkerContext) -> Fut + Send + 'static,
Fut: Future<Output = Result<(), WorkerError>> + Send,
FB: Send,
M: Send,
pub async fn run_with_ctx<F, Fut>(self, fut: F) -> Result<(), WorkerError>where
F: FnMut(WorkerContext) -> Fut + Send + 'static,
Fut: Future<Output = Result<(), WorkerError>> + Send,
FB: Send,
M: Send,
Run the worker until a shutdown signal future is complete.
Note: Using this function requires you to call ctx.stop() in the future to completely stop the worker.
This can also be very powerful with pausing and resuming the worker using the context.
§Example
#[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_with_ctx(|ctx| async move {
sleep(Duration::from_secs(1)).await;
ctx.stop()?;
Ok(())
}).await?;
Ok(())
}Sourcepub fn stream(
self,
) -> impl Stream<Item = Result<Event, WorkerError>> + use<Args, S, M, FB>
pub fn stream( self, ) -> impl Stream<Item = Result<Event, WorkerError>> + use<Args, S, M, FB>
Returns a stream that will yield events as they occur within the worker’s lifecycle
§Example
#[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);
let mut stream = worker.stream();
while let Some(evt) = stream.next().await {
println!("Event: {:?}", evt);
}
Ok(())
}Trait Implementations§
Auto Trait Implementations§
impl<Args, Backend, Svc, Middleware> !RefUnwindSafe for Worker<Args, Backend, Svc, Middleware>
impl<Args, Backend, Svc, Middleware> !UnwindSafe for Worker<Args, Backend, Svc, Middleware>
impl<Args, Backend, Svc, Middleware> Freeze for Worker<Args, Backend, Svc, Middleware>
impl<Args, Backend, Svc, Middleware> Send for Worker<Args, Backend, Svc, Middleware>
impl<Args, Backend, Svc, Middleware> Sync for Worker<Args, Backend, Svc, Middleware>
impl<Args, Backend, Svc, Middleware> Unpin for Worker<Args, Backend, Svc, Middleware>
impl<Args, Backend, Svc, Middleware> UnsafeUnpin for Worker<Args, Backend, Svc, Middleware>where
Backend: UnsafeUnpin,
Svc: UnsafeUnpin,
Middleware: UnsafeUnpin,
PhantomData<Args>: UnsafeUnpin,
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