Skip to content

Commit

Permalink
Error handling for explorer server
Browse files Browse the repository at this point in the history
  • Loading branch information
Kioubit committed Oct 31, 2024
1 parent c0ecb42 commit dafc379
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/modules/explorer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn start_explorer(registry_root: impl AsRef<Path>, port: u16) -> BoxResult<S

let sig_chan_rx_2 = sig_chan_rx.resubscribe();
let app_state_clone = app_state.clone();
let registry_data_updater = tokio::spawn(async move {
let registry_data_updater = tokio::spawn(async move {
loop {
match sig_chan_rx.recv().await.unwrap() {
CustomSignal::Shutdown => {
Expand All @@ -50,10 +50,15 @@ pub fn start_explorer(registry_root: impl AsRef<Path>, port: u16) -> BoxResult<S
}
}
}
Ok(())
});

let server = tokio::spawn(start_server(app_state_clone, port, sig_chan_rx_2));
let result = tokio::try_join!(registry_data_updater, server, signal_listener_handle);
let result = tokio::try_join!(
async {registry_data_updater.await?},
async {server.await?},
async {signal_listener_handle.await?}
);
if let Err(e) = result {
return Err(format!("Error: {}", e));
}
Expand All @@ -64,23 +69,26 @@ pub fn start_explorer(registry_root: impl AsRef<Path>, port: u16) -> BoxResult<S
}


async fn start_server(app_state: Arc<RwLock<AppState>>, port: u16, mut sig_chan_rx: broadcast::Receiver<CustomSignal>) {
async fn start_server(app_state: Arc<RwLock<AppState>>, port: u16, mut sig_chan_rx: broadcast::Receiver<CustomSignal>) -> BoxResult<()> {
let addr = SocketAddr::from((IpAddr::from(Ipv6Addr::UNSPECIFIED), port));
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
let listener = tokio::net::TcpListener::bind(&addr).await
.map_err(|x| format!("Error listening on TCP: {}", x))?;
let app = Router::new()
.route("/", get(handlers::root_handler))
.route("/*path", get(handlers::root_handler))
.route("/api/index/", get(handlers::index_handler))
.route("/api/object/", get(handlers::get_object))
.with_state(app_state);

println!("Starting server on port {}. Send the POSIX 'SIGUSR1' signal to this process to trigger data update", port);
eprintln!("Starting server on port {}. Send the POSIX 'SIGUSR1' signal to this process to trigger data update", port);
axum::serve(listener, app).with_graceful_shutdown(async move {
loop {
match sig_chan_rx.recv().await.unwrap() {
CustomSignal::Shutdown => { break }
CustomSignal::DataUpdate => {}
}
}
}).await.unwrap();
}).await
.map_err(|e| format!("Error starting server: {}", e))?;
Ok(())
}
4 changes: 3 additions & 1 deletion src/modules/explorer/os_signals.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use tokio::select;
use tokio::sync::broadcast;
use crate::modules::explorer::CustomSignal;
use crate::modules::util::BoxResult;

#[cfg(unix)]
pub(super) async fn signal_listener(sig_chan_tx: broadcast::Sender<CustomSignal>) {
pub(super) async fn signal_listener(sig_chan_tx: broadcast::Sender<CustomSignal>) -> BoxResult<()> {
use tokio::signal::unix::{signal, SignalKind};
if let Ok(mut user1_signal) = signal(SignalKind::user_defined1()) {
loop {
Expand All @@ -25,6 +26,7 @@ pub(super) async fn signal_listener(sig_chan_tx: broadcast::Sender<CustomSignal>
}
}
}
Ok(())
}

#[cfg(windows)]
Expand Down

0 comments on commit dafc379

Please sign in to comment.