Skip to content

Signal handler #157

@scossu

Description

@scossu

Hello,

I am trying to catch a SIGTERM or SIGINT signal to perform some clean-up tasks before shutting down the server.

Using one of your examples:

-- requires luaposix to be installed

local pegasus = require 'pegasus'
local signal = require "posix.signal"

local pidfile

local function shutdown()
  io.write("Shutting down server...\n")
  -- delete PID file here
  os.exit()
end

signal.signal(signal.SIGINT, shutdown)
signal.signal(signal.SIGTERM, shutdown)

local server = pegasus:new({
  port = '9090',
})

local printTable = function(table)
  for k, v in pairs(table) do
    print(k, '=', v)
  end
end

-- create PID file here 

server:start(function (req, res)
  printTable(req['querystring'])
  res:addHeader('Content-Type', 'text/html'):write('hello pegasus world!')

  -- return a truthy value to indicate the request was handled, no further handling needed
  return res:close()
end)

If I hit CTRL-C or send SIGTERM to the process, nothing happens. Removing the signal handlers, Pegasus shuts down as expected (hitting CTRL-C twice).

Is there a way to catch termination signals and add a handler that runs just before shutdown (ideally also capable of aborting shutdown if instructed to do so)?

If there isn't any, and it seems a valuable addition to the library, I'd be glad to send a PR if I can get some hints about the best place to add it. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions