Merge pull request #1464 from ZhouBox/main

feat:waiting exit when using stop cmd
This commit is contained in:
fengzeroz
2023-08-24 14:29:48 +08:00
committed by GitHub
3 changed files with 63 additions and 5 deletions
+15 -4
View File
@@ -64,7 +64,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 <LEVEL> default log level(DEBUG,NOTICE)\n"
" --reset-password reset dashboard to use default password\n"
@@ -138,6 +138,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)
{
@@ -341,6 +350,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) < 0) {
ret = 1;
@@ -405,9 +419,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();
+46 -1
View File
@@ -31,9 +31,13 @@
#include <string.h>
#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));
+2
View File
@@ -117,6 +117,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.");