From 149d7b29dfaebd81a61e2cef755e63c650436ac5 Mon Sep 17 00:00:00 2001 From: echel0n Date: Sun, 15 Jun 2014 01:20:18 -0700 Subject: [PATCH] Final fixes for proper shutdown of tornado --- sickbeard/__init__.py | 3 ++- sickbeard/webserveInit.py | 24 ++++++------------------ 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/sickbeard/__init__.py b/sickbeard/__init__.py index 43813f66..70ff8b9b 100644 --- a/sickbeard/__init__.py +++ b/sickbeard/__init__.py @@ -1298,7 +1298,8 @@ def saveAndShutdown(restart=False): halt() saveAll() - IOLoop.instance().add_callback(WEBSERVER.shutdown) + # Shutdown tornado + WEBSERVER.shutdown() if CREATEPID: logger.log(u"Removing pidfile " + str(PIDFILE)) diff --git a/sickbeard/webserveInit.py b/sickbeard/webserveInit.py index 5202b683..2dd4b73f 100644 --- a/sickbeard/webserveInit.py +++ b/sickbeard/webserveInit.py @@ -10,7 +10,7 @@ from sickbeard import logger from sickbeard.helpers import create_https_certificates from tornado.web import Application, StaticFileHandler, RedirectHandler, HTTPError from tornado.httpserver import HTTPServer -from tornado.ioloop import IOLoop +from tornado.ioloop import IOLoop, PeriodicCallback class MultiStaticFileHandler(StaticFileHandler): @@ -154,6 +154,7 @@ class webserverInit(): self.thread = threading.Thread(None, self.monitor, 'TORNADO') def monitor(self): + io_loop = IOLoop.current() while True: @@ -164,7 +165,7 @@ class webserverInit(): try: logger.log(u"Starting tornado", logger.DEBUG) - IOLoop.instance().start() + io_loop.start() except Exception, e: logger.log(u"Exception generated in tornado: " + ex(e), logger.ERROR) logger.log(repr(traceback.format_exc()), logger.DEBUG) @@ -178,27 +179,14 @@ class webserverInit(): def shutdown(self): - logger.logging.info('Shutting down tornado') - self.abort = True - self.server.stop() - deadline = time.time() + 3 - - 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() + logger.log('Shutting down tornado') try: + IOLoop.instance().stop() self.thread.join(10) except: pass - logger.logging.info('Tornado is now shutdown') \ No newline at end of file + logger.log('Tornado is now shutdown') \ No newline at end of file