Skip to content

Commit 38d81ee

Browse files
committed
dev: cleanup launcher
1 parent 933eacb commit 38d81ee

19 files changed

+483
-666
lines changed

Cargo.lock

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ use tokio::task::JoinHandle;
2828
use torrust_tracker_configuration::Configuration;
2929

3030
use crate::bootstrap::jobs::{health_check_api, http_tracker, torrent_cleanup, tracker_apis, udp_tracker};
31-
use crate::core;
32-
use crate::servers::http::Version;
31+
use crate::{core, servers};
3332

3433
/// # Panics
3534
///
@@ -68,21 +67,22 @@ pub async fn start(config: Arc<Configuration>, tracker: Arc<core::Tracker>) -> V
6867
udp_tracker_config.bind_address, config.mode
6968
);
7069
} else {
71-
jobs.push(udp_tracker::start_job(udp_tracker_config, tracker.clone()));
70+
jobs.push(udp_tracker::start_job(udp_tracker_config, tracker.clone()).await);
7271
}
7372
}
7473

7574
// Start the HTTP blocks
7675
for http_tracker_config in &config.http_trackers {
77-
if !http_tracker_config.enabled {
78-
continue;
79-
}
80-
jobs.push(http_tracker::start_job(http_tracker_config, tracker.clone(), Version::V1).await);
76+
if let Some(job) = http_tracker::start_job(http_tracker_config, tracker.clone(), servers::http::Version::V1).await {
77+
jobs.push(job);
78+
};
8179
}
8280

8381
// Start HTTP API
8482
if config.http_api.enabled {
85-
jobs.push(tracker_apis::start_job(&config.http_api, tracker.clone()).await);
83+
if let Some(job) = tracker_apis::start_job(&config.http_api, tracker.clone(), servers::apis::Version::V1).await {
84+
jobs.push(job);
85+
};
8686
}
8787

8888
// Start runners to remove torrents without peers, every interval

src/bootstrap/jobs/health_check_api.rs

+6-19
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,23 @@
66
//! The [`health_check_api::start_job`](crate::bootstrap::jobs::health_check_api::start_job)
77
//! function spawns a new asynchronous task, that tasks is the "**launcher**".
88
//! The "**launcher**" starts the actual server and sends a message back
9-
//! to the main application. The main application waits until receives
10-
//! the message [`ApiServerJobStarted`]
11-
//! from the "**launcher**".
9+
//! to the main application.
1210
//!
1311
//! The "**launcher**" is an intermediary thread that decouples the Health Check
1412
//! API server from the process that handles it.
1513
//!
1614
//! Refer to the [configuration documentation](https://docs.rs/torrust-tracker-configuration)
1715
//! for the API configuration options.
18-
use std::net::SocketAddr;
1916
use std::sync::Arc;
2017

2118
use log::info;
2219
use tokio::sync::oneshot;
2320
use tokio::task::JoinHandle;
2421
use torrust_tracker_configuration::Configuration;
2522

23+
use super::Started;
2624
use crate::servers::health_check_api::server;
2725

