11use std:: net:: SocketAddr ;
22use std:: sync:: Arc ;
33
4+ use axum:: routing:: get;
5+ use axum:: { Json , Router } ;
6+ use log:: info;
7+ use serde_json:: { json, Value } ;
48use tokio:: task:: JoinHandle ;
59
610use crate :: bootstrap:: logging;
@@ -159,6 +163,8 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
159163 let weak_tracker_statistics_importer = Arc :: downgrade ( & tracker_statistics_importer) ;
160164
161165 let tracker_statistics_importer_handle = tokio:: spawn ( async move {
166+ info ! ( "Tracker statistics importer started" ) ;
167+
162168 let interval = std:: time:: Duration :: from_secs ( torrent_info_update_interval) ;
163169 let mut interval = tokio:: time:: interval ( interval) ;
164170 interval. tick ( ) . await ; // first tick is immediate...
@@ -173,12 +179,40 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
173179 } ) ;
174180
175181 // Start API server
176-
177182 let running_api = start ( app_data, & net_ip, net_port, api_version) . await ;
178183
179- Running {
184+ // Full running application
185+ let app = Running {
180186 api_socket_addr : running_api. socket_addr ,
181187 api_server : running_api. api_server ,
182188 tracker_data_importer_handle : tracker_statistics_importer_handle,
183- }
189+ } ;
190+
191+ // Start Health Checker
192+ // This must be done after launching the other services because it does not
193+ // re-check the health of all services.
194+ let _health_check_handle = tokio:: spawn ( async move {
195+ let app = Router :: new ( ) . route ( "/health_check" , get ( get ( health_check) ) ) ;
196+
197+ info ! ( "Health checker listening on http://0.0.0.0:3002/health_check" ) ;
198+
199+ axum:: Server :: bind ( & "0.0.0.0:3002" . parse ( ) . unwrap ( ) )
200+ . serve ( app. into_make_service ( ) )
201+ . await
202+ . unwrap ( ) ;
203+ } ) ;
204+
205+ app
206+ }
207+
208+ /// It runs a health check on the application.
209+ ///
210+ /// For the time being, we only return ok if the application services were
211+ /// launched. This services has to be launched after launching all the other
212+ /// application services.
213+ ///
214+ /// It's used for container health check when the application is containerized.
215+ async fn health_check ( ) -> Json < Value > {
216+ // todo: check that services are healthy
217+ Json ( json ! ( { "status" : "Ok" } ) )
184218}
0 commit comments