From 5998b64c10b3d5f1867ea875936501574e51878e Mon Sep 17 00:00:00 2001 From: yukang Date: Thu, 23 Nov 2023 00:12:14 +0800 Subject: [PATCH] add deamon subcommand --- Cargo.lock | 26 +++++++++++++++++++++++++- ckb-bin/Cargo.toml | 3 +++ ckb-bin/src/helper.rs | 6 +++--- ckb-bin/src/lib.rs | 18 ++++++++++++------ ckb-bin/src/subcommand/mod.rs | 2 +- 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dc69aea129..3fd6f06a43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -571,10 +571,12 @@ dependencies = [ "ckb-util", "ckb-verification-traits", "clap", + "colored", "ctrlc", "daemonize", "fdlimit", "is-terminal", + "nix 0.24.3", "rayon", "sentry", "serde", @@ -1695,6 +1697,17 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "colored" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2674ec482fbc38012cf31e6c42ba0177b431a0cb6f15fe40efa5aab1bda516f6" +dependencies = [ + "is-terminal", + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "console" version = "0.15.7" @@ -1879,7 +1892,7 @@ version = "3.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82e95fbd621905b854affdc67943b043a0fbb6ed7385fd5a25650d19a8a6cfdf" dependencies = [ - "nix", + "nix 0.27.1", "windows-sys 0.48.0", ] @@ -3268,6 +3281,17 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "nix" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa52e972a9a719cecb6864fb88568781eb706bac2cd1d4f04a648542dbf78069" +dependencies = [ + "bitflags 1.3.2", + "cfg-if 1.0.0", + "libc", +] + [[package]] name = "nix" version = "0.27.1" diff --git a/ckb-bin/Cargo.toml b/ckb-bin/Cargo.toml index f9a0993ae6..2928f0d130 100644 --- a/ckb-bin/Cargo.toml +++ b/ckb-bin/Cargo.toml @@ -46,6 +46,9 @@ sentry = { version = "0.26.0", optional = true } is-terminal = "0.4.7" fdlimit = "0.2.1" ckb-stop-handler = { path = "../util/stop-handler", version = "= 0.113.0-pre" } +[target.'cfg(not(target_os="windows"))'.dependencies] +nix = { version = "0.24.0", default-features = false, features = ["signal"] } +colored = "2.0" [features] deadlock_detection = ["ckb-util/deadlock_detection"] diff --git a/ckb-bin/src/helper.rs b/ckb-bin/src/helper.rs index 7dee9de15d..12b234415f 100644 --- a/ckb-bin/src/helper.rs +++ b/ckb-bin/src/helper.rs @@ -1,4 +1,4 @@ -use ckb_logger::info; +use ckb_logger::{debug, info}; use std::io::{stdin, stdout, Write}; @@ -8,7 +8,7 @@ pub fn deadlock_detection() {} #[cfg(feature = "deadlock_detection")] pub fn deadlock_detection() { use ckb_channel::select; - use ckb_logger::{debug, warn}; + use ckb_logger::warn; use ckb_stop_handler::{new_crossbeam_exit_rx, register_thread}; use ckb_util::parking_lot::deadlock; use std::{thread, time::Duration}; @@ -73,6 +73,6 @@ pub fn prompt(msg: &str) -> String { /// on the number of cores available. pub fn raise_fd_limit() { if let Some(limit) = fdlimit::raise_fd_limit() { - info!("raise_fd_limit newly-increased limit: {}", limit); + debug!("raise_fd_limit newly-increased limit: {}", limit); } } diff --git a/ckb-bin/src/lib.rs b/ckb-bin/src/lib.rs index 2df20d5a99..3c5c82cdda 100644 --- a/ckb-bin/src/lib.rs +++ b/ckb-bin/src/lib.rs @@ -10,9 +10,11 @@ use ckb_build_info::Version; use ckb_logger::{debug, info}; use ckb_network::tokio; use clap::ArgMatches; +use colored::Colorize; use daemonize::Daemonize; use helper::raise_fd_limit; use setup_guard::SetupGuard; +use subcommand::check_process; #[cfg(feature = "with_sentry")] pub(crate) const LOG_TARGET_SENTRY: &str = "sentry"; @@ -73,12 +75,18 @@ fn run_in_daemon( matches: &ArgMatches, ) -> Result<(), ExitCode> { eprintln!("starting CKB in daemon mode ..."); - eprintln!("check status with: `ckb status`"); - eprintln!("stop daemon with: `ckb stop`"); + eprintln!("check status : `{}`", "ckb daemon --check".green()); + eprintln!("stop daemon : `{}`", "ckb daemon --stop".yellow()); assert!(matches!(cmd, cli::CMD_RUN | cli::CMD_MINER)); let cmd_name = if cmd == cli::CMD_RUN { "run" } else { "miner" }; let pid_file = format!("/tmp/ckb-{}.pid", cmd_name); + + if check_process(&pid_file).is_ok() { + eprintln!("{}", format!("ckb {} is already running", cmd_name).red()); + return Ok(()); + } + let pwd = std::env::current_dir()?; let daemon = Daemonize::new() .pid_file(pid_file) @@ -87,11 +95,11 @@ fn run_in_daemon( match daemon.start() { Ok(_) => { - eprintln!("Success, daemonized ..."); + info!("Success, daemonized ..."); run_app_inner(version, bin_name, cmd, matches) } Err(e) => { - eprintln!("daemonize error: {}", e); + info!("daemonize error: {}", e); Err(ExitCode::Failure) } } @@ -103,8 +111,6 @@ fn run_app_inner( cmd: &str, matches: &ArgMatches, ) -> Result<(), ExitCode> { - eprintln!("pwd = {:?}", std::env::current_dir()?); - let is_silent_logging = is_silent_logging(cmd); let (mut handle, mut handle_stop_rx, _runtime) = new_global_runtime(); let setup = Setup::from_matches(bin_name, cmd, matches)?; diff --git a/ckb-bin/src/subcommand/mod.rs b/ckb-bin/src/subcommand/mod.rs index 24ae7a16f2..a9c6b8e99a 100644 --- a/ckb-bin/src/subcommand/mod.rs +++ b/ckb-bin/src/subcommand/mod.rs @@ -11,7 +11,7 @@ mod reset_data; mod run; mod stats; -pub use self::daemon::daemon; +pub use self::daemon::{check_process, daemon}; pub use self::export::export; pub use self::import::import; pub use self::init::init;