feat: perform controlled shutdown on ctrl-c

Closes kemitix/git-next#94

Controlled shutdown includes attempting to unregister webhooks.
This commit is contained in:
Paul Campbell 2024-07-11 19:17:33 +01:00
parent 681b2c4c10
commit fd762e2bd2
5 changed files with 21 additions and 2 deletions

View file

@ -1,3 +1,4 @@
mod file_updated;
mod receive_server_config;
mod receive_valid_server_config;
mod shutdown;

View file

@ -0,0 +1,15 @@
//-
use actix::prelude::*;
use git_next_webhook_actor::ShutdownWebhook;
use crate::{messages::Shutdown, Server};
impl Handler<Shutdown> 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);
}
}
}

View file

@ -3,7 +3,7 @@
mod tests;
mod handlers;
mod messages;
pub mod messages;
use actix::prelude::*;
use git_next_config as config;

View file

@ -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");

View file

@ -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();