Skip to content

Commit

Permalink
fix: restart failed (#10696)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanqingwu authored Jan 3, 2024
1 parent bf9e9f8 commit 88107db
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
34 changes: 29 additions & 5 deletions apisix/cli/ops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions t/cli/test_cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion t/cli/test_validate_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 88107db

Please sign in to comment.