Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add status output to logs in non-interactive mode #3244

Merged
merged 2 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions applications/tari_base_node/src/command_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ use tari_p2p::auto_update::SoftwareUpdaterHandle;
use tari_wallet::util::emoji::EmojiId;
use tokio::{runtime, sync::watch};

pub enum StatusOutput {
Log,
Full,
}

pub struct CommandHandler {
executor: runtime::Handle,
config: Arc<GlobalConfig>,
Expand Down Expand Up @@ -95,7 +100,7 @@ impl CommandHandler {
}
}

pub fn status(&self) {
pub fn status(&self, output: StatusOutput) {
let mut state_info = self.state_machine_info.clone();
let mut node = self.node_service.clone();
let mut mempool = self.mempool_service.clone();
Expand Down Expand Up @@ -171,8 +176,14 @@ impl CommandHandler {
),
);

info!(target: "base_node::app::status", "{}", status_line);
println!("{}", status_line);
let target = "base_node::app::status";
match output {
StatusOutput::Full => {
println!("{}", status_line);
info!(target: target, "{}", status_line);
},
StatusOutput::Log => info!(target: target, "{}", status_line),
};
});
}

Expand Down
52 changes: 36 additions & 16 deletions applications/tari_base_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ mod recovery;
mod status_line;
mod utils;

use crate::command_handler::CommandHandler;
use futures::{pin_mut, FutureExt};
use crate::command_handler::{CommandHandler, StatusOutput};
use futures::{future::Fuse, pin_mut, FutureExt};
use log::*;
use parser::Parser;
use rustyline::{config::OutputStreamType, error::ReadlineError, CompletionType, Config, EditMode, Editor};
Expand All @@ -114,7 +114,11 @@ use tari_app_utilities::{
use tari_common::{configuration::bootstrap::ApplicationType, ConfigBootstrap, GlobalConfig};
use tari_comms::{peer_manager::PeerFeatures, tor::HiddenServiceControllerError};
use tari_shutdown::{Shutdown, ShutdownSignal};
use tokio::{runtime, task, time};
use tokio::{
runtime,
task,
time::{self, Delay},
};
use tonic::transport::Server;

const LOG_TARGET: &str = "base_node::app";
Expand Down Expand Up @@ -221,10 +225,11 @@ async fn run_node(node_config: Arc<GlobalConfig>, bootstrap: ConfigBootstrap) ->
}

// Run, node, run!
let command_handler = Arc::new(CommandHandler::new(runtime::Handle::current(), &ctx));
if bootstrap.non_interactive_mode {
task::spawn(status_loop(command_handler, shutdown));
println!("Node started in non-interactive mode (pid = {})", process::id());
} else {
let command_handler = Arc::new(CommandHandler::new(runtime::Handle::current(), &ctx));
let parser = Parser::new(command_handler);
cli::print_banner(parser.get_commands(), 3);

Expand Down Expand Up @@ -291,7 +296,31 @@ async fn read_command(mut rustyline: Editor<Parser>) -> Result<(String, Editor<P
.expect("Could not spawn rustyline task")
}

/// Runs the Base Node
fn status_interval(start_time: Instant) -> Fuse<Delay> {
let duration = match start_time.elapsed().as_secs() {
0..=120 => Duration::from_secs(5),
_ => Duration::from_secs(30),
};
time::delay_for(duration).fuse()
}

async fn status_loop(command_handler: Arc<CommandHandler>, shutdown: Shutdown) {
let start_time = Instant::now();
let mut shutdown_signal = shutdown.to_signal();
loop {
let mut interval = status_interval(start_time);
futures::select! {
_ = interval => {
command_handler.status(StatusOutput::Log);
},
_ = shutdown_signal => {
break;
}
}
}
}

/// Runs the Base Node CLI loop
/// ## Parameters
/// `parser` - The parser to process input commands
/// `shutdown` - The trigger for shutting down
Expand All @@ -315,16 +344,7 @@ async fn cli_loop(parser: Parser, mut shutdown: Shutdown) {
let start_time = Instant::now();
let mut software_update_notif = command_handler.get_software_updater().new_update_notifier().clone();
loop {
let delay_time = if start_time.elapsed() < Duration::from_secs(120) {
Duration::from_secs(2)
} else if start_time.elapsed() < Duration::from_secs(300) {
Duration::from_secs(10)
} else {
Duration::from_secs(30)
};

let mut interval = time::delay_for(delay_time).fuse();

let mut interval = status_interval(start_time);
futures::select! {
res = read_command_fut => {
match res {
Expand Down Expand Up @@ -355,7 +375,7 @@ async fn cli_loop(parser: Parser, mut shutdown: Shutdown) {
}
}
_ = interval => {
command_handler.status();
command_handler.status(StatusOutput::Full);
},
_ = shutdown_signal => {
break;
Expand Down
4 changes: 2 additions & 2 deletions applications/tari_base_node/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use super::LOG_TARGET;
use crate::command_handler::{CommandHandler, Format};
use crate::command_handler::{CommandHandler, Format, StatusOutput};
use futures::future::Either;
use log::*;
use rustyline::{
Expand Down Expand Up @@ -174,7 +174,7 @@ impl Parser {
);
},
Status => {
self.command_handler.status();
self.command_handler.status(StatusOutput::Full);
},
GetStateInfo => {
self.command_handler.state_info();
Expand Down