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
impl ProcessManagerBuilder
Sourcepub fn auto_cleanup(self, enabled: bool) -> Self
pub fn auto_cleanup(self, enabled: bool) -> Self
Enable / disable automatic clean-up of finished children.
Sourcepub fn name<S: Into<Cow<'static, str>>>(self, name: S) -> Self
pub fn name<S: Into<Cow<'static, str>>>(self, name: S) -> Self
Set a custom human-readable name for the supervisor.
Sourcepub fn pre_insert(self, process: impl Runnable) -> Self
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
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}Sourcepub fn build(self) -> ProcessManager
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
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
impl Default for ProcessManagerBuilder
Source§fn default() -> ProcessManagerBuilder
fn default() -> ProcessManagerBuilder
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for ProcessManagerBuilder
impl !RefUnwindSafe for ProcessManagerBuilder
impl Send for ProcessManagerBuilder
impl !Sync for ProcessManagerBuilder
impl Unpin for ProcessManagerBuilder
impl !UnwindSafe for ProcessManagerBuilder
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