From 0d67447d198bea8521718ef4265128270d1f5476 Mon Sep 17 00:00:00 2001 From: jbergstroem Date: Fri, 23 Jul 2010 10:00:54 +0200 Subject: [PATCH] Add pre/post request hooks --- gunicorn/config.py | 40 ++++++++++++++++++++++++++++++++++++--- gunicorn/workers/async.py | 2 ++ gunicorn/workers/sync.py | 2 ++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/gunicorn/config.py b/gunicorn/config.py index f9b26512..0e246d7b 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -569,7 +569,8 @@ class Postfork(Setting): The callable needs to accept two instance variables for the Arbiter and new Worker. """ - + + class WhenReady(Setting): name = "when_ready" section = "Server Hooks" @@ -584,7 +585,8 @@ class WhenReady(Setting): The callable needs to accept a single instance variable for the Arbiter. """ - + + class PreExec(Setting): name = "pre_exec" section = "Server Hooks" @@ -599,5 +601,37 @@ class PreExec(Setting): The callable needs to accept a single instance variable for the Arbiter. """ - + + +class PreRequest(Setting): + name = "pre_request" + section = "Server Hooks" + validator = validate_callable(2) + type = "callable" + def def_pre_request(worker, req): + worker.log.debug("%s %s" % (req.method, req.path)) + def_pre_request = staticmethod(def_pre_request) + default = def_pre_request + desc = """\ + Called just before a worker processes the request. + The callable needs to accept two instance variables for the Worker and + the Request. + """ + + +class PostRequest(Setting): + name = "post_request" + section = "Server Hooks" + validator = validate_callable(2) + type = "callable" + def def_post_request(worker, req): + pass + def_post_request = staticmethod(def_post_request) + default = def_post_request + desc = """\ + Called after a worker processes the request. + + The callable needs to accept two instance variables for the Worker and + the Request. + """ diff --git a/gunicorn/workers/async.py b/gunicorn/workers/async.py index 6d26dcac..2445febe 100644 --- a/gunicorn/workers/async.py +++ b/gunicorn/workers/async.py @@ -61,6 +61,7 @@ class AsyncWorker(base.Worker): def handle_request(self, req, sock, addr): try: debug = self.cfg.debug or False + self.cfg.pre_request(self, req) resp, environ = wsgi.create(req, sock, addr, self.address, self.cfg) respiter = self.wsgi(environ, resp.start_response) @@ -73,6 +74,7 @@ class AsyncWorker(base.Worker): respiter.close() if req.should_close(): raise StopIteration() + self.cfg.post_request(self, req) except Exception, e: #Only send back traceback in HTTP in debug mode. if not self.debug: diff --git a/gunicorn/workers/sync.py b/gunicorn/workers/sync.py index a12e9ae8..bdc6f8a0 100644 --- a/gunicorn/workers/sync.py +++ b/gunicorn/workers/sync.py @@ -94,6 +94,7 @@ class SyncWorker(base.Worker): def handle_request(self, req, client, addr): try: debug = self.cfg.debug or False + self.cfg.pre_request(self, req) resp, environ = wsgi.create(req, client, addr, self.address, self.cfg) respiter = self.wsgi(environ, resp.start_response) @@ -102,6 +103,7 @@ class SyncWorker(base.Worker): resp.close() if hasattr(respiter, "close"): respiter.close() + self.cfg.post_request(self, req) except socket.error: raise except Exception, e: