Skip to content

Commit 808a004

Browse files
committed
dev: cleanup service bootstraping
1 parent 933eacb commit 808a004

File tree

27 files changed

+689
-727
lines changed

27 files changed

+689
-727
lines changed

Cargo.lock

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

packages/configuration/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ use config::{Config, ConfigError, File, FileFormat};
239239
use serde::{Deserialize, Serialize};
240240
use serde_with::{serde_as, NoneAsEmptyString};
241241
use thiserror::Error;
242-
use torrust_tracker_located_error::{Located, LocatedError};
242+
use torrust_tracker_located_error::{DynError, Located, LocatedError};
243243
use torrust_tracker_primitives::{DatabaseDriver, TrackerMode};
244244

245245
/// Information required for loading config
@@ -289,7 +289,7 @@ impl Info {
289289

290290
fs::read_to_string(config_path)
291291
.map_err(|e| Error::UnableToLoadFromConfigFile {
292-
source: (Arc::new(e) as Arc<dyn std::error::Error + Send + Sync>).into(),
292+
source: (Arc::new(e) as DynError).into(),
293293
})?
294294
.parse()
295295
.map_err(|_e: std::convert::Infallible| Error::Infallible)?

packages/located-error/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ use std::error::Error;
3333
use std::panic::Location;
3434
use std::sync::Arc;
3535

36+
pub type DynError = Arc<dyn std::error::Error + Send + Sync>;
37+
3638
/// A generic wrapper around an error.
3739
///
3840
/// Where `E` is the inner error (source error).
@@ -96,7 +98,7 @@ where
9698
}
9799

98100
#[allow(clippy::from_over_into)]
99-
impl<'a> Into<LocatedError<'a, dyn std::error::Error + Send + Sync>> for Arc<dyn std::error::Error + Send + Sync> {
101+
impl<'a> Into<LocatedError<'a, dyn std::error::Error + Send + Sync>> for DynError {
100102
#[track_caller]
101103
fn into(self) -> LocatedError<'a, dyn std::error::Error + Send + Sync> {
102104
LocatedError {

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

+32-61
Original file line numberDiff line numberDiff line change
@@ -7,88 +7,59 @@
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

21+
use super::make_rust_tls;
2222
use crate::core;
23-
use crate::servers::http::v1::launcher;
23+
use crate::servers::http::server::{HttpServer, HttpServerLauncher};
24+
use crate::servers::http::v1::launcher::Launcher;
2425
use crate::servers::http::Version;
2526

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-
3227
/// It starts a new HTTP server with the provided configuration and version.
3328
///
3429
/// Right now there is only one version but in the future we could support more than one HTTP tracker version at the same time.
3530
/// 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-
31+
///
4232
/// # Panics
4333
///
4434
/// 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())
73-
.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-
}
35+
///
36+
pub async fn start_job(config: &HttpTracker, tracker: Arc<core::Tracker>, version: Version) -> Option<JoinHandle<()>> {
37+
if config.enabled {
38+
let socket = config
39+
.bind_address
40+
.parse::<std::net::SocketAddr>()
41+
.expect("Tracker API bind_address invalid.");
42+
43+
let tls = make_rust_tls(config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path)
44+
.await
45+
.map(|tls| tls.expect("tls config failed"));
46+
47+
match version {
48+
Version::V1 => Some(start_v1(socket, tls, tracker.clone()).await),
8449
}
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}"),
50+
} else {
51+
info!("Note: Not loading Http Tracker Service, Not Enabled in Configuration.");
52+
None
9153
}
54+
}
55+
56+
async fn start_v1(socket: SocketAddr, tls: Option<RustlsConfig>, tracker: Arc<core::Tracker>) -> JoinHandle<()> {
57+
let server = HttpServer::new(Launcher::new(socket, tls))
58+
.start(tracker)
59+
.await
60+
.expect("Failed to start Server");
9261

93-
join_handle
62+
tokio::spawn(async move {
63+
server.state.task.await.expect("failed to finish service");
64+
})
9465
}

0 commit comments

Comments
 (0)