forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignal.rs
More file actions
47 lines (41 loc) · 1.4 KB
/
signal.rs
File metadata and controls
47 lines (41 loc) · 1.4 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
use futures::Stream;
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum SignalTo {
/// Signal to reload config.
Reload,
/// Signal to shutdown process.
Shutdown,
/// Shutdown process immediately.
Quit,
}
/// Signals from OS/user.
#[cfg(unix)]
pub fn signals() -> impl Stream<Item = SignalTo> {
use tokio::signal::unix::{signal, SignalKind};
let mut sigint = signal(SignalKind::interrupt()).expect("Signal handlers should not panic.");
let mut sigterm = signal(SignalKind::terminate()).expect("Signal handlers should not panic.");
let mut sigquit = signal(SignalKind::quit()).expect("Signal handlers should not panic.");
let mut sighup = signal(SignalKind::hangup()).expect("Signal handlers should not panic.");
async_stream::stream! {
loop {
let signal = tokio::select! {
_ = sigint.recv() => SignalTo::Shutdown,
_ = sigterm.recv() => SignalTo::Shutdown,
_ = sigquit.recv() => SignalTo::Quit,
_ = sighup.recv() => SignalTo::Reload,
};
yield signal;
}
}
}
/// Signals from OS/user.
#[cfg(windows)]
pub fn signals() -> impl Stream<Item = SignalTo> {
use futures::future::FutureExt;
async_stream::stream! {
loop {
let signal = tokio::signal::ctrl_c().map(|_| SignalTo::Shutdown).await;
yield signal;
}
}
}