aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-09-28 10:41:59 +0200
committerAlex Auvolat <alex@adnab.me>2022-09-28 10:41:59 +0200
commit1f97ce37e682dff13472d6402f6115e8c1bbb0d7 (patch)
tree89492ec06b5d727858605070d320455eaa9b72c4
parent2197753dfdb25944e55c25d911ae6d14b8506c4d (diff)
downloadgarage-handle-sigterm.tar.gz
garage-handle-sigterm.zip
Shutdown properly on SIGTERM/SIGHUP and on Windows signalshandle-sigterm
-rw-r--r--src/garage/server.rs43
1 files changed, 42 insertions, 1 deletions
diff --git a/src/garage/server.rs b/src/garage/server.rs
index 28710a8e..d4099a97 100644
--- a/src/garage/server.rs
+++ b/src/garage/server.rs
@@ -36,7 +36,7 @@ pub async fn run_server(config_file: PathBuf) -> Result<(), Error> {
let metrics_exporter = opentelemetry_prometheus::exporter().init();
info!("Initializing background runner...");
- let watch_cancel = netapp::util::watch_ctrl_c();
+ let watch_cancel = watch_shutdown_signal();
let (background, await_background_done) = BackgroundRunner::new(16, watch_cancel.clone());
info!("Initializing Garage main data store...");
@@ -157,3 +157,44 @@ pub async fn run_server(config_file: PathBuf) -> Result<(), Error> {
Ok(())
}
+
+#[cfg(unix)]
+fn watch_shutdown_signal() -> watch::Receiver<bool> {
+ use tokio::signal::unix::*;
+
+ let (send_cancel, watch_cancel) = watch::channel(false);
+ tokio::spawn(async move {
+ let mut sigint = signal(SignalKind::interrupt()).expect("Failed to install SIGINT handler");
+ let mut sigterm =
+ signal(SignalKind::terminate()).expect("Failed to install SIGTERM handler");
+ let mut sighup = signal(SignalKind::hangup()).expect("Failed to install SIGHUP handler");
+ tokio::select! {
+ _ = sigint.recv() => info!("Received SIGINT, shutting down."),
+ _ = sigterm.recv() => info!("Received SIGTERM, shutting down."),
+ _ = sighup.recv() => info!("Received SIGHUP, shutting down."),
+ }
+ send_cancel.send(true).unwrap();
+ });
+ watch_cancel
+}
+
+#[cfg(windows)]
+fn watch_shutdown_signal() -> watch::Receiver<bool> {
+ use tokio::signal::windows::*;
+
+ let (send_cancel, watch_cancel) = watch::channel(false);
+ tokio::spawn(async move {
+ let mut sigint = ctrl_c().expect("Failed to install Ctrl-C handler");
+ let mut sigclose = ctrl_close().expect("Failed to install Ctrl-Close handler");
+ let mut siglogoff = ctrl_logoff().expect("Failed to install Ctrl-Logoff handler");
+ let mut sigsdown = ctrl_shutdown().expect("Failed to install Ctrl-Shutdown handler");
+ tokio::select! {
+ _ = sigint.recv() => info!("Received Ctrl-C, shutting down."),
+ _ = sigclose.recv() => info!("Received Ctrl-Close, shutting down."),
+ _ = siglogoff.recv() => info!("Received Ctrl-Logoff, shutting down."),
+ _ = sigsdown.recv() => info!("Received Ctrl-Shutdown, shutting down."),
+ }
+ send_cancel.send(true).unwrap();
+ });
+ watch_cancel
+}