Monitor

Struct Monitor 

Source
pub struct Monitor { /* private fields */ }
Expand description

A monitor for coordinating and managing a collection of workers.

Implementations§

Source§

impl Monitor

Source

pub fn register<Args, S, B, M>( self, factory: impl Fn(usize) -> Worker<Args, B::Context, B, S, M> + 'static + Send + Sync, ) -> Self
where 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;
Source

pub async fn run_with_signal<S>(self, signal: S) -> Result<(), MonitorError>
where S: Send + Future<Output = Result<()>>,

Runs the monitor and all its registered workers until they have all completed or a shutdown signal is received.

§Arguments
  • signal - A Future that 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.

Source

pub async fn run(self) -> Result<(), MonitorError>

Runs the monitor and all its registered workers until they have all completed.

§Errors

If the monitor fails to run gracefully, an std::io::Error will be returned.

§Remarks

If all workers have completed execution, then by default the monitor will start a shutdown

Source

pub fn on_event<F: Fn(&WorkerContext, &Event) + Send + Sync + 'static>( self, f: F, ) -> Self

Handles all workers’ events emitted

Source§

impl Monitor

Source

pub fn new() -> Self

Creates a new monitor instance.

§Returns

A new monitor instance, with an empty collection of workers.

Source

pub fn shutdown_timeout(self, duration: Duration) -> Self

Available on crate feature sleep only.

Sets a timeout duration for the monitor’s shutdown process.

§Arguments
  • duration - The timeout duration.
§Returns

The monitor instance, with the shutdown timeout duration set.

Source

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.

Source

pub fn should_restart<F>(self, cb: F) -> Self
where F: Fn(&WorkerContext, &WorkerError, usize) -> bool + Send + Sync + 'static,

Allows controlling the restart strategy for workers

Trait Implementations§

Source§

impl Debug for Monitor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Monitor

Source§

fn default() -> Monitor

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more