diff --git a/SickBeard.py b/SickBeard.py index ebd2cf82..7cd279b6 100755 --- a/SickBeard.py +++ b/SickBeard.py @@ -48,7 +48,6 @@ if sys.hexversion >= 0x020600F0: import locale import datetime import threading -import time import signal import traceback import getopt @@ -65,6 +64,8 @@ from sickbeard.databases.mainDB import MAX_DB_VERSION from lib.configobj import ConfigObj +from tornado.ioloop import IOLoop, PeriodicCallback + signal.signal(signal.SIGINT, sickbeard.sig_handler) signal.signal(signal.SIGTERM, sickbeard.sig_handler) @@ -372,6 +373,9 @@ def main(): logger.log(u"Loading initial show list") loadShowsFromDB() + # Fire up all our threads + sickbeard.start() + # Launch browser if we're supposed to if sickbeard.LAUNCH_BROWSER and not noLaunch and not sickbeard.DAEMON: sickbeard.launchBrowser(startPort) @@ -384,15 +388,13 @@ def main(): sickbeard.WEBSERVER = webserverInit(options) sickbeard.WEBSERVER.ioloop.add_timeout(datetime.timedelta(seconds=5), startup) - # Fire up all our threads - sickbeard.start() - # check for commands to be executed in the background - task = tornado.ioloop.PeriodicCallback(invoke_command, 1000) + task = PeriodicCallback(invoke_command, 1000) task.start() # start tornado sickbeard.WEBSERVER.start() + sickbeard.WEBSERVER.close() return if __name__ == "__main__": diff --git a/sickbeard/__init__.py b/sickbeard/__init__.py index cf093fc2..4eb788af 100644 --- a/sickbeard/__init__.py +++ b/sickbeard/__init__.py @@ -1296,6 +1296,14 @@ def saveAndShutdown(restart=False): halt() saveAll() + logger.log('Shutting down tornado') + try: + WEBSERVER.stop() + except RuntimeError: + pass + except: + logger.log('Failed shutting down the server: %s' % traceback.format_exc(), logger.ERROR) + if CREATEPID: logger.log(u"Removing pidfile " + str(PIDFILE)) remove_pid_file(PIDFILE) @@ -1326,14 +1334,6 @@ def saveAndShutdown(restart=False): subprocess.Popen(popen_list, cwd=os.getcwd()) - logger.log('Shutting down tornado') - try: - WEBSERVER.stop() - except RuntimeError: - pass - except: - logger.log('Failed shutting down the server: %s' % traceback.format_exc(), logger.ERROR) - os._exit(0) diff --git a/sickbeard/webserveInit.py b/sickbeard/webserveInit.py index 2b79b49b..83408c12 100644 --- a/sickbeard/webserveInit.py +++ b/sickbeard/webserveInit.py @@ -39,7 +39,7 @@ class webserverInit(): self.abort = False self.server = None - self.ioloop = IOLoop.instance() + self.ioloop = None self.options = options self.options.setdefault('port', 8081) @@ -145,10 +145,14 @@ class webserverInit(): logger.log(u"Starting SickRage on " + protocol + "://" + str(self.options['host']) + ":" + str( self.options['port']) + "/") + self.ioloop = IOLoop.current() + def start(self): self.server.listen(self.options['port'], self.options['host']) self.ioloop.start() def stop(self): - self.server.stop() - self.ioloop.stop() \ No newline at end of file + self.ioloop.stop() + + def close(self): + self.ioloop.close() \ No newline at end of file