55
66#![ deny( warnings) ]
77#![ deny( missing_docs) ]
8- use std:: time:: Instant ;
8+ use std:: { path :: PathBuf , time:: Instant } ;
99
1010use saluki_app:: bootstrap:: AppBootstrapper ;
11- use saluki_config:: ConfigurationLoader ;
11+ use saluki_config:: { ConfigurationLoader , GenericConfiguration } ;
1212use saluki_error:: { ErrorContext as _, GenericError } ;
1313use tracing:: { error, info, warn} ;
1414
@@ -64,25 +64,41 @@ async fn main() -> Result<(), GenericError> {
6464 . error_context ( "Failed to complete bootstrap phase." ) ?;
6565
6666 // Run the given subcommand.
67- match cli. action {
67+ let maybe_exit_code = run_inner ( cli. action , started, bootstrap_config_path, bootstrap_config) . await ?;
68+
69+ // Drop the bootstrap guard to ensure logs are flushed, etc.
70+ drop ( _bootstrap_guard) ;
71+
72+ // Exit with the specific exit code, if one was provided.
73+ if let Some ( exit_code) = maybe_exit_code {
74+ std:: process:: exit ( exit_code) ;
75+ }
76+
77+ Ok ( ( ) )
78+ }
79+
80+ async fn run_inner (
81+ action : Action , started : Instant , bootstrap_config_path : PathBuf , bootstrap_config : GenericConfiguration ,
82+ ) -> Result < Option < i32 > , GenericError > {
83+ match action {
6884 Action :: Run ( cmd) => {
6985 // Populate our PID file, if configured.
7086 if let Some ( pid_file) = & cmd. pid_file {
7187 let pid = std:: process:: id ( ) ;
7288 if let Err ( e) = std:: fs:: write ( pid_file, pid. to_string ( ) ) {
7389 error ! ( error = %e, path = %pid_file. display( ) , "Failed to update PID file. Exiting." ) ;
74- std :: process :: exit ( 1 ) ;
90+ return Ok ( Some ( 1 ) ) ;
7591 }
7692 }
7793
7894 let exit_code = match handle_run_command ( started, bootstrap_config_path, bootstrap_config) . await {
7995 Ok ( ( ) ) => {
8096 info ! ( "Agent Data Plane stopped." ) ;
81- 0
97+ None
8298 }
8399 Err ( e) => {
84100 error ! ( "{:?}" , e) ;
85- 1
101+ Some ( 1 )
86102 }
87103 } ;
88104
@@ -93,12 +109,14 @@ async fn main() -> Result<(), GenericError> {
93109 }
94110 }
95111
96- std:: process:: exit ( exit_code) ;
112+ if let Some ( exit_code) = exit_code {
113+ return Ok ( Some ( exit_code) ) ;
114+ }
97115 }
98116 Action :: Debug ( cmd) => handle_debug_command ( & bootstrap_config, cmd) . await ,
99117 Action :: Config ( _) => handle_config_command ( & bootstrap_config) . await ,
100118 Action :: Dogstatsd ( cmd) => handle_dogstatsd_command ( & bootstrap_config, cmd) . await ,
101119 }
102120
103- Ok ( ( ) )
121+ Ok ( None )
104122}
0 commit comments