ProcessManager

Struct ProcessManager 

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

Groups several Runnable instances and starts / stops them as a unit.

Implementations§

Source§

impl ProcessManager

Source

pub fn new() -> Self

New manager with auto-cleanup of finished children enabled.

Source

pub fn insert(&mut self, process: impl Runnable)

Register a child before the supervisor is started.

Panics when called after process_start.

Source

pub fn add(&self, process: impl Runnable)

Add a child while the manager is already running. The child is spawned immediately. Before start-up this behaves the same as crate::ProcessManager::insert.

Examples found in repository?
examples/dynamic_add.rs (line 97)
68async fn main() {
69    // ------------------------------------------------------------------
70    // 1. Manager with a single initial worker
71    // ------------------------------------------------------------------
72    let mgr = ProcessManagerBuilder::default()
73        .pre_insert(Worker::new(0))
74        .build();
75
76    // We need to keep access to the manager after starting it, therefore
77    // wrap it in an `Arc` and clone it for the spawning task.
78    let mgr: Arc<ProcessManager> = Arc::new(mgr);
79    let mgr_clone = Arc::clone(&mgr);
80
81    tokio::spawn(async move {
82        mgr_clone
83            .process_start()
84            .await
85            .expect("manager encountered an error");
86    });
87
88    let handle = mgr.process_handle();
89
90    // ------------------------------------------------------------------
91    // 2. Dynamically add workers
92    // ------------------------------------------------------------------
93    println!("==> main: sleeping 3 s before adding worker-1");
94    sleep(Duration::from_secs(3)).await;
95
96    println!("==> main: adding worker-1");
97    mgr.add(Worker::new(1));
98
99    println!("==> main: sleeping 2 s");
100    sleep(Duration::from_secs(2)).await;
101
102    println!("==> main: adding worker-2");
103    mgr.add(Worker::new(2));
104
105    // ------------------------------------------------------------------
106    // 3. Graceful shutdown after a short delay
107    // ------------------------------------------------------------------
108    println!("==> main: running 5 s before global shutdown");
109    sleep(Duration::from_secs(5)).await;
110
111    println!("==> main: initiating graceful shutdown");
112    handle.shutdown().await;
113
114    // Allow children to print their exit messages
115    sleep(Duration::from_secs(1)).await;
116}

Trait Implementations§

Source§

impl Default for ProcessManager

Source§

fn default() -> Self

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

impl Runnable for ProcessManager

Source§

fn process_start(&self) -> ProcFuture<'_>

Start the component. The returned future resolves when the process ends (normally or in error).
Source§

fn process_name(&self) -> Cow<'static, str>

Human-readable name, used for logging only.
Source§

fn process_handle(&self) -> Arc<dyn ProcessControlHandler>

Obtain a handle for shutdown / reload signalling.

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, 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.