ragequit

Crates.ioragequit
lib.rsragequit
version0.1.1
sourcesrc
created_at2023-01-09 01:04:08.95054
updated_at2023-01-10 08:25:18.421447
descriptionGracefully shut down a process
homepage
repositoryhttps://github.com/MrGunflame/ragequit-rs
max_upload_size
id754023
size26,947
0xc0001a2040 (MrGunflame)

documentation

https://docs.rs/ragequit

README

ragequit

Crates.io Docs.rs

Gracefully shut down a process

ragequit provides a set of utilities to shut down a process. It is primarily targeted at server processes, but may have other applications aswell.

Usage

The global [SHUTDOWN] instance is used to signal shutdown events and handle them gracefully by creating [ShutdownListener]s.

use ragequit::{init, SHUTDOWN};

#[tokio::main]
async fn main() {
    // Install default system signal handlers.
    init();

    let listener = SHUTDOWN.listen();
    tokio::spawn(async move {
        // Wait for the shutdown signal.
        tokio::pin!(listener);
        (&mut listener).await;

        // Drop the listener, allowing the main process to exit.
        println!("Goodbye");
        drop(listener);
    });

    // Wait for a shutdown signal and for all listeners to be dropped.
    SHUTDOWN.wait().await;
}

Call [init] once during the start of the process to install the default system signal handlers. Alternatively you can install system signal handlers yourself.

Example for *nix systems

use core::ffi::c_int;

use nix::sys::signal::{sigaction, SaFlags, SigAction, SigHandler, SigSet, Signal};
use ragequit::SHUTDOWN;

let action = SigAction::new(SigHandler::Handler(quit), SaFlags::empty(), SigSet::empty());

unsafe {
    let _ = sigaction(Signal::SIGINT, &action);
    let _ = sigaction(Signal::SIGTERM, &action);
}

extern "C" fn quit(_: c_int) {
    SHUTDOWN.quit();
}

Tokio dependency

ragequit depends on [tokio] only for synchronization primitives. It does not depend on the tokio runtime. ragequit works in any asynchronous runtime.

License

Licensed under either MIT License or Apache License, Version 2.0 at your option.

Commit count: 13

cargo fmt