Skip to content

Commit 191fbac

Browse files
mickvandijkejosecelano
authored andcommitted
refactor: moved signals mod to own file
1 parent 504bad3 commit 191fbac

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

src/lib.rs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod jobs;
55
pub mod logging;
66
pub mod protocol;
77
pub mod setup;
8+
pub mod signals;
89
pub mod stats;
910
pub mod tracker;
1011
pub mod udp;
@@ -30,47 +31,3 @@ pub mod ephemeral_instance_keys {
3031
pub static ref RANDOM_SEED: Seed = Rng::gen(&mut ThreadRng::default());
3132
}
3233
}
33-
34-
pub mod signals {
35-
use log::info;
36-
37-
/// Resolves on `ctrl_c` or the `terminate` signal.
38-
pub async fn global_shutdown_signal() {
39-
let ctrl_c = async {
40-
tokio::signal::ctrl_c().await.expect("failed to install Ctrl+C handler");
41-
};
42-
43-
#[cfg(unix)]
44-
let terminate = async {
45-
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
46-
.expect("failed to install signal handler")
47-
.recv()
48-
.await;
49-
};
50-
51-
#[cfg(not(unix))]
52-
let terminate = std::future::pending::<()>();
53-
54-
tokio::select! {
55-
_ = ctrl_c => {},
56-
_ = terminate => {}
57-
}
58-
}
59-
60-
/// Resolves when the `stop_receiver` or the `global_shutdown_signal()` resolves.
61-
pub async fn shutdown_signal(stop_receiver: tokio::sync::oneshot::Receiver<u8>) {
62-
let stop = async { stop_receiver.await.expect("Failed to install stop signal.") };
63-
64-
tokio::select! {
65-
_ = stop => {},
66-
_ = global_shutdown_signal() => {}
67-
}
68-
}
69-
70-
/// Same as `shutdown_signal()`, but shows a message when it resolves.
71-
pub async fn shutdown_signal_with_message(stop_receiver: tokio::sync::oneshot::Receiver<u8>, message: String) {
72-
shutdown_signal(stop_receiver).await;
73-
74-
info!("{message}");
75-
}
76-
}

src/signals.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use log::info;
2+
3+
/// Resolves on `ctrl_c` or the `terminate` signal.
4+
pub async fn global_shutdown_signal() {
5+
let ctrl_c = async {
6+
tokio::signal::ctrl_c().await.expect("failed to install Ctrl+C handler");
7+
};
8+
9+
#[cfg(unix)]
10+
let terminate = async {
11+
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
12+
.expect("failed to install signal handler")
13+
.recv()
14+
.await;
15+
};
16+
17+
#[cfg(not(unix))]
18+
let terminate = std::future::pending::<()>();
19+
20+
tokio::select! {
21+
_ = ctrl_c => {},
22+
_ = terminate => {}
23+
}
24+
}
25+
26+
/// Resolves when the `stop_receiver` or the `global_shutdown_signal()` resolves.
27+
pub async fn shutdown_signal(stop_receiver: tokio::sync::oneshot::Receiver<u8>) {
28+
let stop = async { stop_receiver.await.expect("Failed to install stop signal.") };
29+
30+
tokio::select! {
31+
_ = stop => {},
32+
_ = global_shutdown_signal() => {}
33+
}
34+
}
35+
36+
/// Same as `shutdown_signal()`, but shows a message when it resolves.
37+
pub async fn shutdown_signal_with_message(stop_receiver: tokio::sync::oneshot::Receiver<u8>, message: String) {
38+
shutdown_signal(stop_receiver).await;
39+
40+
info!("{message}");
41+
}

0 commit comments

Comments
 (0)