move io_loop to commands

This commit is contained in:
Boog900 2025-01-07 02:21:46 +00:00
parent 898548a01d
commit edc9b60db5
No known key found for this signature in database
GPG key ID: 42AB1287CB0041C2
2 changed files with 61 additions and 53 deletions

View file

@ -1,10 +1,21 @@
use std::{io, thread::sleep, time::Duration}; use std::{io, thread::sleep, time::Duration};
use clap::{builder::TypedValueParser, Parser, ValueEnum}; use clap::{builder::TypedValueParser, Parser, ValueEnum};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tower::{Service, ServiceExt};
use tracing::level_filters::LevelFilter; use tracing::level_filters::LevelFilter;
use cuprate_consensus_context::{
BlockChainContextRequest, BlockChainContextResponse, BlockChainContextService,
};
use cuprate_helper::time::secs_to_hms;
use crate::{
constants::PANIC_CRITICAL_SERVICE_ERROR,
logging::{self, CupratedTracingFilter},
statics,
};
const PARSER_TEMPLATE: &str = "{all-args}"; const PARSER_TEMPLATE: &str = "{all-args}";
/// A command received from [`io::stdin`]. /// A command received from [`io::stdin`].
@ -66,3 +77,50 @@ pub fn command_listener(incoming_commands: mpsc::Sender<Command>) -> ! {
line.clear(); line.clear();
} }
} }
/// The [`Command`] handler loop.
pub async fn io_loop(
mut incoming_commands: mpsc::Receiver<Command>,
mut context_service: BlockChainContextService,
) -> ! {
while let Some(command) = incoming_commands.recv().await {
match command {
Command::SetLog {
level,
output_target,
} => {
let modify_output = |filter: &mut CupratedTracingFilter| {
if let Some(level) = level {
filter.level = level;
}
println!("NEW LOG FILTER: {filter}");
};
match output_target {
OutputTarget::File => logging::modify_file_output(modify_output),
OutputTarget::Stdout => logging::modify_stdout_output(modify_output),
}
}
Command::Status => {
let BlockChainContextResponse::Context(blockchain_context) = context_service
.ready()
.await
.expect(PANIC_CRITICAL_SERVICE_ERROR)
.call(BlockChainContextRequest::Context)
.await
.expect(PANIC_CRITICAL_SERVICE_ERROR)
else {
unreachable!();
};
let context = blockchain_context.unchecked_blockchain_context();
let uptime = statics::START_INSTANT.elapsed().unwrap_or_default();
let (hours, minutes, second) = secs_to_hms(uptime.as_secs());
println!("STATUS:\n uptime: {hours}h {minutes}m {second}s,\n height: {},\n top_hash: {}", context.chain_height, hex::encode(context.top_hash));
}
}
}
unreachable!()
}

View file

@ -28,10 +28,7 @@ use cuprate_consensus_context::{
use cuprate_helper::time::secs_to_hms; use cuprate_helper::time::secs_to_hms;
use crate::{ use crate::{
commands::{Command, OutputTarget}, config::Config, constants::PANIC_CRITICAL_SERVICE_ERROR, logging::CupratedTracingFilter,
config::Config,
constants::PANIC_CRITICAL_SERVICE_ERROR,
logging::CupratedTracingFilter,
}; };
mod blockchain; mod blockchain;
@ -131,7 +128,7 @@ fn main() {
std::thread::spawn(|| commands::command_listener(command_tx)); std::thread::spawn(|| commands::command_listener(command_tx));
// Wait on the io_loop, spawned on a separate task as this improves performance. // Wait on the io_loop, spawned on a separate task as this improves performance.
tokio::spawn(io_loop(command_rx, context_svc)) tokio::spawn(commands::io_loop(command_rx, context_svc))
.await .await
.unwrap(); .unwrap();
}); });
@ -155,50 +152,3 @@ fn init_global_rayon_pool(config: &Config) {
.build_global() .build_global()
.unwrap(); .unwrap();
} }
/// The [`Command`] handler loop.
async fn io_loop(
mut incoming_commands: mpsc::Receiver<Command>,
mut context_service: BlockChainContextService,
) -> ! {
while let Some(command) = incoming_commands.recv().await {
match command {
Command::SetLog {
level,
output_target,
} => {
let modify_output = |filter: &mut CupratedTracingFilter| {
if let Some(level) = level {
filter.level = level;
}
println!("NEW LOG FILTER: {filter}");
};
match output_target {
OutputTarget::File => logging::modify_file_output(modify_output),
OutputTarget::Stdout => logging::modify_stdout_output(modify_output),
}
}
Command::Status => {
let BlockChainContextResponse::Context(blockchain_context) = context_service
.ready()
.await
.expect(PANIC_CRITICAL_SERVICE_ERROR)
.call(BlockChainContextRequest::Context)
.await
.expect(PANIC_CRITICAL_SERVICE_ERROR)
else {
unreachable!();
};
let context = blockchain_context.unchecked_blockchain_context();
let uptime = statics::START_INSTANT.elapsed().unwrap_or_default();
let (hours, minutes, second) = secs_to_hms(uptime.as_secs());
println!("STATUS:\n uptime: {hours}h {minutes}m {second}s,\n height: {},\n top_hash: {}", context.chain_height, hex::encode(context.top_hash));
}
}
}
unreachable!()
}