Skip to content

Commit

Permalink
Merge pull request #375 from teonite/handle_unix_signals
Browse files Browse the repository at this point in the history
Handle standard interrupt signals
  • Loading branch information
wojcik91 authored Dec 4, 2024
2 parents 4e2b764 + e558c5c commit a9c6b66
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
5 changes: 5 additions & 0 deletions event_sidecar/src/event_stream_server/event_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(),
Expand Down
31 changes: 25 additions & 6 deletions sidecar/src/run.rs
Original file line number Diff line number Diff line change
@@ -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<ExitCode, Error> {
let maybe_database = config
Expand All @@ -20,10 +21,28 @@ pub async fn run(config: SidecarConfig) -> Result<ExitCode, Error> {
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(
Expand Down

0 comments on commit a9c6b66

Please sign in to comment.