@@ -28,10 +28,13 @@ use std::sync::Arc;
2828
2929use axum_server:: tls_rustls:: RustlsConfig ;
3030use axum_server:: Handle ;
31+ use derive_more:: derive:: Display ;
3132use derive_more:: Constructor ;
3233use futures:: future:: BoxFuture ;
34+ use thiserror:: Error ;
3335use tokio:: sync:: oneshot:: { Receiver , Sender } ;
3436use torrust_tracker_configuration:: AccessTokens ;
37+ use tracing:: { instrument, Level } ;
3538
3639use super :: routes:: router;
3740use crate :: bootstrap:: jobs:: Started ;
@@ -43,9 +46,10 @@ use crate::servers::registar::{ServiceHealthCheckJob, ServiceRegistration, Servi
4346use crate :: servers:: signals:: { graceful_shutdown, Halted } ;
4447
4548/// Errors that can occur when starting or stopping the API server.
46- #[ derive( Debug ) ]
49+ #[ derive( Debug , Error ) ]
4750pub enum Error {
48- Error ( String ) ,
51+ #[ error( "Error when starting or stopping the API server" ) ]
52+ FailedToStartOrStop ( String ) ,
4953}
5054
5155/// An alias for the `ApiServer` struct with the `Stopped` state.
@@ -62,31 +66,39 @@ pub type RunningApiServer = ApiServer<Running>;
6266/// It's a state machine that can be in one of two
6367/// states: `Stopped` or `Running`.
6468#[ allow( clippy:: module_name_repetitions) ]
65- pub struct ApiServer < S > {
69+ #[ derive( Debug , Display ) ]
70+ pub struct ApiServer < S >
71+ where
72+ S : std:: fmt:: Debug + std:: fmt:: Display ,
73+ {
6674 pub state : S ,
6775}
6876
6977/// The `Stopped` state of the `ApiServer` struct.
78+ #[ derive( Debug , Display ) ]
79+ #[ display( "Stopped: {launcher}" ) ]
7080pub struct Stopped {
7181 launcher : Launcher ,
7282}
7383
7484/// The `Running` state of the `ApiServer` struct.
85+ #[ derive( Debug , Display ) ]
86+ #[ display( "Running (with local address): {local_addr}" ) ]
7587pub struct Running {
76- pub binding : SocketAddr ,
88+ pub local_addr : SocketAddr ,
7789 pub halt_task : tokio:: sync:: oneshot:: Sender < Halted > ,
7890 pub task : tokio:: task:: JoinHandle < Launcher > ,
7991}
8092
8193impl Running {
8294 #[ must_use]
8395 pub fn new (
84- binding : SocketAddr ,
96+ local_addr : SocketAddr ,
8597 halt_task : tokio:: sync:: oneshot:: Sender < Halted > ,
8698 task : tokio:: task:: JoinHandle < Launcher > ,
8799 ) -> Self {
88100 Self {
89- binding ,
101+ local_addr ,
90102 halt_task,
91103 task,
92104 }
@@ -110,6 +122,7 @@ impl ApiServer<Stopped> {
110122 /// # Panics
111123 ///
112124 /// It would panic if the bound socket address cannot be sent back to this starter.
125+ #[ instrument( skip( self , tracker, form, access_tokens) , err, ret( Display , level = Level :: INFO ) ) ]
113126 pub async fn start (
114127 self ,
115128 tracker : Arc < Tracker > ,
@@ -157,13 +170,14 @@ impl ApiServer<Running> {
157170 /// # Errors
158171 ///
159172 /// It would return an error if the channel for the task killer signal was closed.
173+ #[ instrument( skip( self ) , err, ret( Display , level = Level :: INFO ) ) ]
160174 pub async fn stop ( self ) -> Result < ApiServer < Stopped > , Error > {
161175 self . state
162176 . halt_task
163177 . send ( Halted :: Normal )
164- . map_err ( |_| Error :: Error ( "Task killer channel was closed." . to_string ( ) ) ) ?;
178+ . map_err ( |_| Error :: FailedToStartOrStop ( "Task killer channel was closed." . to_string ( ) ) ) ?;
165179
166- let launcher = self . state . task . await . map_err ( |e| Error :: Error ( e. to_string ( ) ) ) ?;
180+ let launcher = self . state . task . await . map_err ( |e| Error :: FailedToStartOrStop ( e. to_string ( ) ) ) ?;
167181
168182 Ok ( ApiServer {
169183 state : Stopped { launcher } ,
@@ -178,6 +192,7 @@ impl ApiServer<Running> {
178192/// This function will return an error if unable to connect.
179193/// Or if there request returns an error code.
180194#[ must_use]
195+ #[ instrument( skip( ) ) ]
181196pub fn check_fn ( binding : & SocketAddr ) -> ServiceHealthCheckJob {
182197 let url = format ! ( "http://{binding}/api/health_check" ) ; // DevSkim: ignore DS137138
183198
@@ -199,6 +214,16 @@ pub struct Launcher {
199214 tls : Option < RustlsConfig > ,
200215}
201216
217+ impl std:: fmt:: Display for Launcher {
218+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
219+ if self . tls . is_some ( ) {
220+ write ! ( f, "(with socket): {}, using TLS" , self . bind_to, )
221+ } else {
222+ write ! ( f, "(with socket): {}, without TLS" , self . bind_to, )
223+ }
224+ }
225+ }
226+
202227impl Launcher {
203228 /// Starts the API server with graceful shutdown.
204229 ///
@@ -210,6 +235,7 @@ impl Launcher {
210235 ///
211236 /// Will panic if unable to bind to the socket, or unable to get the address of the bound socket.
212237 /// Will also panic if unable to send message regarding the bound socket address.
238+ #[ instrument( skip( self , tracker, access_tokens, tx_start, rx_halt) ) ]
213239 pub fn start (
214240 & self ,
215241 tracker : Arc < Tracker > ,
0 commit comments