Skip to content

Commit

Permalink
dev: cleanup launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Dec 28, 2023
1 parent 933eacb commit 38d81ee
Show file tree
Hide file tree
Showing 19 changed files with 483 additions and 666 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ use tokio::task::JoinHandle;
use torrust_tracker_configuration::Configuration;

use crate::bootstrap::jobs::{health_check_api, http_tracker, torrent_cleanup, tracker_apis, udp_tracker};
use crate::core;
use crate::servers::http::Version;
use crate::{core, servers};

/// # Panics
///
Expand Down Expand Up @@ -68,21 +67,22 @@ pub async fn start(config: Arc<Configuration>, tracker: Arc<core::Tracker>) -> V
udp_tracker_config.bind_address, config.mode
);
} else {
jobs.push(udp_tracker::start_job(udp_tracker_config, tracker.clone()));
jobs.push(udp_tracker::start_job(udp_tracker_config, tracker.clone()).await);

Check warning on line 70 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L70

Added line #L70 was not covered by tests
}
}

// Start the HTTP blocks
for http_tracker_config in &config.http_trackers {
if !http_tracker_config.enabled {
continue;
}
jobs.push(http_tracker::start_job(http_tracker_config, tracker.clone(), Version::V1).await);
if let Some(job) = http_tracker::start_job(http_tracker_config, tracker.clone(), servers::http::Version::V1).await {
jobs.push(job);
};

Check warning on line 78 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L76-L78

Added lines #L76 - L78 were not covered by tests
}

// Start HTTP API
if config.http_api.enabled {
jobs.push(tracker_apis::start_job(&config.http_api, tracker.clone()).await);
if let Some(job) = tracker_apis::start_job(&config.http_api, tracker.clone(), servers::apis::Version::V1).await {
jobs.push(job);
};

Check warning on line 85 in src/app.rs

View check run for this annotation

Codecov / codecov/patch

src/app.rs#L83-L85

Added lines #L83 - L85 were not covered by tests
}

// Start runners to remove torrents without peers, every interval
Expand Down
25 changes: 6 additions & 19 deletions src/bootstrap/jobs/health_check_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,23 @@
//! The [`health_check_api::start_job`](crate::bootstrap::jobs::health_check_api::start_job)
//! function spawns a new asynchronous task, that tasks is the "**launcher**".
//! The "**launcher**" starts the actual server and sends a message back
//! to the main application. The main application waits until receives
//! the message [`ApiServerJobStarted`]
//! from the "**launcher**".
//! to the main application.
//!
//! The "**launcher**" is an intermediary thread that decouples the Health Check
//! API server from the process that handles it.
//!
//! Refer to the [configuration documentation](https://docs.rs/torrust-tracker-configuration)
//! for the API configuration options.
use std::net::SocketAddr;
use std::sync::Arc;

use log::info;
use tokio::sync::oneshot;
use tokio::task::JoinHandle;
use torrust_tracker_configuration::Configuration;

use super::Started;
use crate::servers::health_check_api::server;

/// This is the message that the "launcher" spawned task sends to the main
/// application process to notify the API server was successfully started.
///
/// > **NOTICE**: it does not mean the API 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.
#[derive(Debug)]
pub struct ApiServerJobStarted {
pub bound_addr: SocketAddr,
}

