pub struct ProcessManager { /* private fields */ }Expand description
Groups several Runnable instances and starts / stops them as a unit.
Implementations§
Source§impl ProcessManager
impl ProcessManager
Sourcepub fn insert(&mut self, process: impl Runnable)
pub fn insert(&mut self, process: impl Runnable)
Register a child before the supervisor is started.
Panics when called after process_start.
Sourcepub fn add(&self, process: impl Runnable)
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
impl Default for ProcessManager
Source§impl Runnable for ProcessManager
impl Runnable for ProcessManager
Source§fn process_start(&self) -> ProcFuture<'_>
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>
fn process_name(&self) -> Cow<'static, str>
Human-readable name, used for logging only.
Source§fn process_handle(&self) -> Arc<dyn ProcessControlHandler>
fn process_handle(&self) -> Arc<dyn ProcessControlHandler>
Obtain a handle for shutdown / reload signalling.
Auto Trait Implementations§
impl Freeze for ProcessManager
impl !RefUnwindSafe for ProcessManager
impl Send for ProcessManager
impl Sync for ProcessManager
impl Unpin for ProcessManager
impl !UnwindSafe for ProcessManager
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