Skip to content

Commit

Permalink
fix: shutdown predicate registration api on Terminate event
Browse files Browse the repository at this point in the history
  • Loading branch information
MicaiahReid committed Mar 12, 2024
1 parent 2ede947 commit 51479c5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
6 changes: 3 additions & 3 deletions components/chainhook-cli/src/service/http_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub async fn start_predicate_api_server(
api_config: PredicatesApiConfig,
observer_commands_tx: Sender<ObserverCommand>,
ctx: Context,
) -> Result<Shutdown, Box<dyn Error>> {
) -> Result<Shutdown, Box<dyn Error + Send + Sync>> {
let log_level = LogLevel::Off;

let mut shutdown_config = config::Shutdown::default();
Expand Down Expand Up @@ -62,12 +62,12 @@ pub async fn start_predicate_api_server(
.ignite()
.await?;

let ingestion_shutdown = ignite.shutdown();
let predicate_api_shutdown = ignite.shutdown();

let _ = std::thread::spawn(move || {
let _ = hiro_system_kit::nestable_block_on(ignite.launch());
});
Ok(ingestion_shutdown)
Ok(predicate_api_shutdown)
}

#[openapi(tag = "Health Check")]
Expand Down
42 changes: 35 additions & 7 deletions components/chainhook-cli/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl Service {

// Enable HTTP Predicates API, if required
let config = self.config.clone();
if let PredicatesApi::On(ref api_config) = config.http_api {
let predicate_api_shutdown = if let PredicatesApi::On(ref api_config) = config.http_api {
info!(
self.ctx.expect_logger(),
"Listening on port {} for chainhook predicate registrations", api_config.http_port
Expand All @@ -211,11 +211,29 @@ impl Service {
let api_config = api_config.clone();
let moved_observer_command_tx = observer_command_tx.clone();
// Test and initialize a database connection
let _ = hiro_system_kit::thread_named("HTTP Predicate API").spawn(move || {
let future = start_predicate_api_server(api_config, moved_observer_command_tx, ctx);
let _ = hiro_system_kit::nestable_block_on(future);
});
}
let res = hiro_system_kit::thread_named("HTTP Predicate API")
.spawn(move || {
let future = start_predicate_api_server(
api_config,
moved_observer_command_tx.clone(),
ctx.clone(),
);
hiro_system_kit::nestable_block_on(future)
})
.expect("unable to spawn thread");
let res = res.join().expect("unable to terminate thread");
match res {
Ok(predicate_api_shutdown) => Some(predicate_api_shutdown),
Err(e) => {
return Err(format!(
"Predicate API Registration server failed to start: {}",
e
));
}
}
} else {
None
};

let observer_event_tx_moved = observer_event_tx.clone();
let moved_observer_command_tx = observer_command_tx.clone();
Expand Down Expand Up @@ -542,7 +560,17 @@ impl Service {
}
}
ObserverEvent::Terminate => {
info!(self.ctx.expect_logger(), "Terminating runloop");
info!(
self.ctx.expect_logger(),
"Terminating ObserverEvent runloop"
);
if let Some(predicate_api_shutdown) = predicate_api_shutdown {
info!(
self.ctx.expect_logger(),
"Terminating Predicate Registration API"
);
predicate_api_shutdown.notify();
}
break;
}
_ => {}
Expand Down

0 comments on commit 51479c5

Please sign in to comment.