pub struct Monitor { /* private fields */ }Expand description
A monitor for coordinating and managing a collection of workers.
Implementations§
Source§impl Monitor
impl Monitor
Sourcepub fn register<Args, S, B, M>(
self,
factory: impl Fn(usize) -> Worker<Args, B::Context, B, S, M> + 'static + Send + Sync,
) -> Selfwhere
S: Service<Task<Args, B::Context, B::IdType>> + Send + 'static,
S::Future: Send,
S::Error: Send + Sync + 'static + Into<BoxDynError>,
B: Backend<Args = Args> + Send + 'static,
B::Error: Into<BoxDynError> + Send + 'static,
B::Stream: Unpin + Send + 'static,
B::Beat: Unpin + Send,
Args: Send + 'static,
B::Context: Send + 'static,
B::Layer: Layer<ReadinessService<TrackerService<S>>> + 'static,
M: Layer<<<B as Backend>::Layer as Layer<ReadinessService<TrackerService<S>>>>::Service> + 'static,
<M as Layer<<<B as Backend>::Layer as Layer<ReadinessService<TrackerService<S>>>>::Service>>::Service: Service<Task<Args, B::Context, B::IdType>> + Send + 'static,
<<M as Layer<<B::Layer as Layer<ReadinessService<TrackerService<S>>>>::Service>>::Service as Service<Task<Args, B::Context, B::IdType>>>::Future: Send,
<<M as Layer<<B::Layer as Layer<ReadinessService<TrackerService<S>>>>::Service>>::Service as Service<Task<Args, B::Context, B::IdType>>>::Error: Into<BoxDynError> + Send + Sync + 'static,
B::IdType: Send + Sync + 'static,
pub fn register<Args, S, B, M>(
self,
factory: impl Fn(usize) -> Worker<Args, B::Context, B, S, M> + 'static + Send + Sync,
) -> Selfwhere
S: Service<Task<Args, B::Context, B::IdType>> + Send + 'static,
S::Future: Send,
S::Error: Send + Sync + 'static + Into<BoxDynError>,
B: Backend<Args = Args> + Send + 'static,
B::Error: Into<BoxDynError> + Send + 'static,
B::Stream: Unpin + Send + 'static,
B::Beat: Unpin + Send,
Args: Send + 'static,
B::Context: Send + 'static,
B::Layer: Layer<ReadinessService<TrackerService<S>>> + 'static,
M: Layer<<<B as Backend>::Layer as Layer<ReadinessService<TrackerService<S>>>>::Service> + 'static,
<M as Layer<<<B as Backend>::Layer as Layer<ReadinessService<TrackerService<S>>>>::Service>>::Service: Service<Task<Args, B::Context, B::IdType>> + Send + 'static,
<<M as Layer<<B::Layer as Layer<ReadinessService<TrackerService<S>>>>::Service>>::Service as Service<Task<Args, B::Context, B::IdType>>>::Future: Send,
<<M as Layer<<B::Layer as Layer<ReadinessService<TrackerService<S>>>>::Service>>::Service as Service<Task<Args, B::Context, B::IdType>>>::Error: Into<BoxDynError> + Send + Sync + 'static,
B::IdType: Send + Sync + 'static,
Registers a worker into the monitor registry.
§Examples
use apalis_core::monitor::Monitor;
let monitor = Monitor::new();
monitor
.register(|_| {
WorkerBuilder::new("example-worker")
.backend(MemoryStorage::new())
.build(|_: u32| async {})
})
.run()
.await;Sourcepub async fn run_with_signal<S>(self, signal: S) -> Result<(), MonitorError>
pub async fn run_with_signal<S>(self, signal: S) -> Result<(), MonitorError>
Runs the monitor and all its registered workers until they have all completed or a shutdown signal is received.
§Arguments
signal- AFuturethat resolves when a shutdown signal is received.
§Errors
If the monitor fails to shutdown gracefully, an std::io::Error will be returned.
§Remarks
If a timeout has been set using the Monitor::shutdown_timeout method, the monitor
will wait for all workers to complete up to the timeout duration before exiting.
If the timeout is reached and workers have not completed, the monitor will exit forcefully.
Sourcepub async fn run(self) -> Result<(), MonitorError>
pub async fn run(self) -> Result<(), MonitorError>
Source§impl Monitor
impl Monitor
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new monitor instance.
§Returns
A new monitor instance, with an empty collection of workers.
Sourcepub fn shutdown_timeout(self, duration: Duration) -> Self
Available on crate feature sleep only.
pub fn shutdown_timeout(self, duration: Duration) -> Self
sleep only.Sourcepub fn with_terminator(
self,
fut: impl Future<Output = ()> + Send + 'static,
) -> Self
pub fn with_terminator( self, fut: impl Future<Output = ()> + Send + 'static, ) -> Self
Sets a future that will start being polled when the monitor’s shutdown process starts.
After shutdown has been initiated, the terminator future will be run, and if it completes
before all tasks are completed the shutdown process will complete, thus finishing the
shutdown even if there are outstanding tasks. This can be useful for using a timeout or
signal (or combination) to force a full shutdown even if one or more tasks are taking
longer than expected to finish.
Sourcepub fn should_restart<F>(self, cb: F) -> Self
pub fn should_restart<F>(self, cb: F) -> Self
Allows controlling the restart strategy for workers