Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli) stop all services when Kong cannot start #1588

Merged
merged 1 commit into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

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