From f4b2d115a7e04887fc78974165178c2ac153b752 Mon Sep 17 00:00:00 2001 From: "xiang.zhou" Date: Tue, 22 Aug 2023 13:33:43 +0800 Subject: [PATCH] feat:waiting exit when using stop cmd --- src/argparse.c | 19 +++++++++++++++---- src/daemon.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/main.c | 2 ++ 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/argparse.c b/src/argparse.c index c8c3d72d..f1d20e6e 100644 --- a/src/argparse.c +++ b/src/argparse.c @@ -63,7 +63,7 @@ const char *usage_text = "OPTIONS:\n" " -d, --daemon run as daemon process\n" " -h, --help show this help message\n" -" --stop stop running neuron\n" +" stop stop running neuron\n" " --log log to the stdout\n" " --log_level default log level(DEBUG,NOTICE)\n" " --reset-password reset dashboard to use default password\n" @@ -137,6 +137,15 @@ static inline bool file_exists(const char *const path) return -1 != stat(path, &buf); } +static inline int load_spec_arg(int argc, char *argv[], neu_cli_args_t *args) +{ + int ret = 0; + if (argc > 1 && strcmp(argv[1], "stop") == 0) { + args->stop = true; + } + return ret; +} + static inline int load_env(neu_cli_args_t *args, char **log_level_out, char **config_dir_out, char **plugin_dir_out) { @@ -394,6 +403,11 @@ void neu_cli_args_init(neu_cli_args_t *args, int argc, char *argv[]) int c = 0; int option_index = 0; + if (load_spec_arg(argc, argv, args) < 0) { + ret = 1; + goto quit; + } + // load config file if (load_config_file(argc, argv, long_options, opts, args, &log_level, &config_dir, &plugin_dir) < 0) { @@ -459,9 +473,6 @@ void neu_cli_args_init(neu_cli_args_t *args, int argc, char *argv[]) } plugin_dir = strdup(optarg); break; - case 's': - args->stop = true; - break; case '?': default: usage(); diff --git a/src/daemon.c b/src/daemon.c index bb344d43..af70474a 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -31,9 +31,13 @@ #include #include "utils/log.h" +#include "utils/time.h" #include "daemon.h" +#define STOP_TIMEOUT 20000 +#define STOP_TICK 2000 + void daemonize() { int nfile; @@ -95,6 +99,45 @@ static inline int lock_file(int fd) return fcntl(fd, F_SETLK, &fl); } +static int neuron_wait_exit(uint32_t ms) +{ + uint32_t tick = 0; + int ret = -1; + int fd = -1; + + fd = open(NEURON_DAEMON_LOCK_FNAME, O_WRONLY); + if (fd < 0) { + nlog_error("cannot open %s reason: %s\n", NEURON_DAEMON_LOCK_FNAME, + strerror(errno)); + exit(1); + } + + do { + // try to lock file + if (lock_file(fd) < 0) { + if (EACCES == errno || EAGAIN == errno) { + // a neuron process is running + printf("neuron is stopping.\n"); + } else { + close(fd); + nlog_error("cannot lock %s reason: %s\n", + NEURON_DAEMON_LOCK_FNAME, strerror(errno)); + exit(1); + } + } else { + printf("neuron had stopped.\n"); + ret = 0; + break; + } + + neu_msleep(STOP_TICK); + tick += STOP_TICK; + + } while (tick < ms); + close(fd); + return ret; +} + int neuron_already_running() { int fd = -1; @@ -160,7 +203,9 @@ int neuron_stop() } if (0 == kill((pid_t)(-gid), SIGINT)) { - ret = 0; + if (neuron_wait_exit(STOP_TIMEOUT) == 0) { + ret = 0; + } } else { nlog_error("cannot kill gpid:%ld reason: %s\n", (long) gid, strerror(errno)); diff --git a/src/main.c b/src/main.c index e665420c..7bcd71ea 100644 --- a/src/main.c +++ b/src/main.c @@ -115,6 +115,8 @@ int main(int argc, char *argv[]) nlog_notice("neuron stop ret=%d", rv); if (rv == 0) { printf("neuron stop successfully.\n"); + } else { + printf("neuron stop failed.\n"); } } else { nlog_fatal("neuron process already running, exit.");