From b3298dc4023819b9c885014f6e1a4a63845d4920 Mon Sep 17 00:00:00 2001 From: yukang Date: Wed, 29 Nov 2023 12:28:32 +0800 Subject: [PATCH] add more log for deamon stop --- ckb-bin/src/subcommand/daemon.rs | 46 +++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/ckb-bin/src/subcommand/daemon.rs b/ckb-bin/src/subcommand/daemon.rs index f780866509..6a2b6a2408 100644 --- a/ckb-bin/src/subcommand/daemon.rs +++ b/ckb-bin/src/subcommand/daemon.rs @@ -2,8 +2,9 @@ use ckb_app_config::{DaemonArgs, ExitCode}; use colored::*; use nix::sys::signal::{kill, Signal}; use nix::unistd::Pid; -use std::fs; +use std::io::Write; use std::path::PathBuf; +use std::{fs, io}; pub fn daemon(args: DaemonArgs) -> Result<(), ExitCode> { let pid_file = &args.pid_file; @@ -49,27 +50,40 @@ fn kill_process(pid_file: &PathBuf, name: &str) -> Result<(), ExitCode> { .parse::() .map_err(|_| ExitCode::Failure)?; eprintln!( - "stopping {} deamon service {} ...", + "stopping {} deamon service with pid {} ...", name, pid.to_string().red() ); // Send a SIGTERM signal to the process let _ = kill(Pid::from_raw(pid), Some(Signal::SIGTERM)).map_err(|_| ExitCode::Failure); - std::thread::sleep(std::time::Duration::from_secs(20)); - match check_process(pid_file) { - Ok(_) => { - eprintln!( - "{}", - format!( - "ckb daemon service is is still running with pid {}..., stop it now forcefully ...", - pid - ) - .red() - ); - kill(Pid::from_raw(pid), Some(Signal::SIGKILL)).map_err(|_| ExitCode::Failure)?; + let mut wait_time = 15; + eprintln!("{}", "waiting ckb service to stop ...".yellow()); + loop { + let res = check_process(pid_file); + match res { + Ok(_) => { + wait_time -= 1; + eprint!("{}", ".".yellow()); + let _ = io::stderr().flush(); + std::thread::sleep(std::time::Duration::from_secs(1)); + } + _ if wait_time <= 0 => { + eprintln!( + "{}", + format!( + "ckb daemon service is is still running with pid {}..., stop it now forcefully ...", + pid + ) + .red() + ); + kill(Pid::from_raw(pid), Some(Signal::SIGKILL)).map_err(|_| ExitCode::Failure)?; + break; + } + _ => { + break; + } } - _ => {} } - eprintln!("{}", "cbk daemon service stopped successfully".green()); + eprintln!("\n{}", "cbk daemon service stopped successfully".green()); Ok(()) }