Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle standard interrupt signals #375

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading