apollo_framework/
signals.rs

1//! Installs a signal handler which terminates the platform in CTRL+C or SIGHUP.
2//!
3//! Forks an async task which waits for either **CTRL+C** or **SIGHUP** and then invokes
4//! [Platform::terminate](crate::platform::Platform::terminate) on the given platform.
5use std::sync::Arc;
6
7use crate::platform::Platform;
8
9/// Installs a signal handler for the given platform which awaits either a **CTRL+C** or **SIGHUP**.
10#[cfg(not(windows))]
11pub fn install(platform: Arc<Platform>) {
12    let _ = tokio::spawn(async move {
13        let ctrl_c = tokio::signal::ctrl_c();
14        let mut sig_hup =
15            tokio::signal::unix::signal(tokio::signal::unix::SignalKind::hangup()).unwrap();
16
17        tokio::select! {
18            _ = ctrl_c => {
19                log::info!("Received CTRL-C. Shutting down...");
20                platform.terminate();
21            },
22            _ = sig_hup.recv() => {
23               log::info!("Received SIGHUP. Shutting down...");
24                platform.terminate();
25            }
26        }
27    });
28}
29
30/// Installs a signal handler for the given platform which awaits a **CTRL+C**.
31#[cfg(windows)]
32pub fn install(platform: Arc<Platform>) {
33    let _ = tokio::spawn(async move {
34        let ctrl_c = tokio::signal::ctrl_c();
35
36        tokio::select! {
37            _ = ctrl_c => {
38                log::info!("Received CTRL-C. Shutting down...");
39                platform.terminate();
40            },
41        }
42    });
43}