Skip to content

Commit

Permalink
add deamon subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Nov 22, 2023
1 parent cf0abad commit 5998b64
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
26 changes: 25 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ckb-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
6 changes: 3 additions & 3 deletions ckb-bin/src/helper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ckb_logger::info;
use ckb_logger::{debug, info};

use std::io::{stdin, stdout, Write};

Expand All @@ -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};
Expand Down Expand Up @@ -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);
}
}
18 changes: 12 additions & 6 deletions ckb-bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
Expand All @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion ckb-bin/src/subcommand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5998b64

Please sign in to comment.