mod_signal/lib.rs
1//! Unified OS signal handling for Rust.
2//!
3//! `mod-signal` is a runtime-agnostic substrate for cross-platform
4//! signal handling and graceful shutdown orchestration. It provides:
5//!
6//! - A platform-neutral [`Signal`] enum covering SIGTERM, SIGINT,
7//! SIGHUP, SIGQUIT, SIGPIPE, SIGUSR1, SIGUSR2 and their Windows
8//! console-event equivalents.
9//! - A [`Coordinator`] that owns the shutdown state machine and runs
10//! priority-ordered [`ShutdownHook`]s under a configurable timeout
11//! ladder.
12//! - Cloneable [`ShutdownToken`] (observer) and [`ShutdownTrigger`]
13//! (initiator) handles that can be passed independently through a
14//! program's supervision tree.
15//! - Optional runtime adapters for `tokio` and `async-std` exposing
16//! `token.wait().await`.
17//!
18//! # Quick start
19//!
20//! ```no_run
21//! use mod_signal::{Coordinator, ShutdownReason, SignalSet};
22//! use std::time::Duration;
23//!
24//! # #[cfg(feature = "tokio")]
25//! #[tokio::main]
26//! async fn main() -> mod_signal::Result<()> {
27//! let coord = Coordinator::builder()
28//! .signals(SignalSet::graceful())
29//! .graceful_timeout(Duration::from_secs(5))
30//! .hook(mod_signal::hook_from_fn(
31//! "flush-logs",
32//! 100,
33//! |reason| eprintln!("shutting down: {reason}"),
34//! ))
35//! .build();
36//!
37//! coord.install()?;
38//!
39//! let token = coord.token();
40//! token.wait().await;
41//!
42//! let reason = token.reason().unwrap_or(ShutdownReason::Requested);
43//! coord.run_hooks(reason);
44//! Ok(())
45//! }
46//! # #[cfg(not(feature = "tokio"))]
47//! # fn main() {}
48//! ```
49//!
50//! See `.dev/DESIGN.md` and `REPS.md` for the design contract.
51
52#![doc(html_root_url = "https://docs.rs/mod-signal")]
53#![cfg_attr(docsrs, feature(doc_cfg))]
54#![forbid(unsafe_code)]
55#![warn(missing_docs)]
56
57mod coord;
58mod error;
59mod hook;
60mod reason;
61mod signal;
62mod state;
63mod token;
64
65pub use crate::coord::{Coordinator, CoordinatorBuilder, Statistics};
66pub use crate::error::{Error, Result};
67pub use crate::hook::{hook_from_fn, FnHook, ShutdownHook};
68pub use crate::reason::ShutdownReason;
69pub use crate::signal::{Signal, SignalSet, SignalSetIter};
70pub use crate::token::{ShutdownToken, ShutdownTrigger};
71
72/// Crate version string, populated by Cargo at build time.
73pub const VERSION: &str = env!("CARGO_PKG_VERSION");