From e6d435553d0243fb0a6bbb8821438cd0c63db64d Mon Sep 17 00:00:00 2001 From: internationalcrisis Date: Tue, 1 Oct 2024 03:59:48 -0500 Subject: [PATCH] Gracefully shutdown on CTRL+C and SIGTERM Fixes #205 --- src/server.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/server.rs b/src/server.rs index f69d2007..d746cb59 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,5 +1,7 @@ #![allow(dead_code)] +use tokio::signal::unix::{signal, SignalKind}; + use brotli::enc::{BrotliCompress, BrotliEncoderParams}; use cached::proc_macro::cached; use cookie::Cookie; @@ -267,8 +269,13 @@ impl Server { // Bind server to address specified above. Gracefully shut down if CTRL+C is pressed let server = HyperServer::bind(address).serve(make_svc).with_graceful_shutdown(async { - // Wait for the CTRL+C signal - tokio::signal::ctrl_c().await.expect("Failed to install CTRL+C signal handler"); + // Wait for the CTRL+C or SIGTERM + let mut signal_terminate = signal(SignalKind::terminate()).expect("Failed to install SIGTERM signal handler"); + + tokio::select! { + _ = signal_terminate.recv() => (), + _ = tokio::signal::ctrl_c() => () + } }); server.boxed()