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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use super::*;
impl Default for ShutdownConfig {
/// Builds the configuration the shutdown plugin uses when none is given.
///
/// # Returns
///
/// - `Self` - The default configuration.
fn default() -> Self {
Self {
watch_terminate: true,
message: MESSAGE_DEFAULT.to_owned(),
}
}
}
impl ShutdownPlugin {
/// Stores the configuration the plugin works from.
///
/// # Arguments
///
/// - `ShutdownConfig` - The configuration to store.
///
/// # Returns
///
/// - `Result<(), String>` - Nothing, or the reason the configuration was
/// refused.
pub fn init(config: ShutdownConfig) -> Result<(), String> {
CONFIG
.set(config)
.map_err(|_: ShutdownConfig| "the shutdown configuration was already set".to_owned())
}
/// The configuration the plugin works from.
///
/// # Returns
///
/// - `Option<&'static ShutdownConfig>` - The configuration, when it was
/// set.
pub fn get() -> Option<&'static ShutdownConfig> {
CONFIG.get()
}
/// Sets the handler that runs when the server stops.
///
/// # Arguments
///
/// - `&ServerControlHookHandler<()>` - The handler to run.
///
/// # Returns
///
/// - `Result<(), String>` - Nothing, or the reason the handler was refused.
pub fn on_shutdown(handler: &ServerControlHookHandler<()>) -> Result<(), String> {
HANDLER
.set(handler.clone())
.map_err(|_: ServerControlHookHandler<()>| {
"the shutdown handler was already set".to_owned()
})
}
/// Stops the server when the process is asked to.
///
/// The watcher runs in the background, so the call returns as soon as the
/// task is started.
///
/// # Arguments
///
/// - `&ServerControlHook` - The hook that owns the server.
///
/// # Returns
///
/// - `Result<(), String>` - Nothing, or the reason the watcher could not
/// be started.
pub fn watch(hook: &ServerControlHook) -> Result<(), String> {
let config: ShutdownConfig = match CONFIG.get() {
Some(config) => config.clone(),
None => ShutdownConfig::default(),
};
let hook: ServerControlHook = hook.clone();
tokio::spawn(async move {
let _: Option<String> = Self::wait(&config, &hook).await;
});
Ok(())
}
/// Waits for a termination signal and stops the server.
///
/// # Arguments
///
/// - `&ShutdownConfig` - The configuration to work from.
/// - `&ServerControlHook` - The hook that owns the server.
///
/// # Returns
///
/// - `Option<String>` - The signal that stopped the server, when one did.
async fn wait(config: &ShutdownConfig, hook: &ServerControlHook) -> Option<String> {
let signal: String = Self::wait_for_signal(config.watch_terminate).await?;
Self::stop(&signal, config, hook).await;
Some(signal)
}
/// Waits for the signal that asks the process to stop.
///
/// # Arguments
///
/// - `bool` - Whether the terminate signal also stops the server.
///
/// # Returns
///
/// - `Option<String>` - The signal that arrived, when one did.
async fn wait_for_signal(watch_terminate: bool) -> Option<String> {
#[cfg(unix)]
{
if watch_terminate {
return Self::wait_for_terminate().await;
}
}
let _: bool = watch_terminate;
Self::wait_for_interrupt().await
}
/// Waits for the interrupt signal.
///
/// # Returns
///
/// - `Option<String>` - The signal that arrived, when one did.
async fn wait_for_interrupt() -> Option<String> {
let interrupt: Result<(), Error> = tokio::signal::ctrl_c().await;
if interrupt.is_err() {
return None;
}
Some(MESSAGE_INTERRUPTED.to_owned())
}
/// Waits for the terminate signal.
///
/// # Returns
///
/// - `Option<String>` - The signal that arrived, when one did.
#[cfg(unix)]
async fn wait_for_terminate() -> Option<String> {
match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
Ok(mut signal) => {
signal.recv().await;
Some(MESSAGE_TERMINATED.to_owned())
}
Err(_) => None,
}
}
/// Runs the handler and stops the server.
///
/// # Arguments
///
/// - `&str` - The signal that asked the process to stop.
/// - `&ShutdownConfig` - The configuration to work from.
/// - `&ServerControlHook` - The hook that owns the server.
async fn stop(signal: &str, config: &ShutdownConfig, hook: &ServerControlHook) {
match HANDLER.get() {
Some(handler) => handler().await,
None => eprintln!("{}: {signal}", config.message),
}
hook.shutdown().await;
}
}