Skip to content

Commit 15aa831

Browse files
committed
refactor: api job starter waits until api is ready
There are two main changes: - The API server does not send the message when is ready. The job starter waits until the API server is running. This change is less radical becuase we keep the `start_job` return type as the other job starters. We did not want to send a real message from the API. We only wanted to know that the API thread is up and running. - The job starter waits until the API job is running even in production code. In the previous version we did that only for the e2e tests.
1 parent 32a6d79 commit 15aa831

File tree

4 files changed

+20
-29
lines changed

4 files changed

+20
-29
lines changed

src/api/server.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ use std::sync::Arc;
55
use std::time::Duration;
66

77
use serde::{Deserialize, Serialize};
8-
use tokio::sync::oneshot::Sender;
98
use warp::{filters, reply, serve, Filter};
109

1110
use super::resources::auth_key_resource::AuthKeyResource;
12-
use crate::jobs::tracker_api::ApiReady;
1311
use crate::peer::TorrentPeer;
1412
use crate::protocol::common::*;
1513
use crate::tracker::TorrentTracker;
@@ -90,11 +88,7 @@ fn authenticate(tokens: HashMap<String, String>) -> impl Filter<Extract = (), Er
9088
.untuple_one()
9189
}
9290

93-
pub fn start(
94-
socket_addr: SocketAddr,
95-
tracker: Arc<TorrentTracker>,
96-
messenger_to_initiator: Sender<ApiReady>,
97-
) -> impl warp::Future<Output = ()> {
91+
pub fn start(socket_addr: SocketAddr, tracker: Arc<TorrentTracker>) -> impl warp::Future<Output = ()> {
9892
// GET /api/torrents?offset=:u32&limit=:u32
9993
// View torrent list
10094
let api_torrents = tracker.clone();
@@ -349,11 +343,6 @@ pub fn start(
349343

350344
let server = api_routes.and(authenticate(tracker.config.http_api.access_tokens.clone()));
351345

352-
// Send a message to the initiator to notify the API is ready to accept requests
353-
if messenger_to_initiator.send(ApiReady()).is_err() {
354-
panic!("the receiver dropped");
355-
}
356-
357346
let (_addr, api_server) = serve(server).bind_with_graceful_shutdown(socket_addr, async move {
358347
tokio::signal::ctrl_c().await.expect("Failed to listen to shutdown signal.");
359348
});

src/jobs/tracker_api.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
use std::sync::Arc;
22

33
use log::info;
4-
use tokio::sync::oneshot::{self, Receiver};
4+
use tokio::sync::oneshot;
55
use tokio::task::JoinHandle;
66

77
use crate::api::server;
88
use crate::tracker::TorrentTracker;
99
use crate::Configuration;
1010

1111
#[derive(Debug)]
12-
pub struct ApiReady();
12+
pub struct ApiServerJobStarted();
1313

14-
pub fn start_job(config: &Configuration, tracker: Arc<TorrentTracker>) -> (JoinHandle<()>, Receiver<ApiReady>) {
14+
pub async fn start_job(config: &Configuration, tracker: Arc<TorrentTracker>) -> JoinHandle<()> {
1515
let bind_addr = config
1616
.http_api
1717
.bind_address
1818
.parse::<std::net::SocketAddr>()
1919
.expect("Tracker API bind_address invalid.");
2020

21-
let (tx, rx) = oneshot::channel::<ApiReady>();
22-
2321
info!("Starting Torrust API server on: {}", bind_addr);
2422

23+
let (tx, rx) = oneshot::channel::<ApiServerJobStarted>();
24+
25+
// Run the API server
2526
let join_handle = tokio::spawn(async move {
26-
server::start(bind_addr, tracker, tx).await;
27+
if tx.send(ApiServerJobStarted()).is_err() {
28+
panic!("the start job dropped");
29+
}
30+
server::start(bind_addr, tracker).await;
2731
});
2832

29-
(join_handle, rx)
33+
// Wait until the API server job is running
34+
match rx.await {
35+
Ok(_msg) => info!("Torrust API server started"),
36+
Err(_) => panic!("the api server dropped"),
37+
}
38+
39+
join_handle
3040
}

src/setup.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ pub async fn setup(config: &Configuration, tracker: Arc<TorrentTracker>) -> Vec<
4949

5050
// Start HTTP API server
5151
if config.http_api.enabled {
52-
let (join_handle, _receiver) = tracker_api::start_job(config, tracker.clone());
53-
jobs.push(join_handle);
52+
jobs.push(tracker_api::start_job(config, tracker.clone()).await);
5453
}
5554

5655
// Remove torrents without peers, every interval

tests/api.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,9 @@ mod tracker_api {
152152
logging::setup_logging(&configuration);
153153

154154
// Start the HTTP API job
155-
let (join_handle, api_receiver) = tracker_api::start_job(&configuration, tracker.clone());
156-
self.job = Some(join_handle);
155+
self.job = Some(tracker_api::start_job(&configuration, tracker).await);
157156

158157
self.started.store(true, Ordering::Relaxed);
159-
160-
// Wait until the API is ready
161-
match api_receiver.await {
162-
Ok(msg) => println!("Message received from API server: {:?}", msg),
163-
Err(_) => panic!("the api server dropped"),
164-
}
165158
}
166159
}
167160
}

0 commit comments

Comments
 (0)