fix: shutdown properly on error
All checks were successful
Rust / build (push) Successful in 9m7s
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
Release Please / Release-plz (push) Successful in 2m2s

This commit is contained in:
Paul Campbell 2024-09-03 20:08:12 +01:00
parent b4a4631a1d
commit 5e0cf270dd
3 changed files with 26 additions and 18 deletions

1
Cargo.lock generated
View file

@ -1127,6 +1127,7 @@ dependencies = [
"test-log",
"thiserror",
"time",
"tokio",
"toml",
"tracing",
"tracing-error",

View file

@ -51,6 +51,7 @@ toml = { workspace = true }
# Actors
actix = { workspace = true }
actix-rt = { workspace = true }
tokio = { workspace = true }
# boilerplate
bon = { workspace = true }

View file

@ -20,7 +20,7 @@ use git_next_core::git::RepositoryFactory;
use color_eyre::{eyre::Context, Result};
use kxio::{fs::FileSystem, network::Network};
use tracing::{error, info};
use tracing::info;
use std::{
path::PathBuf,
@ -46,6 +46,7 @@ pub fn init(fs: &FileSystem) -> Result<()> {
Ok(())
}
#[allow(clippy::too_many_lines)]
pub fn start(
ui: bool,
fs: FileSystem,
@ -97,7 +98,6 @@ pub fn start(
use crate::server::actor::messages::SubscribeToUpdates;
use crate::tui;
let (tx_shutdown, rx_shutdown) = channel::<String>();
let tui_addr = tui::Tui::new(tx_shutdown.clone()).start();
server.do_send(SubscribeToUpdates::new(tui_addr.clone().recipient()));
server.do_send(ShutdownTrigger::new(tx_shutdown));
@ -116,18 +116,26 @@ pub fn start(
} else {
server.do_send(ShutdownTrigger::new(tx_shutdown.clone()));
server.do_send(FileUpdated);
info!("Server running - Press Ctrl-C to stop...");
actix_rt::spawn(async move {
let _ = signal::ctrl_c().await;
info!("Ctrl-C received, shutting down...");
let _ = tx_shutdown.send(String::new());
});
if let Ok(message) = rx_shutdown.try_recv() {
let _ = shutdown_message_holder_exec
.write()
.map(|mut o| o.replace(message));
}
info!("Server running - Press Ctrl-C to stop...");
tokio::select! {
_r = signal::ctrl_c() => {
info!("Ctrl-C received, shutting down...");
}
_x = async move {
loop{
if let Ok(message) = rx_shutdown.try_recv() {
let _ = shutdown_message_holder_exec
.write()
.map(|mut o| o.replace(message));
break;
}
actix_rt::task::yield_now().await;
}
} => {
info!("signaled shutdown");
}
};
}
// shutdown
@ -147,10 +155,10 @@ pub fn start(
#[cfg(feature = "tui")]
if ui {
ratatui::restore();
eprintln!("Server: {err:?}");
}
error!(?err, "server");
return Err(color_eyre::eyre::eyre!(format!("{err}")));
if !err.is_empty() {
return Err(color_eyre::eyre::eyre!(format!("{err}")));
}
}
// check for error from file watcher thread
@ -159,9 +167,7 @@ pub fn start(
#[cfg(feature = "tui")]
if ui {
ratatui::restore();
eprintln!("File Watcher: {err:?}");
}
error!(?err, "file watcher");
return Err(color_eyre::eyre::eyre!(format!("{err}")));
}