Skip to content

Commit

Permalink
feat: kong quit for graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Apr 6, 2015
1 parent 219fa7f commit 1580a2f
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 21 deletions.
1 change: 1 addition & 0 deletions bin/kong
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local infos = cutils.get_infos()
local commands = {
db = "kong.cli.db",
stop = "kong.cli.stop",
quit = "kong.cli.quit",
start = "kong.cli.start",
reload = "kong.cli.reload",
config = "kong.cli.config",
Expand Down
1 change: 1 addition & 0 deletions kong-0.2.0-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ build = {
["kong.cli.utils.signal"] = "src/cli/utils/signal.lua",
["kong.cli.db"] = "src/cli/db.lua",
["kong.cli.config"] = "src/cli/config.lua",
["kong.cli.quit"] = "src/cli/quit.lua",
["kong.cli.stop"] = "src/cli/stop.lua",
["kong.cli.start"] = "src/cli/start.lua",
["kong.cli.reload"] = "src/cli/reload.lua",
Expand Down
19 changes: 19 additions & 0 deletions src/cli/quit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env lua

local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local signal = require "kong.cli.utils.signal"
local args = require("lapp")(string.format([[
Graceful shutdown
Usage: kong stop [options]
Options:
-c,--config (default %s) configuration file
]], constants.CLI.GLOBAL_KONG_CONF))

if signal.send_signal(args.config, "quit") then
cutils.logger:success("Stopped")
else
cutils.logger:error_exit("Could not gracefully stop Kong")
end
17 changes: 15 additions & 2 deletions src/cli/restart.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env lua

local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local signal = require "kong.cli.utils.signal"
local args = require("lapp")(string.format([[
Usage: kong restart [options]
Expand All @@ -9,5 +10,17 @@ Options:
-c,--config (default %s) configuration file
]], constants.CLI.GLOBAL_KONG_CONF))

signal.send_stop(args.config)
signal.send_start(args.config)
-- Check if running, will exit if not
signal.is_running(args.config)

if not signal.send_signal(args.config, "stop") then
cutils.logger:error_exit("Could not stop Kong")
end

signal.prepare_kong(args_config)

if not signal.send_signal(args.config) then
cutils.logger:error_exit("Could not restart Kong")
end

cutils.logger:success("Restarted")
7 changes: 6 additions & 1 deletion src/cli/start.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env lua

local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local signal = require "kong.cli.utils.signal"
local args = require("lapp")(string.format([[
Usage: kong start [options]
Expand All @@ -9,4 +10,8 @@ Options:
-c,--config (default %s) configuration file
]], constants.CLI.GLOBAL_KONG_CONF))

signal.send_start(args.config)
if signal.send_signal(args.config) then
cutils.logger:success("Started")
else
cutils.logger:error_exit("Could not start Kong")
end
13 changes: 12 additions & 1 deletion src/cli/stop.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#!/usr/bin/env lua

local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local signal = require "kong.cli.utils.signal"
local args = require("lapp")(string.format([[
Fast shutdown
Usage: kong stop [options]
Options:
-c,--config (default %s) configuration file
]], constants.CLI.GLOBAL_KONG_CONF))

signal.send_stop(args.config)
-- Check if running, will exit if not
signal.is_running(args.config)

-- Send 'stop' signal (fast shutdown)
if signal.send_signal(args.config, "stop") then
cutils.logger:success("Stopped")
else
cutils.logger:error_exit("Could not stop Kong")
end
18 changes: 1 addition & 17 deletions src/cli/utils/signal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,9 @@ function _M.send_signal(args_config, signal)
return os.execute(cmd) == 0
end

-- Wrapper around a start signal, also migrating the schema if needed
-- @param args_config Path to the desired configuration (usually from the --config CLI argument)
function _M.send_start(args_config)
_M.prepare_kong(args_config)

if _M.send_signal(args_config) then
cutils.logger:success("Started")
else
cutils.logger:error_exit("Could not start Kong")
end
end

-- Wrapper around a stop signal, testing if Kong is already running
-- @param args_config Path to the desired configuration (usually from the --config CLI argument)
function _M.send_stop(args_config)
function _M.is_running(args_config)
-- Get configuration from default or given path
local _, kong_config = get_kong_config_path(args_config)

Expand All @@ -172,10 +160,6 @@ function _M.send_stop(args_config)
if not IO.file_exists(pid) then
cutils.logger:error_exit("Not running. Could not find pid at: "..pid)
end

if _M.send_signal(args_config, "stop") then
cutils.logger:success("Stopped")
end
end

return _M

0 comments on commit 1580a2f

Please sign in to comment.