forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignals.rs
More file actions
86 lines (71 loc) · 2.65 KB
/
signals.rs
File metadata and controls
86 lines (71 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! This module contains functions to handle signals.
use std::time::Duration;
use derive_more::Display;
use tokio::time::sleep;
use tracing::instrument;
/// This is the message that the "launcher" spawned task receives from the main
/// application process to notify the service to shutdown.
///
#[derive(Copy, Clone, Debug, Display)]
pub enum Halted {
Normal,
}
/// Resolves on `ctrl_c` or the `terminate` signal.
///
/// # Panics
///
/// Will panic if the `ctrl_c` or `terminate` signal resolves with an error.
#[instrument(skip())]
pub async fn global_shutdown_signal() {
let ctrl_c = async {
tokio::signal::ctrl_c().await.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
() = ctrl_c => {tracing::warn!("caught interrupt signal (ctrl-c), halting...");},
() = terminate => {tracing::warn!("caught interrupt signal (terminate), halting...");}
}
}
/// Resolves when the `stop_receiver` or the `global_shutdown_signal()` resolves.
///
/// # Panics
///
/// Will panic if the `stop_receiver` resolves with an error.
#[instrument(skip(rx_halt))]
pub async fn shutdown_signal(rx_halt: tokio::sync::oneshot::Receiver<Halted>) {
let halt = async {
match rx_halt.await {
Ok(signal) => signal,
Err(err) => panic!("Failed to install stop signal: {err}"),
}
};
tokio::select! {
signal = halt => { tracing::debug!("Halt signal processed: {}", signal) },
() = global_shutdown_signal() => { tracing::debug!("Global shutdown signal processed") }
}
}
/// Same as `shutdown_signal()`, but shows a message when it resolves.
#[instrument(skip(rx_halt))]
pub async fn shutdown_signal_with_message(rx_halt: tokio::sync::oneshot::Receiver<Halted>, message: String) {
shutdown_signal(rx_halt).await;
tracing::info!("{message}");
}
#[instrument(skip(handle, rx_halt, message))]
pub async fn graceful_shutdown(handle: axum_server::Handle, rx_halt: tokio::sync::oneshot::Receiver<Halted>, message: String) {
shutdown_signal_with_message(rx_halt, message).await;
tracing::debug!("Sending graceful shutdown signal");
handle.graceful_shutdown(Some(Duration::from_secs(90)));
println!("!! shuting down in 90 seconds !!");
loop {
sleep(Duration::from_secs(1)).await;
tracing::info!("remaining alive connections: {}", handle.connection_count());
}
}