From f9c852a9c8f5527c306266037e243e709a9d32b5 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 11 Jul 2024 19:17:33 +0100 Subject: [PATCH] feat: perform controlled shutdown on ctrl-c Closes kemitix/git-next#94 Controlled shutdown includes attempting to unregister webhooks. --- crates/server-actor/src/handlers/mod.rs | 1 + crates/server-actor/src/handlers/shutdown.rs | 15 +++++++++++++++ crates/server-actor/src/lib.rs | 2 +- crates/server-actor/src/messages.rs | 2 ++ crates/server/src/lib.rs | 3 ++- 5 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 crates/server-actor/src/handlers/shutdown.rs diff --git a/crates/server-actor/src/handlers/mod.rs b/crates/server-actor/src/handlers/mod.rs index 4c189e5..c92aeeb 100644 --- a/crates/server-actor/src/handlers/mod.rs +++ b/crates/server-actor/src/handlers/mod.rs @@ -1,3 +1,4 @@ mod file_updated; mod receive_server_config; mod receive_valid_server_config; +mod shutdown; diff --git a/crates/server-actor/src/handlers/shutdown.rs b/crates/server-actor/src/handlers/shutdown.rs new file mode 100644 index 0000000..24bb468 --- /dev/null +++ b/crates/server-actor/src/handlers/shutdown.rs @@ -0,0 +1,15 @@ +//- +use actix::prelude::*; +use git_next_webhook_actor::ShutdownWebhook; + +use crate::{messages::Shutdown, Server}; + +impl Handler for Server { + type Result = (); + + fn handle(&mut self, _msg: Shutdown, _ctx: &mut Self::Context) -> Self::Result { + if let Some(webhook) = self.webhook.take() { + webhook.do_send(ShutdownWebhook); + } + } +} diff --git a/crates/server-actor/src/lib.rs b/crates/server-actor/src/lib.rs index d04ff5b..adea345 100644 --- a/crates/server-actor/src/lib.rs +++ b/crates/server-actor/src/lib.rs @@ -3,7 +3,7 @@ mod tests; mod handlers; -mod messages; +pub mod messages; use actix::prelude::*; use git_next_config as config; diff --git a/crates/server-actor/src/messages.rs b/crates/server-actor/src/messages.rs index c7f8f71..adbf46e 100644 --- a/crates/server-actor/src/messages.rs +++ b/crates/server-actor/src/messages.rs @@ -19,3 +19,5 @@ pub struct ValidServerConfig { pub server_storage: ServerStorage, } message!(ReceiveValidServerConfig: ValidServerConfig: "Notification of validated server configuration."); + +message!(Shutdown: "Notification to shutdown the server actor"); diff --git a/crates/server/src/lib.rs b/crates/server/src/lib.rs index 601b0ae..3dee926 100644 --- a/crates/server/src/lib.rs +++ b/crates/server/src/lib.rs @@ -44,7 +44,7 @@ pub fn start( server.do_send(FileUpdated); info!("Starting File Watcher..."); - let fw = match FileWatcher::new("git-next-server.toml".into(), server.recipient()) { + let fw = match FileWatcher::new("git-next-server.toml".into(), server.clone().recipient()) { Ok(fw) => fw, Err(err) => { error!(?err, "Failed to start file watcher"); @@ -56,6 +56,7 @@ pub fn start( info!("Server running - Press Ctrl-C to stop..."); let _ = actix_rt::signal::ctrl_c().await; info!("Ctrl-C received, shutting down..."); + server.do_send(git_next_server_actor::messages::Shutdown); System::current().stop(); }; let system = System::new();