ProcessManagerBuilder

Struct ProcessManagerBuilder 

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

Build-time configuration for a ProcessManager.

let mgr = ProcessManagerBuilder::default()
    .auto_cleanup(true)
    .pre_insert(MySvc)       // add before start
    .build();

Implementations§

Source§

impl ProcessManagerBuilder

Source

pub fn new() -> Self

Create a new builder with defaults (auto_cleanup = true, no children).

Source

pub fn auto_cleanup(self, enabled: bool) -> Self

Enable / disable automatic clean-up of finished children.

Source

pub fn name<S: Into<Cow<'static, str>>>(self, name: S) -> Self

Set a custom human-readable name for the supervisor.

Source

pub fn pre_insert(self, process: impl Runnable) -> Self

Register a child that should be present right from the start.

This is the compile-time safe counterpart to ProcessManager::insert.

Examples found in repository?
examples/simple.rs (line 73)
68async fn main() {
69    // -----------------------------------------------------------
70    // 1. Build a manager and register two workers
71    // -----------------------------------------------------------
72    let manager = ProcessManagerBuilder::default()
73        .pre_insert(Worker::new(0))
74        .pre_insert(Worker::new(1))
75        .build();
76
77    let handle = manager.process_handle();
78
79    // Spawn the supervisor; it will oversee both workers.
80    tokio::spawn(async move {
81        manager
82            .process_start()
83            .await
84            .expect("manager encountered an error");
85    });
86
87    // -----------------------------------------------------------
88    // 2. Let the system run for a short while
89    // -----------------------------------------------------------
90    println!("==> main: sleeping 3 s");
91    sleep(Duration::from_secs(3)).await;
92
93    // -----------------------------------------------------------
94    // 3. Graceful shutdown
95    // -----------------------------------------------------------
96    println!("==> main: initiating graceful shutdown");
97    handle.shutdown().await;
98
99    // Give children time to print their exit messages
100    sleep(Duration::from_secs(1)).await;
101}
More examples
Hide additional examples
examples/dynamic_add.rs (line 73)
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}
Source

pub fn build(self) -> ProcessManager

Finalise the configuration and return a ready-to-use ProcessManager.

Examples found in repository?
examples/simple.rs (line 75)
68async fn main() {
69    // -----------------------------------------------------------
70    // 1. Build a manager and register two workers
71    // -----------------------------------------------------------
72    let manager = ProcessManagerBuilder::default()
73        .pre_insert(Worker::new(0))
74        .pre_insert(Worker::new(1))
75        .build();
76
77    let handle = manager.process_handle();
78
79    // Spawn the supervisor; it will oversee both workers.
80    tokio::spawn(async move {
81        manager
82            .process_start()
83            .await
84            .expect("manager encountered an error");
85    });
86
87    // -----------------------------------------------------------
88    // 2. Let the system run for a short while
89    // -----------------------------------------------------------
90    println!("==> main: sleeping 3 s");
91    sleep(Duration::from_secs(3)).await;
92
93    // -----------------------------------------------------------
94    // 3. Graceful shutdown
95    // -----------------------------------------------------------
96    println!("==> main: initiating graceful shutdown");
97    handle.shutdown().await;
98
99    // Give children time to print their exit messages
100    sleep(Duration::from_secs(1)).await;
101}
More examples
Hide additional examples
examples/dynamic_add.rs (line 74)
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 ProcessManagerBuilder

Source§

fn default() -> ProcessManagerBuilder

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