Skip to content

Commit a57af72

Browse files
committed
feat: catch SIGTERM gracefully
1 parent d0ba42f commit a57af72

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod args;
33
#[tokio::main]
44
async fn main() {
55
use clap::{CommandFactory, Parser};
6+
use tokio::signal::unix::SignalKind;
67
use tracing::{error, info};
78

89
// Parse command-line arguments.
@@ -45,7 +46,9 @@ async fn main() {
4546
let cxl_child = cxl.clone();
4647
let mut handle = tokio::spawn(async move { cxl_child.cancelled().await });
4748

48-
// Wait for server exit or SIGINT.
49+
// Wait for server exit or SIGTERM/SIGINT.
50+
let mut sigterm = tokio::signal::unix::signal(SignalKind::terminate()).unwrap();
51+
let mut sigint = tokio::signal::unix::signal(SignalKind::interrupt()).unwrap();
4952
tokio::select! {
5053
res = tokio::signal::ctrl_c() => {
5154
res.expect("Failed to register SIGINT hook");
@@ -55,6 +58,8 @@ async fn main() {
5558

5659
handle.await.unwrap();
5760
}
61+
_ = sigterm.recv() => info!("SIGTERM caught, stopping server"),
62+
_ = sigint.recv() => info!("SIGINT caught, stopping server"),
5863
res = &mut handle => {
5964
res.unwrap();
6065
}

0 commit comments

Comments
 (0)