diff --git a/event_sidecar/src/event_stream_server/event_indexer.rs b/event_sidecar/src/event_stream_server/event_indexer.rs index b3f3865d..353462e2 100644 --- a/event_sidecar/src/event_stream_server/event_indexer.rs +++ b/event_sidecar/src/event_stream_server/event_indexer.rs @@ -13,6 +13,7 @@ pub(super) struct EventIndexer { } impl EventIndexer { + #![allow(clippy::cognitive_complexity)] pub(super) fn new(storage_path: PathBuf) -> Self { fs::create_dir_all(&storage_path).unwrap_or_else(|err| { error!("Failed to create directory for sse cache: {}", err); @@ -32,6 +33,10 @@ impl EventIndexer { Ok(cached_bytes) => { if cached_bytes.len() == bytes.len() { bytes.copy_from_slice(cached_bytes.as_slice()); + debug!( + file = %persistent_cache.display(), + "successfully read sse index from cache file" + ); } else { warn!( file = %persistent_cache.display(), diff --git a/sidecar/src/run.rs b/sidecar/src/run.rs index fb76a029..c44ac177 100644 --- a/sidecar/src/run.rs +++ b/sidecar/src/run.rs @@ -1,9 +1,10 @@ use crate::component::*; use crate::config::SidecarConfig; -use anyhow::{anyhow, Error}; +use anyhow::{anyhow, Context, Error}; use casper_event_sidecar::LazyDatabaseWrapper; use std::process::ExitCode; -use tracing::info; +use tokio::signal::unix::{signal, SignalKind}; +use tracing::{error, info}; pub async fn run(config: SidecarConfig) -> Result { let maybe_database = config @@ -20,10 +21,28 @@ pub async fn run(config: SidecarConfig) -> Result { components.push(Box::new(sse_server_component)); let rpc_api_component = RpcApiComponent::new(); components.push(Box::new(rpc_api_component)); - do_run(config, components).await.map_err(|component_error| { - info!("The server has exited with an error: {}", component_error); - anyhow!(component_error.to_string()) - }) + + // setup signal handler futures to wait for interrupts + let mut sigterm = + signal(SignalKind::terminate()).context("Failed to initialize SIGTERM handler")?; + let mut sigint = + signal(SignalKind::interrupt()).context("Failed to initialize SIGINT handler")?; + + // run signal handlers and main task + tokio::select! { + _ = sigterm.recv() => { + info!("Received SIGTERM signal. Shutting down..."); + Ok(ExitCode::SUCCESS) + }, + _ = sigint.recv() => { + info!("Received SIGINT signal. Shutting down..."); + Ok(ExitCode::SUCCESS) + }, + res = do_run(config, components) => res.map_err(|component_error| { + error!("The server has exited with an error: {}", component_error); + anyhow!(component_error.to_string()) + }), + } } async fn do_run(