feat: improved error display when startup fails

This commit is contained in:
Paul Campbell 2024-09-01 13:10:14 +01:00
parent 4160b6d6ee
commit 3c01a822fd
3 changed files with 61 additions and 32 deletions

View file

@ -3,16 +3,17 @@ use actix::prelude::*;
use actix::Recipient;
use anyhow::{Context, Result};
use notify::event::ModifyKind;
use notify::Watcher;
use tracing::error;
use tracing::info;
use notify::{event::ModifyKind, Watcher};
use tracing::{error, info};
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;
use std::{
path::PathBuf,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::Duration,
};
#[derive(Debug, Message)]
#[rtype(result = "()")]
@ -30,8 +31,7 @@ pub fn watch_file(path: PathBuf, recipient: Recipient<FileUpdated>) -> Result<Ar
let mut handler = notify::recommended_watcher(tx).context("file watcher")?;
handler
.watch(&path, notify::RecursiveMode::NonRecursive)
.with_context(|| format!("Watch file: {path:?}"))?;
info!("Watching: {:?}", path);
.with_context(|| format!("Watching: {path:?}"))?;
let thread_shutdown = shutdown.clone();
actix_rt::task::spawn_blocking(move || {
loop {
@ -43,7 +43,7 @@ pub fn watch_file(path: PathBuf, recipient: Recipient<FileUpdated>) -> Result<Ar
match result {
Ok(event) => match event.kind {
notify::EventKind::Modify(ModifyKind::Data(_)) => {
tracing::info!("File modified");
info!("File modified");
recipient.do_send(FileUpdated);
break;
}

View file

@ -53,24 +53,25 @@ fn main() -> Result<()> {
let commands = Commands::parse();
match commands.command {
Command::Init => {
init::run(&fs)?;
}
Command::Init => init::run(&fs),
Command::Server(server) => match server {
Server::Init => {
server::init(&fs)?;
}
Server::Init => server::init(&fs),
#[cfg(not(feature = "tui"))]
Server::Start {} => {
let sleep_duration = std::time::Duration::from_secs(10);
server::start(false, fs, net, repository_factory, sleep_duration)?;
}
Server::Start {} => server::start(
false,
fs,
net,
repository_factory,
std::time::Duration::from_secs(10),
),
#[cfg(feature = "tui")]
Server::Start { ui } => {
let sleep_duration = std::time::Duration::from_secs(10);
server::start(ui, fs, net, repository_factory, sleep_duration)?;
}
Server::Start { ui } => server::start(
ui,
fs,
net,
repository_factory,
std::time::Duration::from_secs(10),
),
},
}
Ok(())
}

View file

@ -19,9 +19,13 @@ use git_next_core::git::RepositoryFactory;
use color_eyre::{eyre::Context, Result};
use kxio::{fs::FileSystem, network::Network};
use tracing::info;
use tracing::{error, info};
use std::{path::PathBuf, sync::atomic::Ordering, time::Duration};
use std::{
path::PathBuf,
sync::{atomic::Ordering, Arc, RwLock},
time::Duration,
};
const A_DAY: Duration = Duration::from_secs(24 * 60 * 60);
@ -57,6 +61,8 @@ pub fn start(
init_logging();
}
let file_watcher_err_channel: Arc<RwLock<Option<anyhow::Error>>> = Arc::new(RwLock::new(None));
let file_watcher_err_channel_exec = file_watcher_err_channel.clone();
let execution = async move {
info!("Starting Alert Dispatcher...");
let alerts_addr = AlertsActor::new(None, History::new(A_DAY), net.clone()).start();
@ -66,9 +72,20 @@ pub fn start(
ServerActor::new(fs.clone(), net.clone(), alerts_addr, repo, sleep_duration).start();
info!("Starting File Watcher...");
#[allow(clippy::expect_used)]
let fw_shutdown = watch_file("git-next-server.toml".into(), server.clone().recipient())
.expect("file watcher");
let watch_file = watch_file("git-next-server.toml".into(), server.clone().recipient());
let fw_shutdown = match watch_file {
Ok(fw_shutdown) => fw_shutdown,
Err(err) => {
// shutdown now
server.do_send(crate::server::actor::messages::Shutdown);
actix_rt::time::sleep(std::time::Duration::from_millis(10)).await;
System::current().stop();
let _ = file_watcher_err_channel_exec
.write()
.map(|mut o| o.replace(err));
return;
}
};
if ui {
#[cfg(feature = "tui")]
@ -106,6 +123,17 @@ pub fn start(
let system = System::new();
Arbiter::current().spawn(execution);
system.run()?;
// check for error from file watcher thread
#[allow(clippy::unwrap_used)]
if let Some(err) = &*file_watcher_err_channel.read().unwrap() {
if ui {
eprintln!("File Watcher: {err:?}");
}
error!(?err, "file watcher");
return Err(color_eyre::eyre::eyre!(format!("{err}")));
}
Ok(())
}