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

feature: restart and health #1366

Merged
merged 17 commits into from
Jul 12, 2016
Merged
2 changes: 2 additions & 0 deletions kong-0.8.2-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ build = {
["kong.cmd.start"] = "kong/cmd/start.lua",
["kong.cmd.check"] = "kong/cmd/check.lua",
["kong.cmd.reload"] = "kong/cmd/reload.lua",
["kong.cmd.restart"] = "kong/cmd/restart.lua",
["kong.cmd.cluster"] = "kong/cmd/cluster.lua",
["kong.cmd.compile"] = "kong/cmd/compile.lua",
["kong.cmd.migrations"] = "kong/cmd/migrations.lua",
["kong.cmd.health"] = "kong/cmd/health.lua",
["kong.cmd.version"] = "kong/cmd/version.lua",
["kong.cmd.utils.log"] = "kong/cmd/utils/log.lua",
["kong.cmd.utils.kill"] = "kong/cmd/utils/kill.lua",
Expand Down
54 changes: 54 additions & 0 deletions kong/cmd/health.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
local conf_loader = require "kong.conf_loader"
local log = require "kong.cmd.utils.log"
local kill = require "kong.cmd.utils.kill"
local pl_stringx = require "pl.stringx"
local pl_path = require "pl.path"
local pl_tablex = require "pl.tablex"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please order those (I know it sounds silly but so far all new modules are ordered and as a result among many other efforts, the code is more readable).

See: https://github.com/Mashape/kong/blob/refactor/cli/kong/cmd/stop.lua#L1-L7


local function is_running(pid_path)
if not pl_path.exists(pid_path) then return nil end
local code = kill(pid_path, "-0")
return code == 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to for the code local.

end

local function execute(args)
local default_conf = assert(conf_loader(args.conf, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this called default_conf? It is the user-defined node's configuration.

prefix = args.prefix
}))
assert(pl_path.exists(default_conf.prefix),
"no such prefix: "..default_conf.prefix)

local pids = {
nginx = default_conf.nginx_pid,
serf = default_conf.serf_pid,
dnsmasq = default_conf.dnsmasq and default_conf.dnsmasq_pid or nil
}

local count = 0
for k, v in pairs(pids) do
local running = is_running(v)
local msg = pl_stringx.ljust(k, 10, ".")..(running and "running" or "not running")
if running then
count = count + 1
log(msg)
else
log.warn(msg)
end
end

assert(count > 0, "Kong is not running")
assert(count == pl_tablex.size(pids), "Some services are not running")
end

local lapp = [[
Usage: kong health [OPTIONS]

Options:
-c,--conf (optional string) configuration file
--prefix (optional string) override prefix directory
]]

return {
lapp = lapp,
execute = execute
}
3 changes: 3 additions & 0 deletions kong/cmd/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Usage: kong COMMAND [OPTIONS]
The available commands are:
start
stop
restart
reload
check
compile
Expand All @@ -26,12 +27,14 @@ Options:
local cmds = {
start = "start",
stop = "stop",
restart = "restart",
reload = "reload",
check = "check",
compile = "compile",
migrations = "migrations",
cluster = "cluster",
version = "version",
health = "health",
roar = "roar"
}

Expand Down
25 changes: 25 additions & 0 deletions kong/cmd/restart.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
local conf_loader = require "kong.conf_loader"
local stop = require "kong.cmd.stop"
local start = require "kong.cmd.start"

local function execute(args)
local conf = assert(conf_loader(args.conf))
args.prefix = conf.prefix -- Required for stop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

args.prefix should be specified in the conf_loader's 2nd argument.

args.graceful = nil -- Restart is always not graceful (reload should be used instead)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this? There is no graceful option in the lapp string so none can be given anyways.


pcall(stop.execute, args)
start.execute(args)
end

local lapp = [[
Usage: kong restart [OPTIONS]

Options:
-c,--conf (optional string) configuration file
--prefix (optional string) override prefix directory
]]

return {
lapp = lapp,
execute = execute
}
5 changes: 3 additions & 2 deletions kong/cmd/stop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@ local function execute(args)

