-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Changes from 2 commits
f0f61b0
2c3710d
6c2820c
cef1baa
c55c981
de03293
fcf7470
3cd3ece
80906ce
ccea6d3
7217798
30bb41c
e8cb830
c566b76
6e00b81
5d35ee2
82f52d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to for the |
||
end | ||
|
||
local function execute(args) | ||
local default_conf = assert(conf_loader(args.conf, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this called |
||
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 | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
args.graceful = nil -- Restart is always not graceful (reload should be used instead) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of this? There is no |
||
|
||
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 | ||
} |
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) |
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) |
There was a problem hiding this comment.
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