add worker_int callback

fix #516
This commit is contained in:
benoitc 2013-12-26 12:25:06 +01:00
parent 5625dbcfcd
commit 95efe1b7d2
3 changed files with 37 additions and 1 deletions

View File

@ -70,7 +70,7 @@ backlog = 2048
#
workers = 1
worker_class = 'egg:gunicorn#sync'
worker_class = 'sync'
worker_connections = 1000
timeout = 30
keepalive = 2
@ -200,3 +200,20 @@ def pre_exec(server):
def when_ready(server):
server.log.info("Server is ready. Spwawning workers")
def worker_int(worker):
worker.log.info("worker received INT or TERM signal")
## get traceback info
import threading, sys, traceback
id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
code = []
for threadId, stack in sys._current_frames().items():
code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""),
threadId))
for filename, lineno, name, line in traceback.extract_stack(stack):
code.append('File: "%s", line %d, in %s' % (filename,
lineno, name))
if line:
code.append(" %s" % (line.strip()))
worker.log.debug("\n".join(code))

View File

@ -1286,6 +1286,23 @@ class PostWorkerInit(Setting):
Worker.
"""
class WorkerInt(Setting):
name = "worker_int"
section = "Server Hooks"
validator = validate_callable(1)
type = six.callable
def worker_int(worker):
pass
default = staticmethod(worker_int)
desc = """\
Called just after a worker exited on SIGINT or SIGTERM.
The callable needs to accept one instance variable for the initialized
Worker.
"""
class PreExec(Setting):
name = "pre_exec"

View File

@ -134,6 +134,8 @@ class Worker(object):
def handle_exit(self, sig, frame):
self.alive = False
# worker_int callback
self.cfg.worker_int(self)
sys.exit(0)
def handle_error(self, req, client, addr, exc):