#graceful-shutdown #process #signal #runnable #async-trait #tokio #fault

processmanager

manage process lifecycles, graceful shutdown and process faults

7 releases

0.3.2 Nov 10, 2024
0.3.1 Nov 6, 2024
0.3.0 Sep 16, 2024
0.2.2 Nov 6, 2024
0.1.0 Mar 27, 2024

#536 in Rust patterns

Download history 8/week @ 2024-08-31 282/week @ 2024-09-07 171/week @ 2024-09-14 27/week @ 2024-09-21 13/week @ 2024-09-28 2/week @ 2024-10-05 12/week @ 2024-10-12 52/week @ 2024-10-19 44/week @ 2024-10-26 233/week @ 2024-11-02 179/week @ 2024-11-09 16/week @ 2024-11-16 2/week @ 2024-11-23 37/week @ 2024-11-30

279 downloads per month

MIT license

21KB
375 lines

ProcessManager

Manage multiple running services. A ProcessManager collects impl of Runnable and takes over the runtime management like starting, stopping (graceful or in failure) of services.

If one service fails, the manager initiates a graceful shutdown for all other.

Examples

use processmanager::*;

#[tokio::main]
async fn main() {

    #[derive(Default)]
    struct ExampleController {
        runtime_guard: RuntimeGuard,
    }

    #[async_trait::async_trait]
    impl Runnable for ExampleController {
        async fn process_start(&self) -> Result<(), RuntimeError> {
            // This can be any type of future like an async streams
            let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1));

            loop {
                match self.runtime_guard.tick(interval.tick()).await {
                    ProcessOperation::Next(_) => println!("work"),
                    ProcessOperation::Control(RuntimeControlMessage::Shutdown) => {
                        println!("shutdown"); 
                        break
                    },
                    ProcessOperation::Control(RuntimeControlMessage::Reload) => println!("trigger relead"),
                }
            }

            Ok(())
        }

        fn process_handle(&self) -> Box<dyn ProcessControlHandler> {
            Box::new(self.runtime_guard.handle())
         }
    }

    let mut manager = ProcessManager::new();
    manager.insert(ExampleController::default());

    let handle = manager.process_handle();

    // start all processes
    let _ = tokio::spawn(async move {
        manager.process_start().await.expect("service start failed");
    });

    // Shutdown waits for all services to shutdown gracefully.
    handle.shutdown().await;
}

Dependencies

~3–11MB
~117K SLoC