Skip to content

Commit

Permalink
fix(cli) stop all services when Kong cannot start
Browse files Browse the repository at this point in the history
- use an xpcall to catch errors and stop all services from there (no
  error checking)
- new test for this behavior

Fix #1530
  • Loading branch information
thibaultcha committed Sep 2, 2016
1 parent 2614119 commit 56c70e2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
30 changes: 22 additions & 8 deletions kong/cmd/start.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,28 @@ local function execute(args)
}))

local dao = DAOFactory(conf)
assert(dao:run_migrations())
assert(prefix_handler.prepare_prefix(conf, args.nginx_conf))
if conf.dnsmasq then
assert(dnsmasq_signals.start(conf))
end
assert(serf_signals.start(conf, dao))
assert(nginx_signals.start(conf))
log("Kong started")
local err
xpcall(function()
assert(dao:run_migrations())
assert(prefix_handler.prepare_prefix(conf, args.nginx_conf))
if conf.dnsmasq then
assert(dnsmasq_signals.start(conf))
end
assert(serf_signals.start(conf, dao))
assert(nginx_signals.start(conf))
log("Kong started")
end, function(e)
log.verbose("could not start Kong, stopping services")
nginx_signals.stop(conf)
serf_signals.stop(conf, dao)
if conf.dnsmasq then
dnsmasq_signals.stop(conf)
end
err = e -- cannot throw from this function
log.verbose("stopped services")
end)

This comment has been minimized.

Copy link
@Tieske

Tieske Sep 13, 2016

Member

@thibaultcha just had to review this code in a merge conflict, nice fix, using xpcall 👍


error(err) -- report to main error handler
end

local lapp = [[
Expand Down
22 changes: 22 additions & 0 deletions spec/02-integration/01-cmd/02-start_stop_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,27 @@ describe("kong start/stop", function()
assert.False(ok)
assert.matches("nginx is already running in "..helpers.test_conf.prefix, stderr, nil, true)
end)
it("stops other services when could not start", function()
local kill = require "kong.cmd.utils.kill"
local thread = helpers.tcp_server(helpers.test_conf.proxy_port)
finally(function()
-- make tcp server receive and close
helpers.proxy_client():send {
method = "GET",
path = "/"
}
thread:join()
end)

local ok, err = helpers.kong_exec("start --conf "..helpers.test_conf_path, {
dnsmasq = true,
dns_resolver = ""
})
assert.False(ok)
assert.matches("Address already in use", err, nil, true)

assert.falsy(kill.is_running(helpers.test_conf.dnsmasq_pid))
assert.falsy(kill.is_running(helpers.test_conf.serf_pid))
end)
end)
end)
1 change: 1 addition & 0 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ local function tcp_server(port, ...)
function(port)
local socket = require "socket"
local server = assert(socket.tcp())
server:settimeout(10)
assert(server:setoption('reuseaddr', true))
assert(server:bind("*", port))
assert(server:listen())
Expand Down

0 comments on commit 56c70e2

Please sign in to comment.