mirror of
https://github.com/Cuprate/cuprate.git
synced 2025-01-10 04:44:53 +00:00
move io_loop
to commands
This commit is contained in:
parent
898548a01d
commit
edc9b60db5
2 changed files with 61 additions and 53 deletions
|
@ -1,10 +1,21 @@
|
|||
use std::{io, thread::sleep, time::Duration};
|
||||
|
||||
use clap::{builder::TypedValueParser, Parser, ValueEnum};
|
||||
|
||||
use tokio::sync::mpsc;
|
||||
use tower::{Service, ServiceExt};
|
||||
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}";
|
||||
|
||||
/// A command received from [`io::stdin`].
|
||||
|
@ -66,3 +77,50 @@ pub fn command_listener(incoming_commands: mpsc::Sender<Command>) -> ! {
|
|||
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!()
|
||||
}
|
||||
|
|
|
@ -28,10 +28,7 @@ use cuprate_consensus_context::{
|
|||
use cuprate_helper::time::secs_to_hms;
|
||||
|
||||
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;
|
||||
|
@ -131,7 +128,7 @@ fn main() {
|
|||
std::thread::spawn(|| commands::command_listener(command_tx));
|
||||
|
||||
// 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
|
||||
.unwrap();
|
||||
});
|
||||
|
@ -155,50 +152,3 @@ fn init_global_rayon_pool(config: &Config) {
|
|||
.build_global()
|
||||
.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!()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue