Implement daemon-mode in tari_base_node
@@ -225,17 +225,23 @@ fn main_inner() -> Result<(), ExitCodes> {
}
// Run, node, run!
+ let base_node_handle;
+ if !bootstrap.daemon_mode {
let parser = Parser::new(rt.handle().clone(), &ctx, &node_config);
cli::print_banner(parser.get_commands(), 3);
- let base_node_handle = rt.spawn(ctx.run());
+ base_node_handle = rt.spawn(ctx.run());
info!(
target: LOG_TARGET,
"Node has been successfully configured and initialized. Starting CLI loop."
);
- cli_loop(parser, shutdown);
-
+ cli_loop(Some(parser), shutdown);
+ } else {
+ println!("Node has been successfully configured and initialized in daemon mode. Starting CLI loop.");
+ base_node_handle = rt.spawn(ctx.run());
+ cli_loop(None, shutdown);
+ }
match rt.block_on(base_node_handle) {
Ok(_) => info!(target: LOG_TARGET, "Node shutdown successfully."),
Err(e) => error!(target: LOG_TARGET, "Node has crashed: {}", e),
@@ -273,7 +279,7 @@ async fn run_grpc(
///
/// ## Returns
/// Doesn't return anything
-fn cli_loop(parser: Parser, mut shutdown: Shutdown) {
+fn cli_loop(parser: Option<Parser>, mut shutdown: Shutdown) {
let cli_config = Config::builder()
.history_ignore_space(true)
.completion_type(CompletionType::List)
@@ -281,6 +287,8 @@ fn cli_loop(parser: Parser, mut shutdown: Shutdown) {
.output_stream(OutputStreamType::Stdout)
.build();
let mut rustyline = Editor::with_config(cli_config);
+ match parser {
+ Some(parser) => {
rustyline.set_helper(Some(parser));
loop {
let readline = rustyline.readline(">> ");
@@ -312,4 +320,33 @@ fn cli_loop(parser: Parser, mut shutdown: Shutdown) {
break;
};
}
+ },
+ None => {
+ loop {
+ let readline = rustyline.readline("");
+ match readline {
+ Ok(_) => {},
+ Err(ReadlineError::Interrupted) => {
+ // shutdown section. Will shutdown all interfaces when ctrl-c was pressed
+ println!("The node is shutting down because Ctrl+C was received...");
+ info!(
+ target: LOG_TARGET,
+ "Termination signal received from user. Shutting node down."
+ );
+ if shutdown.trigger().is_err() {
+ error!(target: LOG_TARGET, "Shutdown signal failed to trigger");
+ };
+ break;
+ },
+ Err(err) => {
+ println!("Error: {:?}", err);
+ break;
+ },
+ }
+ if shutdown.is_triggered() {
+ break;
+ };
+ }
+ },
+ }
}
