Final fixes for proper shutdown of tornado

This commit is contained in:
echel0n 2014-06-15 01:20:18 -07:00
parent 661c4ed6a7
commit 149d7b29df
2 changed files with 8 additions and 19 deletions

View file

@ -1298,7 +1298,8 @@ def saveAndShutdown(restart=False):
halt() halt()
saveAll() saveAll()
IOLoop.instance().add_callback(WEBSERVER.shutdown) # Shutdown tornado
WEBSERVER.shutdown()
if CREATEPID: if CREATEPID:
logger.log(u"Removing pidfile " + str(PIDFILE)) logger.log(u"Removing pidfile " + str(PIDFILE))

View file

@ -10,7 +10,7 @@ from sickbeard import logger
from sickbeard.helpers import create_https_certificates from sickbeard.helpers import create_https_certificates
from tornado.web import Application, StaticFileHandler, RedirectHandler, HTTPError from tornado.web import Application, StaticFileHandler, RedirectHandler, HTTPError
from tornado.httpserver import HTTPServer from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop, PeriodicCallback
class MultiStaticFileHandler(StaticFileHandler): class MultiStaticFileHandler(StaticFileHandler):
@ -154,6 +154,7 @@ class webserverInit():
self.thread = threading.Thread(None, self.monitor, 'TORNADO') self.thread = threading.Thread(None, self.monitor, 'TORNADO')
def monitor(self): def monitor(self):
io_loop = IOLoop.current()
while True: while True:
@ -164,7 +165,7 @@ class webserverInit():
try: try:
logger.log(u"Starting tornado", logger.DEBUG) logger.log(u"Starting tornado", logger.DEBUG)
IOLoop.instance().start() io_loop.start()
except Exception, e: except Exception, e:
logger.log(u"Exception generated in tornado: " + ex(e), logger.ERROR) logger.log(u"Exception generated in tornado: " + ex(e), logger.ERROR)
logger.log(repr(traceback.format_exc()), logger.DEBUG) logger.log(repr(traceback.format_exc()), logger.DEBUG)
@ -178,27 +179,14 @@ class webserverInit():
def shutdown(self): def shutdown(self):
logger.logging.info('Shutting down tornado')
self.abort = True self.abort = True
self.server.stop()
deadline = time.time() + 3 logger.log('Shutting down tornado')
io_loop = IOLoop.instance()
def stop_loop():
now = time.time()
if now < deadline and (io_loop._callbacks or io_loop._timeouts):
io_loop.add_timeout(now + 1, stop_loop)
else:
io_loop.stop()
stop_loop()
try: try:
IOLoop.instance().stop()
self.thread.join(10) self.thread.join(10)
except: except:
pass pass
logger.logging.info('Tornado is now shutdown') logger.log('Tornado is now shutdown')