Skip to content

Commit

Permalink
fix: [#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 17296cd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 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";

Check warning on line 46 in src/bootstrap/jobs/health_check_api.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/health_check_api.rs#L46

Added line #L46 was not covered by tests

// 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);

Check warning on line 50 in src/bootstrap/jobs/health_check_api.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/health_check_api.rs#L50

Added line #L50 was not covered by tests

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);

Check warning on line 55 in src/bootstrap/jobs/health_check_api.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/health_check_api.rs#L55

Added line #L55 was not covered by tests
}
});

// 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),

Check warning on line 61 in src/bootstrap/jobs/health_check_api.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/health_check_api.rs#L61

Added line #L61 was not covered by tests
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");

Check warning on line 67 in src/bootstrap/jobs/health_check_api.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/health_check_api.rs#L66-L67

Added lines #L66 - L67 were not covered by tests

join_handle
.await

Check warning on line 70 in src/bootstrap/jobs/health_check_api.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/health_check_api.rs#L69-L70

Added lines #L69 - L70 were not covered by tests
.expect("it should be able to join to the Health Check API server task");
})

Check warning on line 72 in src/bootstrap/jobs/health_check_api.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/health_check_api.rs#L72

Added line #L72 was not covered by tests
}
5 changes: 4 additions & 1 deletion 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,10 +38,12 @@ 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,
format!("shutting down http server on socket address: {address}"),
format!("Shutting down http server on socket address: {address}"),
));

let running = axum_server::from_tcp(socket)
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 17296cd

Please sign in to comment.