-- load <PREFIX>/kong.conf containing running node's config
local conf = assert(conf_loader(default_conf.kong_conf))
assert(nginx_signals.stop(conf))
assert(nginx_signals.stop(conf, args.graceful))
assert(serf_signals.stop(conf, DAOFactory(conf)))
if conf.dnsmasq then
assert(dnsmasq_signals.stop(conf))
end
log("Stopped")
log("Stopped%s", args.graceful and " gracefully" or "")
end

local lapp = [[
Usage: kong stop [OPTIONS]

Options:
--prefix (optional string) prefix Kong is running at
--graceful (optional boolean) graceful shutdown
]]

return {
Expand Down
4 changes: 2 additions & 2 deletions kong/cmd/utils/nginx_signals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ function _M.start(kong_conf)
return true
end

function _M.stop(kong_conf)
return send_signal(kong_conf.nginx_pid, "QUIT")
function _M.stop(kong_conf, graceful)
return send_signal(kong_conf.nginx_pid, graceful and "QUIT" or "TERM")
end

function _M.reload(kong_conf)
Expand Down
6 changes: 4 additions & 2 deletions spec/02-integration/01-cmd/02-start_stop_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local pl_dir = require "pl.dir"
local pl_path = require "pl.path"
local helpers = require "spec.helpers"

describe("kong start/stop", function()
Expand Down Expand Up @@ -33,6 +31,10 @@ describe("kong start/stop", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path))
assert(helpers.kong_exec("stop --prefix "..helpers.test_conf.prefix))
end)
it("start/stop gracefully", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path))
assert(helpers.kong_exec("stop --graceful --prefix "..helpers.test_conf.prefix))
end)
it("start with inexistent prefix", function()
finally(function()
pcall(helpers.dir.rmtree, "foobar")
Expand Down
28 changes: 28 additions & 0 deletions spec/02-integration/01-cmd/08-restart_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local helpers = require "spec.helpers"

describe("kong restart", function()
before_each(function()
helpers.kill_all()
end)
teardown(function()
helpers.kill_all()
helpers.clean_prefix()
end)

it("restarts if not running", function()
assert(helpers.kong_exec("restart --conf "..helpers.test_conf_path))
end)
it("restarts if already running", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path, {dnsmasq = true, dns_resolver = ""}))

local nginx_pid = assert(helpers.file.read(helpers.test_conf.nginx_pid))
local serf_pid = assert(helpers.file.read(helpers.test_conf.serf_pid))
local dnsmasq_pid = assert(helpers.file.read(helpers.test_conf.dnsmasq_pid))

assert(helpers.kong_exec("restart --trace --conf "..helpers.test_conf_path, {dnsmasq = true, dns_resolver = ""}))

assert.is_not.equal(assert(helpers.file.read(helpers.test_conf.nginx_pid)), nginx_pid)
assert.is_not.equal(assert(helpers.file.read(helpers.test_conf.serf_pid)), serf_pid)
assert.is_not.equal(assert(helpers.file.read(helpers.test_conf.dnsmasq_pid)), dnsmasq_pid)
end)
end)
33 changes: 33 additions & 0 deletions spec/02-integration/01-cmd/09-health_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local helpers = require "spec.helpers"
local prefix_handler = require "kong.cmd.utils.prefix_handler"

describe("kong restart", function()
before_each(function()
helpers.kill_all()
end)
teardown(function()
helpers.kill_all()
helpers.clean_prefix()
end)

it("succeeds when Kong is running", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path, {dnsmasq = true, dns_resolver = ""}))
assert(helpers.kong_exec("health --conf "..helpers.test_conf_path))
end)

describe("errors", function()
it("fails when Kong is not running", function()
assert(prefix_handler.prepare_prefix(helpers.test_conf))
local ok, stderr = helpers.kong_exec("health --conf "..helpers.test_conf_path)
assert.False(ok)
assert.matches("Kong is not running", stderr)
end)
it("fails when a service is not running", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path, {dnsmasq = true, dns_resolver = ""}))
helpers.execute("pkill serf")
local ok, stderr = helpers.kong_exec("health --conf "..helpers.test_conf_path)
assert.False(ok)
assert.matches("Some services are not running", stderr)
end)
end)
end)