From 9bf6503e750da7c34c64dc39e5bb1d7e3dda763b Mon Sep 17 00:00:00 2001 From: Denis Kolodin Date: Thu, 31 Mar 2022 10:06:54 +0300 Subject: [PATCH] fix: separate watch loop for the non-interactive mode (#3979) Description --- The PR adds the separate loop for the `watch` command in the non-interactive mode. Motivation and Context --- The new `CliLoop` struct uses the same routine in both: interactive and non-interactive mode, but it failed if started under `systemd`. How Has This Been Tested? --- Manually --- .../tari_base_node/src/commands/cli_loop.rs | 43 ++++++++++++++++--- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/applications/tari_base_node/src/commands/cli_loop.rs b/applications/tari_base_node/src/commands/cli_loop.rs index f95941bf06..062e365e54 100644 --- a/applications/tari_base_node/src/commands/cli_loop.rs +++ b/applications/tari_base_node/src/commands/cli_loop.rs @@ -80,13 +80,14 @@ impl CliLoop { pub async fn cli_loop(mut self) { cli::print_banner(self.commands.clone(), 3); - // TODO: Check for a new version here - while !self.done { - self.watch_loop().await; - if self.non_interactive { - break; + if self.non_interactive { + self.watch_loop_non_interactive().await; + } else { + // TODO: Check for a new version here + while !self.done { + self.watch_loop().await; + self.execute_command().await; } - self.execute_command().await; } } @@ -164,6 +165,36 @@ impl CliLoop { } } + async fn watch_loop_non_interactive(&mut self) { + if let Some(command) = self.watch_task.take() { + let mut interrupt = signal::ctrl_c().fuse().boxed(); + let config = self.context.config.clone(); + let line = command.line(); + let interval = command + .interval + .map(Duration::from_secs) + .unwrap_or(config.base_node_status_line_interval); + if let Err(err) = self.context.handle_command_str(line).await { + println!("Wrong command to watch `{}`. Failed with: {}", line, err); + } else { + loop { + let interval = time::sleep(interval); + tokio::select! { + _ = interval => { + if let Err(err) = self.context.handle_command_str(line).await { + println!("Watched command `{}` failed: {}", line, err); + } + continue; + }, + _ = &mut interrupt => { + break; + } + } + } + } + } + } + async fn handle_line(&mut self, line: String) { // Reset the interruption flag if the command entered. self.first_signal = false;