28-
/// This is the message that the "launcher" spawned task sends to the main
29-
/// application process to notify the API server was successfully started.
30-
///
31-
/// > **NOTICE**: it does not mean the API server is ready to receive requests.
32-
/// It only means the new server started. It might take some time to the server
33-
/// to be ready to accept request.
34-
#[derive(Debug)]
35-
pub struct ApiServerJobStarted {
36-
pub bound_addr: SocketAddr,
37-
}
38-
3926
/// This function starts a new Health Check API server with the provided
4027
/// configuration.
4128
///
@@ -53,22 +40,22 @@ pub async fn start_job(config: Arc<Configuration>) -> JoinHandle<()> {
5340
.parse::<std::net::SocketAddr>()
5441
.expect("Health Check API bind_address invalid.");
5542

56-
let (tx, rx) = oneshot::channel::<ApiServerJobStarted>();
43+
let (tx_start, rx_start) = oneshot::channel::<Started>();
5744

5845
// Run the API server
5946
let join_handle = tokio::spawn(async move {
6047
info!("Starting Health Check API server: http://{}", bind_addr);
6148

62-
let handle = server::start(bind_addr, tx, config.clone());
49+
let handle = server::start(bind_addr, tx_start, config.clone());
6350

6451
if let Ok(()) = handle.await {
6552
info!("Health Check API server on http://{} stopped", bind_addr);
6653
}
6754
});
6855

6956
// Wait until the API server job is running
70-
match rx.await {
71-
Ok(_msg) => info!("Torrust Health Check API server started"),
57+
match rx_start.await {
58+
Ok(msg) => info!("Torrust Health Check API server started on socket: {}", msg.address),
7259
Err(e) => panic!("the Health Check API server was dropped: {e}"),
7360
}
7461

src/bootstrap/jobs/http_tracker.rs

+38-60
Original file line numberDiff line numberDiff line change
@@ -7,88 +7,66 @@
77
//!
88
//! The [`http_tracker::start_job`](crate::bootstrap::jobs::http_tracker::start_job) function spawns a new asynchronous task,
99
//! that tasks is the "**launcher**". The "**launcher**" starts the actual server and sends a message back to the main application.
10-
//! The main application waits until receives the message [`ServerJobStarted`] from the "**launcher**".
1110
//!
1211
//! The "**launcher**" is an intermediary thread that decouples the HTTP servers from the process that handles it. The HTTP could be used independently in the future.
1312
//! In that case it would not need to notify a parent process.
13+
use std::net::SocketAddr;
1414
use std::sync::Arc;
1515

1616
use axum_server::tls_rustls::RustlsConfig;
1717
use log::info;
18-
use tokio::sync::oneshot;
1918
use tokio::task::JoinHandle;
2019
use torrust_tracker_configuration::HttpTracker;
2120

2221
use crate::core;
23-
use crate::servers::http::v1::launcher;
22+
use crate::servers::http::server::{HttpServer, HttpServerLauncher};
23+
use crate::servers::http::v1::launcher::Launcher;
2424
use crate::servers::http::Version;
2525

26-
/// This is the message that the "**launcher**" spawned task sends to the main application process to notify that the HTTP server was successfully started.
27-
///
28-
/// > **NOTICE**: it does not mean the HTTP server is ready to receive requests. It only means the new server started. It might take some time to the server to be ready to accept request.
29-
#[derive(Debug)]
30-
pub struct ServerJobStarted();
31-
3226
/// It starts a new HTTP server with the provided configuration and version.
3327
///
3428
/// Right now there is only one version but in the future we could support more than one HTTP tracker version at the same time.
3529
/// This feature allows supporting breaking changes on `BitTorrent` BEPs.
36-
pub async fn start_job(config: &HttpTracker, tracker: Arc<core::Tracker>, version: Version) -> JoinHandle<()> {
37-
match version {
38-
Version::V1 => start_v1(config, tracker.clone()).await,
39-
}
40-
}
41-
30+
///
4231
/// # Panics
4332
///
4433
/// It would panic if the `config::HttpTracker` struct would contain inappropriate values.
45-
async fn start_v1(config: &HttpTracker, tracker: Arc<core::Tracker>) -> JoinHandle<()> {
46-
let bind_addr = config
47-
.bind_address
48-
.parse::<std::net::SocketAddr>()
49-
.expect("Tracker API bind_address invalid.");
50-
let ssl_enabled = config.ssl_enabled;
51-
let ssl_cert_path = config.ssl_cert_path.clone();
52-
let ssl_key_path = config.ssl_key_path.clone();
53-
54-
let (tx, rx) = oneshot::channel::<ServerJobStarted>();
55-
56-
// Run the API server
57-
let join_handle = tokio::spawn(async move {
58-
if !ssl_enabled {
59-
info!("Starting Torrust HTTP tracker server on: http://{}", bind_addr);
60-
61-
let handle = launcher::start(bind_addr, tracker);
62-
63-
tx.send(ServerJobStarted())
64-
.expect("the HTTP tracker server should not be dropped");
65-
66-
if let Ok(()) = handle.await {
67-
info!("Torrust HTTP tracker server on http://{} stopped", bind_addr);
68-
}
69-
} else if ssl_enabled && ssl_cert_path.is_some() && ssl_key_path.is_some() {
70-
info!("Starting Torrust HTTP tracker server on: https://{}", bind_addr);
71-
72-
let ssl_config = RustlsConfig::from_pem_file(ssl_cert_path.unwrap(), ssl_key_path.unwrap())
34+
///
35+
pub async fn start_job(config: &HttpTracker, tracker: Arc<core::Tracker>, version: Version) -> Option<JoinHandle<()>> {
36+
if config.enabled {
37+
let socket = config
38+
.bind_address
39+
.parse::<std::net::SocketAddr>()
40+
.expect("Tracker API bind_address invalid.");
41+
42+
let tls = if let (true, Some(cert), Some(key)) = (&config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path) {
43+
let tls = RustlsConfig::from_pem_file(cert, key)
7344
.await
74-
.unwrap();
75-
76-
let handle = launcher::start_tls(bind_addr, ssl_config, tracker);
77-
78-
tx.send(ServerJobStarted())
79-
.expect("the HTTP tracker server should not be dropped");
80-
81-
if let Ok(()) = handle.await {
82-
info!("Torrust HTTP tracker server on https://{} stopped", bind_addr);
83-
}
45+
.expect("Could not read tls cert.");
46+
info!("Using https: cert path: {cert}.");
47+
info!("Using https: key path: {cert}.");
48+
Some(tls)
49+
} else {
50+
info!("Loading HTTP tracker without TLS.");
51+
None
52+
};
53+
54+
match version {
55+
Version::V1 => Some(start_v1(socket, tls, tracker.clone()).await),
8456
}
85-
});
86-
87-
// Wait until the HTTP tracker server job is running
88-
match rx.await {
89-
Ok(_msg) => info!("Torrust HTTP tracker server started"),
90-
Err(e) => panic!("the HTTP tracker server was dropped: {e}"),
57+
} else {
58+
info!("Note: Not loading Http Tracker Service, Not Enabled in Configuration.");
59+
None
9160
}
61+
}
62+
63+
async fn start_v1(socket: SocketAddr, tls: Option<RustlsConfig>, tracker: Arc<core::Tracker>) -> JoinHandle<()> {
64+
let server = HttpServer::new(Launcher::new(socket, tls))
65+
.start(tracker)
66+
.await
67+
.expect("Failed to start Server");
9268

93-
join_handle
69+
tokio::spawn(async move {
70+
server.state.task.await.expect("failed to finish service");
71+
})
9472
}

src/bootstrap/jobs/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ pub mod http_tracker;
1111
pub mod torrent_cleanup;
1212
pub mod tracker_apis;
1313
pub mod udp_tracker;
14+
15+
/// This is the message that the "launcher" spawned task sends to the main
16+
/// application process to notify the service was successfully started.
17+
///
18+
#[derive(Debug)]
19+
pub struct Started {
20+
pub address: std::net::SocketAddr,
21+
}

0 commit comments

Comments
 (0)