From 88107db2c0dac0a8fca8dc2d0bbfd8bbd75d308e Mon Sep 17 00:00:00 2001 From: "cooper.wu" Date: Wed, 3 Jan 2024 20:58:27 +0800 Subject: [PATCH] fix: restart failed (#10696) --- apisix/cli/ops.lua | 34 +++++++++++++++++++++++++++++----- t/cli/test_cmd.sh | 4 ++-- t/cli/test_validate_config.sh | 2 +- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua index 0eaebae56c43..836c87bce521 100644 --- a/apisix/cli/ops.lua +++ b/apisix/cli/ops.lua @@ -767,6 +767,22 @@ local function cleanup(env) end +local function sleep(n) + execute("sleep " .. tonumber(n)) +end + + +local function check_running(env) + local pid_path = env.apisix_home .. "/logs/nginx.pid" + local pid = util.read_file(pid_path) + pid = tonumber(pid) + if not pid then + return false, nil + end + return true, pid +end + + local function start(env, ...) cleanup(env) @@ -791,10 +807,18 @@ local function start(env, ...) util.die(logs_path, " is not directory nor symbol link") end - -- check running - local pid_path = env.apisix_home .. "/logs/nginx.pid" - local pid = util.read_file(pid_path) - pid = tonumber(pid) + -- check running and wait old apisix stop + local pid = nil + for i = 1, 30 do + local running + running, pid = check_running(env) + if not running then + break + else + sleep(0.1) + end + end + if pid then if pid <= 0 then print("invalid pid") @@ -805,7 +829,7 @@ local function start(env, ...) local ok, err, err_no = signal.kill(pid, signone) if ok then - print("APISIX is running...") + print("the old APISIX is still running, the new one will not start") return -- no such process elseif err_no ~= errno.ESRCH then diff --git a/t/cli/test_cmd.sh b/t/cli/test_cmd.sh index 2f3a46cd2070..fa93465a66e7 100755 --- a/t/cli/test_cmd.sh +++ b/t/cli/test_cmd.sh @@ -24,7 +24,7 @@ git checkout conf/config.yaml # check restart with old nginx.pid exist echo "-1" > logs/nginx.pid out=$(./bin/apisix start 2>&1 || true) -if echo "$out" | grep "APISIX is running"; then +if echo "$out" | grep "the old APISIX is still running"; then rm logs/nginx.pid echo "failed: should reject bad nginx.pid" exit 1 @@ -50,7 +50,7 @@ echo "pass: no corresponding process" # check running when run repeatedly out=$(make run; make run || true) -if ! echo "$out" | grep "APISIX is running"; then +if ! echo "$out" | grep "the old APISIX is still running"; then echo "failed: should find APISIX running" exit 1 fi diff --git a/t/cli/test_validate_config.sh b/t/cli/test_validate_config.sh index 8db581684332..0379b986c5b2 100755 --- a/t/cli/test_validate_config.sh +++ b/t/cli/test_validate_config.sh @@ -142,7 +142,7 @@ nginx_config: # apisix restart out=$(./bin/apisix restart 2>&1 || true) -if ! (echo "$out" | grep "\[emerg\] unknown directive \"notexist\"") && ! (echo "$out" | grep "APISIX is running"); then +if ! (echo "$out" | grep "\[emerg\] unknown directive \"notexist\"") && ! (echo "$out" | grep "the old APISIX is still running"); then echo "failed: should restart failed when configuration invalid" exit 1 fi