@@ -2,11 +2,15 @@ use super::{
22 service:: Service ,
33 types:: { OverallStatus , Priority , ShutdownError , StartupError , Status } ,
44} ;
5- use crate :: {
6- event:: EventRepeater , service:: Watchdog
7- } ;
5+ use crate :: { event:: EventRepeater , service:: Watchdog } ;
86use log:: { error, info, warn} ;
9- use std:: { collections:: HashMap , fmt:: { self , Display } , mem, sync:: { Arc , OnceLock , Weak } , time:: Duration } ;
7+ use std:: {
8+ collections:: HashMap ,
9+ fmt:: { self , Display } ,
10+ mem,
11+ sync:: { Arc , OnceLock , Weak } ,
12+ time:: Duration ,
13+ } ;
1014use tokio:: {
1115 spawn,
1216 sync:: { Mutex , MutexGuard } ,
@@ -20,8 +24,10 @@ pub struct ServiceManagerBuilder {
2024}
2125
2226impl ServiceManagerBuilder {
23- pub fn new ( ) -> Self {
24- Self { services : Vec :: new ( ) }
27+ pub fn new ( ) -> Self {
28+ Self {
29+ services : Vec :: new ( ) ,
30+ }
2531 }
2632
2733 //TODO: When Rust allows async closures, refactor this to use iterator methods instead of for loop
@@ -65,8 +71,8 @@ pub fn new() -> Self {
6571
6672 let result = arc. weak . set ( weak) ;
6773 if result. is_err ( ) {
68- error ! ( "Unable to set ServiceManager's Weak self-reference in ServiceManagerBuilder because it was already set. This should never happen. Shutting down ungracefully to prevent further undefined behavior." ) ;
69- unreachable ! ( "Unable to set ServiceManager's Weak self-reference in ServiceManagerBuilder because it was already set." ) ;
74+ error ! ( "Unable to set ServiceManager's Weak self-reference in ServiceManagerBuilder because it was already set. This should never happen. Shutting down ungracefully to prevent further undefined behavior." ) ;
75+ unreachable ! ( "Unable to set ServiceManager's Weak self-reference in ServiceManagerBuilder because it was already set." ) ;
7076 }
7177
7278 arc
@@ -86,8 +92,7 @@ impl ServiceManager {
8692 ServiceManagerBuilder :: new ( )
8793 }
8894
89- pub async fn manages_service ( & self , service_id : & str ) -> bool
90- {
95+ pub async fn manages_service ( & self , service_id : & str ) -> bool {
9196 for service in self . services . iter ( ) {
9297 let service_lock = service. lock ( ) . await ;
9398
@@ -99,7 +104,10 @@ impl ServiceManager {
99104 false
100105 }
101106
102- pub async fn start_service ( & self , service : Arc < Mutex < dyn Service > > ) -> Result < ( ) , StartupError > {
107+ pub async fn start_service (
108+ & self ,
109+ service : Arc < Mutex < dyn Service > > ,
110+ ) -> Result < ( ) , StartupError > {
103111 let service_id = service. lock ( ) . await . info ( ) . id . clone ( ) ;
104112 if !self . manages_service ( & service_id) . await {
105113 return Err ( StartupError :: ServiceNotManaged ( service_id. clone ( ) ) ) ;
@@ -113,13 +121,18 @@ impl ServiceManager {
113121 }
114122
115123 if self . has_background_task_registered ( & service_id) . await {
116- return Err ( StartupError :: BackgroundTaskAlreadyRunning ( service_id. clone ( ) ) ) ;
124+ return Err ( StartupError :: BackgroundTaskAlreadyRunning (
125+ service_id. clone ( ) ,
126+ ) ) ;
117127 }
118128
119129 let service_status_event = service_lock. info ( ) . status . as_ref ( ) ;
120130 let attachment_result = self . on_status_change . attach ( service_status_event, 2 ) . await ;
121131 if let Err ( err) = attachment_result {
122- return Err ( StartupError :: StatusAttachmentFailed ( service_id. clone ( ) , err) ) ;
132+ return Err ( StartupError :: StatusAttachmentFailed (
133+ service_id. clone ( ) ,
134+ err,
135+ ) ) ;
123136 }
124137
125138 service_lock. info ( ) . status . set ( Status :: Starting ) . await ;
@@ -133,7 +146,10 @@ impl ServiceManager {
133146 }
134147
135148 //TODO: Clean up
136- pub async fn stop_service ( & self , service : Arc < Mutex < dyn Service > > ) -> Result < ( ) , ShutdownError > {
149+ pub async fn stop_service (
150+ & self ,
151+ service : Arc < Mutex < dyn Service > > ,
152+ ) -> Result < ( ) , ShutdownError > {
137153 let service_id = service. lock ( ) . await . info ( ) . id . clone ( ) ;
138154 if !( self . manages_service ( & service_id) . await ) {
139155 return Err ( ShutdownError :: ServiceNotManaged ( service_id. clone ( ) ) ) ;
@@ -155,7 +171,10 @@ impl ServiceManager {
155171 let service_status_event = service_lock. info ( ) . status . as_ref ( ) ;
156172 let detach_result = self . on_status_change . detach ( service_status_event) . await ;
157173 if let Err ( err) = detach_result {
158- return Err ( ShutdownError :: StatusDetachmentFailed ( service_id. clone ( ) , err) ) ;
174+ return Err ( ShutdownError :: StatusDetachmentFailed (
175+ service_id. clone ( ) ,
176+ err,
177+ ) ) ;
159178 }
160179
161180 info ! ( "Stopped service {}" , service_lock. info( ) . name) ;
@@ -305,7 +324,7 @@ impl ServiceManager {
305324 . map ( |line| line. len ( ) )
306325 . max ( )
307326 . unwrap_or ( 0 ) ;
308-
327+
309328 let mut headline = String :: from ( "Status overview\n " ) ;
310329 headline. push_str ( "─" . repeat ( longest_width) . as_str ( ) ) ;
311330 headline. push ( '\n' ) ;
@@ -322,7 +341,10 @@ impl ServiceManager {
322341 Some ( weak) => weak,
323342 None => {
324343 error ! ( "ServiceManager's Weak self-reference was None while initializing service {}. This should never happen. Did you not use a ServiceManagerBuilder? Shutting down ungracefully to prevent further undefined behavior." , service. info( ) . name) ;
325- unreachable ! ( "ServiceManager's Weak self-reference was None while initializing service {}." , service. info( ) . name) ;
344+ unreachable ! (
345+ "ServiceManager's Weak self-reference was None while initializing service {}." ,
346+ service. info( ) . name
347+ ) ;
326348 }
327349 } ;
328350
@@ -335,7 +357,6 @@ impl ServiceManager {
335357 }
336358 } ;
337359
338-
339360 //TODO: Add to config instead of hardcoding duration
340361 let start = service. start ( arc) ;
341362 let timeout_result = timeout ( Duration :: from_secs ( 10 ) , start) . await ;
@@ -351,7 +372,9 @@ impl ServiceManager {
351372 . status
352373 . set ( Status :: FailedToStart ( error. to_string ( ) ) )
353374 . await ;
354- return Err ( StartupError :: FailedToStartService ( service. info ( ) . id . clone ( ) ) ) ;
375+ return Err ( StartupError :: FailedToStartService (
376+ service. info ( ) . id . clone ( ) ,
377+ ) ) ;
355378 }
356379 } ,
357380 Err ( error) => {
@@ -360,7 +383,9 @@ impl ServiceManager {
360383 . status
361384 . set ( Status :: FailedToStart ( error. to_string ( ) ) )
362385 . await ;
363- return Err ( StartupError :: FailedToStartService ( service. info ( ) . id . clone ( ) ) ) ;
386+ return Err ( StartupError :: FailedToStartService (
387+ service. info ( ) . id . clone ( ) ,
388+ ) ) ;
364389 }
365390 }
366391
@@ -386,7 +411,9 @@ impl ServiceManager {
386411 . status
387412 . set ( Status :: FailedToStop ( error. to_string ( ) ) )
388413 . await ;
389- return Err ( ShutdownError :: FailedToStopService ( service. info ( ) . id . clone ( ) ) ) ;
414+ return Err ( ShutdownError :: FailedToStopService (
415+ service. info ( ) . id . clone ( ) ,
416+ ) ) ;
390417 }
391418 } ,
392419 Err ( error) => {
@@ -395,7 +422,9 @@ impl ServiceManager {
395422 . status
396423 . set ( Status :: FailedToStop ( error. to_string ( ) ) )
397424 . await ;
398- return Err ( ShutdownError :: FailedToStopService ( service. info ( ) . id . clone ( ) ) ) ;
425+ return Err ( ShutdownError :: FailedToStopService (
426+ service. info ( ) . id . clone ( ) ,
427+ ) ) ;
399428 }
400429 }
401430
@@ -412,7 +441,10 @@ impl ServiceManager {
412441 service_lock : & MutexGuard < ' _ , dyn Service > ,
413442 service : Arc < Mutex < dyn Service > > ,
414443 ) {
415- if self . has_background_task_registered ( & service_lock. info ( ) . id ) . await {
444+ if self
445+ . has_background_task_registered ( & service_lock. info ( ) . id )
446+ . await
447+ {
416448 return ;
417449 }
418450
@@ -433,14 +465,14 @@ impl ServiceManager {
433465 "Background task of service {} ended unexpectedly! Service will be marked as failed." ,
434466 service. info( ) . name
435467 ) ;
436-
468+
437469 service
438470 . info ( )
439471 . status
440472 . set ( Status :: RuntimeError ( "Background task ended unexpectedly!" . to_string ( ) ) )
441473 . await ;
442474 }
443-
475+
444476 Err ( error) => {
445477 error ! (
446478 "Background task of service {} ended with error: {}. Service will be marked as failed." ,
@@ -470,7 +502,10 @@ impl ServiceManager {
470502 }
471503
472504 async fn stop_background_task ( & self , service_lock : & MutexGuard < ' _ , dyn Service > ) {
473- if !self . has_background_task_registered ( & service_lock. info ( ) . id ) . await {
505+ if !self
506+ . has_background_task_registered ( & service_lock. info ( ) . id )
507+ . await
508+ {
474509 return ;
475510 }
476511
0 commit comments