@@ -7,7 +7,7 @@ use moon_api::Launchpad;
77use moon_app_context:: AppContext ;
88use moon_cache:: CacheEngine ;
99use moon_codegen:: CodeGenerator ;
10- use moon_common:: is_formatted_output;
10+ use moon_common:: { is_docker , is_formatted_output, is_remote } ;
1111use moon_config:: { ExtensionsConfig , InheritedTasksManager , ToolchainsConfig , WorkspaceConfig } ;
1212use moon_config_loader:: ConfigLoader ;
1313use moon_console:: { Console , MoonReporter , create_console_theme} ;
@@ -46,6 +46,7 @@ pub struct MoonSession {
4646
4747 // Lazy components
4848 pub ( crate ) cache_engine : OnceLock < Arc < CacheEngine > > ,
49+ // pub(crate) daemon_client: OnceCell<Option<DaemonClient>>,
4950 pub ( crate ) extension_registry : OnceCell < Arc < ExtensionRegistry > > ,
5051 pub ( crate ) project_graph : OnceLock < Arc < ProjectGraph > > ,
5152 pub ( crate ) task_graph : OnceLock < Arc < TaskGraph > > ,
@@ -75,6 +76,7 @@ impl MoonSession {
7576 config_dir : PathBuf :: new ( ) ,
7677 config_loader : ConfigLoader :: default ( ) ,
7778 console : Console :: new ( cli. quiet || is_formatted_output ( ) ) ,
79+ // daemon_client: OnceCell::new(),
7880 extensions_config : Arc :: new ( ExtensionsConfig :: default ( ) ) ,
7981 extension_registry : OnceCell :: new ( ) ,
8082 moon_env : Arc :: new ( MoonEnvironment :: default ( ) ) ,
@@ -126,13 +128,18 @@ impl MoonSession {
126128 }
127129
128130 pub async fn connect_to_daemon ( & self ) -> miette:: Result < Option < DaemonClient > > {
129- if !self . workspace_config . daemon {
131+ if !self . is_daemon_allowed ( ) {
130132 return Ok ( None ) ;
131133 }
132134
133- let client = self . get_daemon_connector ( ) ?. connect ( ) . await ?;
135+ // let client = self
136+ // .daemon_client
137+ // .get_or_try_init(async move || self.get_daemon_connector()?.connect().await)
138+ // .await?;
134139
135- Ok ( Some ( client) )
140+ // Ok(client.clone())
141+
142+ self . get_daemon_connector ( ) ?. connect ( ) . await
136143 }
137144
138145 pub async fn create_workspace_graph_context ( & self ) -> miette:: Result < WorkspaceBuilderContext > {
@@ -271,6 +278,21 @@ impl MoonSession {
271278 . map ( Arc :: clone)
272279 }
273280
281+ pub fn is_daemon_allowed ( & self ) -> bool {
282+ self . workspace_config . daemon && self . is_pipeline_command ( ) && !is_docker ( )
283+ }
284+
285+ pub fn is_pipeline_command ( & self ) -> bool {
286+ matches ! (
287+ self . cli. command,
288+ Commands :: Ci ( _)
289+ | Commands :: Check ( _)
290+ | Commands :: Exec ( _)
291+ | Commands :: Run ( _)
292+ | Commands :: Sync { .. }
293+ )
294+ }
295+
274296 pub fn is_telemetry_enabled ( & self ) -> bool {
275297 self . workspace_config . telemetry
276298 }
@@ -389,39 +411,37 @@ impl AppSession for MoonSession {
389411
390412 analyze:: extract_repo_info ( & vcs) . await ?;
391413
392- // Preload
414+ // Preload components
393415 if self . requires_workspace_configured ( ) {
394416 let _ = self . get_cache_engine ( ) ?;
395417 }
396418
419+ // Start the daemon in the background
420+ if self . is_daemon_allowed ( ) {
421+ self . get_daemon_connector ( ) ?. start_daemon ( false ) . await ?;
422+ }
423+
397424 Ok ( None )
398425 }
399426
400427 async fn execute ( & mut self ) -> AppResult {
401- let is_exec_command = matches ! (
402- self . cli. command,
403- Commands :: Ci ( _)
404- | Commands :: Check ( _)
405- | Commands :: Exec ( _)
406- | Commands :: Run ( _)
407- | Commands :: Sync { .. }
408- ) ;
409-
410428 // Check for a new version and log to the console
411- if self . is_telemetry_enabled ( ) && is_exec_command {
429+ if self . is_telemetry_enabled ( ) && self . is_pipeline_command ( ) {
412430 execute:: check_for_new_version ( & self , & self . toolchains_config . moon . manifest_url )
413431 . await ?;
414432 }
415433
416- // Start the daemon in the background
417- if self . workspace_config . daemon && is_exec_command {
418- self . get_daemon_connector ( ) ?. start_daemon ( ) . await ?;
419- }
420-
421434 Ok ( None )
422435 }
423436
424437 async fn shutdown ( & mut self ) -> AppResult {
438+ // Stop the daemon if it's running
439+ if is_remote ( )
440+ && let Ok ( Some ( mut daemon) ) = self . connect_to_daemon ( ) . await
441+ {
442+ daemon. stop ( ) . await ?;
443+ }
444+
425445 // Ensure all child processes have finished running
426446 ProcessRegistry :: instance ( )
427447 . wait_for_running_to_shutdown ( )
0 commit comments