Skip to content

Commit

Permalink
fix: [torrust#626] healt check api server shutdown
Browse files Browse the repository at this point in the history
This fixes:

- The error: "Failed to install stop signal: channel closed"
- And CRTL+C to shutdown the service
  • Loading branch information
josecelano committed Jan 19, 2024
1 parent bbf1be6 commit 0d14a74
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/bootstrap/jobs/health_check_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,32 @@ pub async fn start_job(config: &HealthCheckApi, register: ServiceRegistry) -> Jo

let (tx_start, rx_start) = oneshot::channel::<Started>();
let (tx_halt, rx_halt) = tokio::sync::oneshot::channel::<Halted>();
drop(tx_halt);

let protocol = "http";

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

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

if let Ok(()) = handle.await {
info!(target: "Health Check API", "Stopped server running on: http://{}", bind_addr);
info!(target: "Health Check API", "Stopped server running on: {protocol}://{}", bind_addr);
}
});

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

join_handle
// Wait until the server finishes
tokio::spawn(async move {
assert!(!tx_halt.is_closed(), "Halt channel for Health Check API should be open");

join_handle
.await
.expect("it should be able to join to the Health Check API server task");
})
}
3 changes: 3 additions & 0 deletions src/servers/health_check_api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use axum::routing::get;
use axum::{Json, Router};
use axum_server::Handle;
use futures::Future;
use log::debug;
use serde_json::json;
use tokio::sync::oneshot::{Receiver, Sender};

Expand Down Expand Up @@ -37,6 +38,8 @@ pub fn start(

let handle = Handle::new();

debug!(target: "Health Check API", "Starting service with graceful shutdown in a spawned task ...");

tokio::task::spawn(graceful_shutdown(
handle.clone(),
rx_halt,
Expand Down
10 changes: 10 additions & 0 deletions tests/servers/health_check_api/environment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::net::SocketAddr;
use std::sync::Arc;

use log::debug;
use tokio::sync::oneshot::{self, Sender};
use tokio::task::JoinHandle;
use torrust_tracker::bootstrap::jobs::Started;
Expand Down Expand Up @@ -50,13 +51,22 @@ impl Environment<Stopped> {

let register = self.registar.entries();

debug!(target: "Health Check API", "Spawning task to launch the service ...");

let server = tokio::spawn(async move {
debug!(target: "Health Check API", "Starting the server in a spawned task ...");

server::start(self.state.bind_to, tx_start, rx_halt, register)
.await
.expect("it should start the health check service");

debug!(target: "Health Check API", "Server started. Sending the binding {} ...", self.state.bind_to);

self.state.bind_to
});

debug!(target: "Health Check API", "Waiting for spawning task to send the binding ...");

let binding = rx_start.await.expect("it should send service binding").address;

Environment {
Expand Down

0 comments on commit 0d14a74

Please sign in to comment.