Skip to content

Commit 62fddba

Browse files
committed
Merge #629: Fix services bootstraping
17296cd fix: [#626] healt check api server shutdown (Jose Celano) bbf1be6 fix: don't start HTTP tracker if it's disabled (Jose Celano) Pull request description: This fixes all errors introduced after merging [this PR](#623). - [x] HTTP tracker should not be started if it's disabled Error running the Health Check API server: ```console thread 'tokio-runtime-worker' panicked at src/servers/signals.rs:52:25: Failed to install stop signal: channel closed ``` ACKs for top commit: josecelano: ACK 17296cd Tree-SHA512: 95fb8011f6eb58b52f8805185ccabb484be29d6231930aaa9a24d4a9b0a4b5dd457259a343c4a337529264ecd42f76609a57914a661a7ce19255e0fcb41a3145
2 parents 203ce96 + 17296cd commit 62fddba

File tree

6 files changed

+49
-12
lines changed

6 files changed

+49
-12
lines changed

src/app.rs

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ pub async fn start(config: &Configuration, tracker: Arc<core::Tracker>) -> Vec<J
7676

7777
// Start the HTTP blocks
7878
for http_tracker_config in &config.http_trackers {
79+
if !http_tracker_config.enabled {
80+
continue;
81+
}
82+
7983
if let Some(job) = http_tracker::start_job(
8084
http_tracker_config,
8185
tracker.clone(),

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/apis/server.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use axum_server::tls_rustls::RustlsConfig;
3030
use axum_server::Handle;
3131
use derive_more::Constructor;
3232
use futures::future::BoxFuture;
33-
use log::{error, info};
33+
use log::{debug, error, info};
3434
use tokio::sync::oneshot::{Receiver, Sender};
3535
use torrust_tracker_configuration::AccessTokens;
3636

@@ -120,7 +120,12 @@ impl ApiServer<Stopped> {
120120
let launcher = self.state.launcher;
121121

122122
let task = tokio::spawn(async move {
123-
launcher.start(tracker, access_tokens, tx_start, rx_halt).await;
123+
debug!(target: "API", "Starting with launcher in spawned task ...");
124+
125+
let _task = launcher.start(tracker, access_tokens, tx_start, rx_halt).await;
126+
127+
debug!(target: "API", "Started with launcher in spawned task");
128+
124129
launcher
125130
});
126131

@@ -266,9 +271,10 @@ mod tests {
266271
#[tokio::test]
267272
async fn it_should_be_able_to_start_and_stop() {
268273
let cfg = Arc::new(ephemeral_mode_public());
269-
let tracker = initialize_with_configuration(&cfg);
270274
let config = &cfg.http_api;
271275

276+
let tracker = initialize_with_configuration(&cfg);
277+
272278
let bind_to = config
273279
.bind_address
274280
.parse::<std::net::SocketAddr>()

src/servers/health_check_api/server.rs

+4-1
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,10 +38,12 @@ 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,
43-
format!("shutting down http server on socket address: {address}"),
46+
format!("Shutting down http server on socket address: {address}"),
4447
));
4548

4649
let running = axum_server::from_tcp(socket)

src/servers/registar.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::net::SocketAddr;
55
use std::sync::Arc;
66

77
use derive_more::Constructor;
8+
use log::debug;
89
use tokio::sync::Mutex;
910
use tokio::task::JoinHandle;
1011

@@ -81,10 +82,15 @@ impl Registar {
8182

8283
/// Inserts a listing into the registry.
8384
async fn insert(&self, rx: tokio::sync::oneshot::Receiver<ServiceRegistration>) {
84-
let listing = rx.await.expect("it should receive the listing");
85+
debug!("Waiting for the started service to send registration data ...");
86+
87+
let service_registration = rx
88+
.await
89+
.expect("it should receive the service registration from the started service");
8590

8691
let mut mutex = self.registry.lock().await;
87-
mutex.insert(listing.binding, listing);
92+
93+
mutex.insert(service_registration.binding, service_registration);
8894
}
8995

9096
/// Returns the [`ServiceRegistry`] of services

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)