pub struct Worker<Args, Ctx, Backend, Svc, Middleware> { /* private fields */ }Expand description
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, S, B, M> Worker<Args, B::Context, B, S, M>where
B: Backend<Args>,
S: Service<Task<Args, B::Context, B::IdType>> + Send + 'static,
B::Stream: Unpin + Send + 'static,
B::Beat: Unpin + Send + 'static,
Args: Send + 'static,
B::Context: Send + 'static,
B::Error: Into<BoxDynError> + Send + 'static,
M: Layer<ReadinessService<TrackerService<S>>>,
B::Layer: Layer<M::Service>,
<B::Layer as Layer<M::Service>>::Service: Service<Task<Args, B::Context, B::IdType>> + Send + 'static,
<<B::Layer as Layer<M::Service>>::Service as Service<Task<Args, B::Context, B::IdType>>>::Error: Into<BoxDynError> + Send + Sync + 'static,
<<B::Layer as Layer<M::Service>>::Service as Service<Task<Args, B::Context, B::IdType>>>::Future: Send,
M::Service: Service<Task<Args, B::Context, B::IdType>> + Send + 'static,
<<M as Layer<ReadinessService<TrackerService<S>>>>::Service as Service<Task<Args, B::Context, B::IdType>>>::Future: Send,
<<M as Layer<ReadinessService<TrackerService<S>>>>::Service as Service<Task<Args, B::Context, B::IdType>>>::Error: Into<BoxDynError> + Send + Sync + 'static,
B::IdType: Send + 'static,
impl<Args, S, B, M> Worker<Args, B::Context, B, S, M>where
B: Backend<Args>,
S: Service<Task<Args, B::Context, B::IdType>> + Send + 'static,
B::Stream: Unpin + Send + 'static,
B::Beat: Unpin + Send + 'static,
Args: Send + 'static,
B::Context: Send + 'static,
B::Error: Into<BoxDynError> + Send + 'static,
M: Layer<ReadinessService<TrackerService<S>>>,
B::Layer: Layer<M::Service>,
<B::Layer as Layer<M::Service>>::Service: Service<Task<Args, B::Context, B::IdType>> + Send + 'static,
<<B::Layer as Layer<M::Service>>::Service as Service<Task<Args, B::Context, B::IdType>>>::Error: Into<BoxDynError> + Send + Sync + 'static,
<<B::Layer as Layer<M::Service>>::Service as Service<Task<Args, B::Context, B::IdType>>>::Future: Send,
M::Service: Service<Task<Args, B::Context, B::IdType>> + Send + 'static,
<<M as Layer<ReadinessService<TrackerService<S>>>>::Service as Service<Task<Args, B::Context, B::IdType>>>::Future: Send,
<<M as Layer<ReadinessService<TrackerService<S>>>>::Service as Service<Task<Args, B::Context, B::IdType>>>::Error: Into<BoxDynError> + Send + Sync + 'static,
B::IdType: Send + '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_with_ctx(
self,
ctx: &mut WorkerContext,
) -> Result<(), WorkerError>
pub async fn run_with_ctx( self, ctx: &mut WorkerContext, ) -> Result<(), WorkerError>
Run the worker with the given context.
See run for an example.
Sourcepub fn stream(
self,
) -> impl Stream<Item = Result<Event, WorkerError>> + use<Args, S, B, M>
pub fn stream( self, ) -> impl Stream<Item = Result<Event, WorkerError>> + use<Args, S, B, M>
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(())
}Sourcepub fn stream_with_ctx(
self,
ctx: &mut WorkerContext,
) -> impl Stream<Item = Result<Event, WorkerError>> + use<Args, S, B, M>
pub fn stream_with_ctx( self, ctx: &mut WorkerContext, ) -> impl Stream<Item = Result<Event, WorkerError>> + use<Args, S, B, M>
Returns a stream that will yield events as they occur within the worker’s lifecycle when provided
with a WorkerContext.
See stream for an example.
Trait Implementations§
Auto Trait Implementations§
impl<Args, Ctx, Backend, Svc, Middleware> Freeze for Worker<Args, Ctx, Backend, Svc, Middleware>
impl<Args, Ctx, Backend, Svc, Middleware> !RefUnwindSafe for Worker<Args, Ctx, Backend, Svc, Middleware>
impl<Args, Ctx, Backend, Svc, Middleware> Send for Worker<Args, Ctx, Backend, Svc, Middleware>
impl<Args, Ctx, Backend, Svc, Middleware> Sync for Worker<Args, Ctx, Backend, Svc, Middleware>
impl<Args, Ctx, Backend, Svc, Middleware> Unpin for Worker<Args, Ctx, Backend, Svc, Middleware>
impl<Args, Ctx, Backend, Svc, Middleware> !UnwindSafe for Worker<Args, Ctx, Backend, Svc, Middleware>
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