/// This function starts a new Health Check API server with the provided
/// configuration.
///
Expand All @@ -53,22 +40,22 @@ pub async fn start_job(config: Arc<Configuration>) -> JoinHandle<()> {
.parse::<std::net::SocketAddr>()
.expect("Health Check API bind_address invalid.");

let (tx, rx) = oneshot::channel::<ApiServerJobStarted>();
let (tx_start, rx_start) = oneshot::channel::<Started>();

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

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/health_check_api.rs#L43

Added line #L43 was not covered by tests

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

let handle = server::start(bind_addr, tx, config.clone());
let handle = server::start(bind_addr, tx_start, config.clone());

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

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/health_check_api.rs#L49

Added line #L49 was not covered by tests

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

// Wait until the API server job is running
match rx.await {
Ok(_msg) => info!("Torrust Health Check API server started"),
match rx_start.await {
Ok(msg) => info!("Torrust Health Check API server started on socket: {}", msg.address),

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

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/health_check_api.rs#L57-L58

Added lines #L57 - L58 were not covered by tests
Err(e) => panic!("the Health Check API server was dropped: {e}"),
}

Expand Down
98 changes: 38 additions & 60 deletions src/bootstrap/jobs/http_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,88 +7,66 @@
//!
//! The [`http_tracker::start_job`](crate::bootstrap::jobs::http_tracker::start_job) function spawns a new asynchronous task,
//! that tasks is the "**launcher**". The "**launcher**" starts the actual server and sends a message back to the main application.
//! The main application waits until receives the message [`ServerJobStarted`] from the "**launcher**".
//!
//! 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.
//! In that case it would not need to notify a parent process.
use std::net::SocketAddr;
use std::sync::Arc;

use axum_server::tls_rustls::RustlsConfig;
use log::info;
use tokio::sync::oneshot;
use tokio::task::JoinHandle;
use torrust_tracker_configuration::HttpTracker;

use crate::core;
use crate::servers::http::v1::launcher;
use crate::servers::http::server::{HttpServer, HttpServerLauncher};
use crate::servers::http::v1::launcher::Launcher;
use crate::servers::http::Version;

/// This is the message that the "**launcher**" spawned task sends to the main application process to notify that the HTTP server was successfully started.
///
/// > **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.
#[derive(Debug)]
pub struct ServerJobStarted();

/// It starts a new HTTP server with the provided configuration and version.
///
/// Right now there is only one version but in the future we could support more than one HTTP tracker version at the same time.
/// This feature allows supporting breaking changes on `BitTorrent` BEPs.
pub async fn start_job(config: &HttpTracker, tracker: Arc<core::Tracker>, version: Version) -> JoinHandle<()> {
match version {
Version::V1 => start_v1(config, tracker.clone()).await,
}
}

///
/// # Panics
///
/// It would panic if the `config::HttpTracker` struct would contain inappropriate values.
async fn start_v1(config: &HttpTracker, tracker: Arc<core::Tracker>) -> JoinHandle<()> {
let bind_addr = config
.bind_address
.parse::<std::net::SocketAddr>()
.expect("Tracker API bind_address invalid.");
let ssl_enabled = config.ssl_enabled;
let ssl_cert_path = config.ssl_cert_path.clone();
let ssl_key_path = config.ssl_key_path.clone();

let (tx, rx) = oneshot::channel::<ServerJobStarted>();

// Run the API server
let join_handle = tokio::spawn(async move {
if !ssl_enabled {
info!("Starting Torrust HTTP tracker server on: http://{}", bind_addr);

let handle = launcher::start(bind_addr, tracker);

tx.send(ServerJobStarted())
.expect("the HTTP tracker server should not be dropped");

if let Ok(()) = handle.await {
info!("Torrust HTTP tracker server on http://{} stopped", bind_addr);
}
} else if ssl_enabled && ssl_cert_path.is_some() && ssl_key_path.is_some() {
info!("Starting Torrust HTTP tracker server on: https://{}", bind_addr);

let ssl_config = RustlsConfig::from_pem_file(ssl_cert_path.unwrap(), ssl_key_path.unwrap())
///
pub async fn start_job(config: &HttpTracker, tracker: Arc<core::Tracker>, version: Version) -> Option<JoinHandle<()>> {
if config.enabled {
let socket = config

Check warning on line 37 in src/bootstrap/jobs/http_tracker.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/http_tracker.rs#L35-L37

Added lines #L35 - L37 were not covered by tests
.bind_address
.parse::<std::net::SocketAddr>()
.expect("Tracker API bind_address invalid.");

Check warning on line 40 in src/bootstrap/jobs/http_tracker.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/http_tracker.rs#L40

Added line #L40 was not covered by tests

let tls = if let (true, Some(cert), Some(key)) = (&config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path) {
let tls = RustlsConfig::from_pem_file(cert, key)

Check warning on line 43 in src/bootstrap/jobs/http_tracker.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/http_tracker.rs#L42-L43

Added lines #L42 - L43 were not covered by tests
.await
.unwrap();

let handle = launcher::start_tls(bind_addr, ssl_config, tracker);

tx.send(ServerJobStarted())
.expect("the HTTP tracker server should not be dropped");

if let Ok(()) = handle.await {
info!("Torrust HTTP tracker server on https://{} stopped", bind_addr);
}
.expect("Could not read tls cert.");
info!("Using https: cert path: {cert}.");
info!("Using https: key path: {cert}.");
Some(tls)

Check warning on line 48 in src/bootstrap/jobs/http_tracker.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/http_tracker.rs#L45-L48

Added lines #L45 - L48 were not covered by tests
} else {
info!("Loading HTTP tracker without TLS.");
None
};

Check warning on line 52 in src/bootstrap/jobs/http_tracker.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/http_tracker.rs#L50-L52

Added lines #L50 - L52 were not covered by tests

match version {
Version::V1 => Some(start_v1(socket, tls, tracker.clone()).await),

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

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/http_tracker.rs#L55

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

// Wait until the HTTP tracker server job is running
match rx.await {
Ok(_msg) => info!("Torrust HTTP tracker server started"),
Err(e) => panic!("the HTTP tracker server was dropped: {e}"),
} else {
info!("Note: Not loading Http Tracker Service, Not Enabled in Configuration.");
None

Check warning on line 59 in src/bootstrap/jobs/http_tracker.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/http_tracker.rs#L57-L59

Added lines #L57 - L59 were not covered by tests
}
}

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

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/http_tracker.rs#L61

Added line #L61 was not covered by tests

async fn start_v1(socket: SocketAddr, tls: Option<RustlsConfig>, tracker: Arc<core::Tracker>) -> JoinHandle<()> {
let server = HttpServer::new(Launcher::new(socket, tls))
.start(tracker)
.await
.expect("Failed to start Server");

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

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/http_tracker.rs#L63-L67

Added lines #L63 - L67 were not covered by tests

join_handle
tokio::spawn(async move {
server.state.task.await.expect("failed to finish service");
})

Check warning on line 71 in src/bootstrap/jobs/http_tracker.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/http_tracker.rs#L69-L71

Added lines #L69 - L71 were not covered by tests
}
8 changes: 8 additions & 0 deletions src/bootstrap/jobs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ pub mod http_tracker;
pub mod torrent_cleanup;
pub mod tracker_apis;
pub mod udp_tracker;

/// This is the message that the "launcher" spawned task sends to the main
/// application process to notify the service was successfully started.
///
#[derive(Debug)]

Check warning on line 18 in src/bootstrap/jobs/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/mod.rs#L18

Added line #L18 was not covered by tests
pub struct Started {
pub address: std::net::SocketAddr,

Check warning on line 20 in src/bootstrap/jobs/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bootstrap/jobs/mod.rs#L20

Added line #L20 was not covered by tests
}
Loading

0 comments on commit 38d81ee

Please sign in to comment.