Skip to content

Commit 0d14a74

Browse files
committed
fix: [#626] healt check api server shutdown
This fixes: - The error: "Failed to install stop signal: channel closed" - And CRTL+C to shutdown the service
1 parent bbf1be6 commit 0d14a74

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/bootstrap/jobs/health_check_api.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,32 @@ pub async fn start_job(config: &HealthCheckApi, register: ServiceRegistry) -> Jo
4242

4343
let (tx_start, rx_start) = oneshot::channel::<Started>();
4444
let (tx_halt, rx_halt) = tokio::sync::oneshot::channel::<Halted>();
45-
drop(tx_halt);
45+
46+
let protocol = "http";
4647

4748
// Run the API server
4849
let join_handle = tokio::spawn(async move {
49-
info!(target: "Health Check API", "Starting on: http://{}", bind_addr);
50+
info!(target: "Health Check API", "Starting on: {protocol}://{}", bind_addr);
5051

5152
let handle = server::start(bind_addr, tx_start, rx_halt, register);
5253

5354
if let Ok(()) = handle.await {
54-
info!(target: "Health Check API", "Stopped server running on: http://{}", bind_addr);
55+
info!(target: "Health Check API", "Stopped server running on: {protocol}://{}", bind_addr);
5556
}
5657
});
5758

58-
// Wait until the API server job is running
59+
// Wait until the server sends the started message
5960
match rx_start.await {
60-
Ok(msg) => info!(target: "Health Check API", "Started on: http://{}", msg.address),
61+
Ok(msg) => info!(target: "Health Check API", "Started on: {protocol}://{}", msg.address),
6162
Err(e) => panic!("the Health Check API server was dropped: {e}"),
6263
}
6364

64-
join_handle
65+
// Wait until the server finishes
66+
tokio::spawn(async move {
67+
assert!(!tx_halt.is_closed(), "Halt channel for Health Check API should be open");
68+
69+
join_handle
70+
.await
71+
.expect("it should be able to join to the Health Check API server task");
72+
})
6573
}

src/servers/health_check_api/server.rs

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use axum::routing::get;
88
use axum::{Json, Router};
99
use axum_server::Handle;
1010
use futures::Future;
11+
use log::debug;
1112
use serde_json::json;
1213
use tokio::sync::oneshot::{Receiver, Sender};
1314

@@ -37,6 +38,8 @@ pub fn start(
3738

3839
let handle = Handle::new();
3940

41+
debug!(target: "Health Check API", "Starting service with graceful shutdown in a spawned task ...");
42+
4043
tokio::task::spawn(graceful_shutdown(
4144
handle.clone(),
4245
rx_halt,

tests/servers/health_check_api/environment.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::net::SocketAddr;
22
use std::sync::Arc;
33

4+
use log::debug;
45
use tokio::sync::oneshot::{self, Sender};
56
use tokio::task::JoinHandle;
67
use torrust_tracker::bootstrap::jobs::Started;
@@ -50,13 +51,22 @@ impl Environment<Stopped> {
5051

5152
let register = self.registar.entries();
5253

54+
debug!(target: "Health Check API", "Spawning task to launch the service ...");
55+
5356
let server = tokio::spawn(async move {
57+
debug!(target: "Health Check API", "Starting the server in a spawned task ...");
58+
5459
server::start(self.state.bind_to, tx_start, rx_halt, register)
5560
.await
5661
.expect("it should start the health check service");
62+
63+
debug!(target: "Health Check API", "Server started. Sending the binding {} ...", self.state.bind_to);
64+
5765
self.state.bind_to
5866
});
5967

68+
debug!(target: "Health Check API", "Waiting for spawning task to send the binding ...");
69+
6070
let binding = rx_start.await.expect("it should send service binding").address;
6171

6272
Environment {

0 commit comments

Comments
 (0)