Skip to content

Commit

Permalink
Merge pull request #1421 from ZhouBox/main
Browse files Browse the repository at this point in the history
feat:add cmd about stopping running neuron
  • Loading branch information
fengzeroz authored Jul 27, 2023
2 parents d7f1ae1 + 5cbc42a commit 2d92f84
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/argparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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"
" --log log to the stdout\n"
" --log_level <LEVEL> default log level(DEBUG,NOTICE)\n"
" --reset-password reset dashboard to use default password\n"
Expand Down Expand Up @@ -151,6 +152,7 @@ void neu_cli_args_init(neu_cli_args_t *args, int argc, char *argv[])
{ "disable_auth", no_argument, NULL, 'a' },
{ "config_dir", required_argument, NULL, 'c' },
{ "plugin_dir", required_argument, NULL, 'p' },
{ "stop", no_argument, NULL, 's' },
{ NULL, 0, NULL, 0 },
};

Expand Down Expand Up @@ -200,6 +202,9 @@ void neu_cli_args_init(neu_cli_args_t *args, int argc, char *argv[])
case 'p':
plugin_dir = strdup(optarg);
break;
case 's':
args->stop = true;
break;
case '?':
default:
usage();
Expand Down
1 change: 1 addition & 0 deletions src/argparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ typedef struct {
char * log_init_file;
char * config_dir;
char * plugin_dir;
bool stop;
} neu_cli_args_t;

/** Parse command line arguments.
Expand Down
31 changes: 31 additions & 0 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,34 @@ int neuron_already_running()

return 0;
}

int neuron_stop()
{
int ret = -1;
int fd = -1;
char buf[16] = { 0 };
fd = open(NEURON_DAEMON_LOCK_FNAME, O_RDONLY);
if (fd < 0) {
nlog_error("cannot open %s reason: %s\n", NEURON_DAEMON_LOCK_FNAME,
strerror(errno));
return ret;
}
int size = read(fd, buf, sizeof(buf) - 1);
if (size <= 0) {
nlog_error("cannot read %s reason: %s\n", NEURON_DAEMON_LOCK_FNAME,
strerror(errno));
ret = -1;
} else {
long pid = -1;
if (sscanf(buf, "%ld", &pid) == 1) {
if (kill((pid_t) pid, SIGINT) == -1) {
ret = -1;
} else {
ret = 0;
}
}
}
close(fd);

return ret;
}
2 changes: 2 additions & 0 deletions src/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ void daemonize();
*/
int neuron_already_running();

int neuron_stop();

#ifdef __cplusplus
}
#endif
Expand Down
18 changes: 16 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,23 @@ int main(int argc, char *argv[])
zlog_level_switch(neuron, default_log_level);

if (neuron_already_running()) {
nlog_fatal("neuron process already running, exit.");
rv = -1;
if (args.stop) {
rv = neuron_stop();
nlog_notice("neuron stop ret=%d", rv);
if (rv == 0) {
printf("neuron stop successfully.\n");
}
} else {
nlog_fatal("neuron process already running, exit.");
rv = -1;
}
goto main_end;
} else {
if (args.stop) {
rv = 0;
printf("neuron no running.\n");
goto main_end;
}
}

for (size_t i = 0; i < args.restart; ++i) {
Expand Down

0 comments on commit 2d92f84

Please sign in to comment.