Skip to content

Commit c87be78

Browse files
chore: gracefull shutdown on sigterm
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
1 parent 70e599a commit c87be78

File tree

4 files changed

+61
-17
lines changed

4 files changed

+61
-17
lines changed

Cargo.lock

Lines changed: 41 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ async fn main() -> Result<()> {
2626
app.run_background_tasks();
2727

2828
tokio::select! {
29-
_ = tokio::signal::ctrl_c() => app_copy.shutdown(),
29+
biased;
30+
_ = liwan::utils::signals::shutdown() => app_copy.shutdown(),
3031
res = web::start_webserver(app.clone(), s) => res,
3132
res = tokio::task::spawn_blocking(move || app.clone().events.process(r)) => res?
3233
}

src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod referrer;
55
pub mod refinery_duckdb;
66
pub mod refinery_sqlite;
77
pub mod seed;
8+
pub mod signals;
89
pub mod useragent;
910
pub mod validate;
1011

src/utils/signals.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use eyre::Result;
2+
use tokio::task::JoinSet;
3+
4+
pub async fn shutdown() -> Result<()> {
5+
let mut shutdown = JoinSet::new();
6+
shutdown.spawn(tokio::signal::ctrl_c());
7+
8+
#[cfg(unix)]
9+
shutdown.spawn(async {
10+
let mut sigterm = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?;
11+
sigterm.recv().await;
12+
Ok(())
13+
});
14+
15+
shutdown.join_next().await.expect("there are tasks")??;
16+
Ok(())
17+
}

0 commit comments

Comments
 (0)