mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
make graceful shutdown thread-safe
This commit is contained in:
parent
cc7459d953
commit
d9b89599cc
@ -33,14 +33,12 @@ class TornadoWorker(Worker):
|
|||||||
def handle_exit(self, sig, frame):
|
def handle_exit(self, sig, frame):
|
||||||
if self.alive:
|
if self.alive:
|
||||||
super(TornadoWorker, self).handle_exit(sig, frame)
|
super(TornadoWorker, self).handle_exit(sig, frame)
|
||||||
self.stop()
|
|
||||||
|
|
||||||
def handle_request(self):
|
def handle_request(self):
|
||||||
self.nr += 1
|
self.nr += 1
|
||||||
if self.alive and self.nr >= self.max_requests:
|
if self.alive and self.nr >= self.max_requests:
|
||||||
self.alive = False
|
|
||||||
self.log.info("Autorestarting worker after current request.")
|
self.log.info("Autorestarting worker after current request.")
|
||||||
self.stop()
|
self.alive = False
|
||||||
|
|
||||||
def watchdog(self):
|
def watchdog(self):
|
||||||
if self.alive:
|
if self.alive:
|
||||||
@ -48,12 +46,27 @@ class TornadoWorker(Worker):
|
|||||||
|
|
||||||
if self.ppid != os.getppid():
|
if self.ppid != os.getppid():
|
||||||
self.log.info("Parent changed, shutting down: %s", self)
|
self.log.info("Parent changed, shutting down: %s", self)
|
||||||
self.stop()
|
self.alive = False
|
||||||
|
|
||||||
|
def heartbeat(self):
|
||||||
|
if not self.alive:
|
||||||
|
if self.server_alive:
|
||||||
|
if hasattr(self, 'server'):
|
||||||
|
try:
|
||||||
|
self.server.stop()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
self.server_alive = False
|
||||||
|
else:
|
||||||
|
if not self.ioloop._callbacks and len(self.ioloop._timeouts) <= 1:
|
||||||
|
self.ioloop.stop()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.ioloop = IOLoop.instance()
|
self.ioloop = IOLoop.instance()
|
||||||
self.alive = True
|
self.alive = True
|
||||||
|
self.server_alive = False
|
||||||
PeriodicCallback(self.watchdog, 1000, io_loop=self.ioloop).start()
|
PeriodicCallback(self.watchdog, 1000, io_loop=self.ioloop).start()
|
||||||
|
PeriodicCallback(self.heartbeat, 1000, io_loop=self.ioloop).start()
|
||||||
|
|
||||||
# Assume the app is a WSGI callable if its not an
|
# Assume the app is a WSGI callable if its not an
|
||||||
# instance of tornado.web.Application or is an
|
# instance of tornado.web.Application or is an
|
||||||
@ -95,6 +108,7 @@ class TornadoWorker(Worker):
|
|||||||
server = server_class(app, io_loop=self.ioloop)
|
server = server_class(app, io_loop=self.ioloop)
|
||||||
|
|
||||||
self.server = server
|
self.server = server
|
||||||
|
self.server_alive = True
|
||||||
|
|
||||||
for s in self.sockets:
|
for s in self.sockets:
|
||||||
s.setblocking(0)
|
s.setblocking(0)
|
||||||
@ -107,15 +121,3 @@ class TornadoWorker(Worker):
|
|||||||
server.start(num_processes=1)
|
server.start(num_processes=1)
|
||||||
|
|
||||||
self.ioloop.start()
|
self.ioloop.start()
|
||||||
|
|
||||||
def stop(self):
|
|
||||||
if hasattr(self, 'server'):
|
|
||||||
try:
|
|
||||||
self.server.stop()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
PeriodicCallback(self.stop_ioloop, 1000, io_loop=self.ioloop).start()
|
|
||||||
|
|
||||||
def stop_ioloop(self):
|
|
||||||
if not self.ioloop._callbacks and len(self.ioloop._timeouts) <= 1:
|
|
||||||
self.ioloop.stop()
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user