diff --git a/SickBeard.py b/SickBeard.py index ad9395d6..ebd2cf82 100755 --- a/SickBeard.py +++ b/SickBeard.py @@ -146,6 +146,12 @@ def daemonize(): os.dup2(stderr.fileno(), sys.stderr.fileno()) +# background update every x seconds +def invoke_command(): + if sickbeard.invoked_command: + sickbeard.invoked_command() + sickbeard.invoked_command = None + def main(): """ TV for me @@ -361,36 +367,34 @@ def main(): 'https_key': sickbeard.HTTPS_KEY, } - # Build from the DB to start with - logger.log(u"Loading initial show list") - loadShowsFromDB() + def startup(): + # Build from the DB to start with + logger.log(u"Loading initial show list") + loadShowsFromDB() - # start tornado thread - webserverInit(options).start() + # Launch browser if we're supposed to + if sickbeard.LAUNCH_BROWSER and not noLaunch and not sickbeard.DAEMON: + sickbeard.launchBrowser(startPort) + + # Start an update if we're supposed to + if forceUpdate or sickbeard.UPDATE_SHOWS_ON_START: + sickbeard.showUpdateScheduler.action.run(force=True) # @UndefinedVariable + + # init tornado + sickbeard.WEBSERVER = webserverInit(options) + sickbeard.WEBSERVER.ioloop.add_timeout(datetime.timedelta(seconds=5), startup) # 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) - - # Start an update if we're supposed to - if forceUpdate or sickbeard.UPDATE_SHOWS_ON_START: - sickbeard.showUpdateScheduler.action.run(force=True) # @UndefinedVariable - - # Stay alive while my threads do the work - while (True): - - if sickbeard.invoked_command: - sickbeard.invoked_command() - sickbeard.invoked_command = None - - time.sleep(1) + # check for commands to be executed in the background + task = tornado.ioloop.PeriodicCallback(invoke_command, 1000) + task.start() + # start tornado + sickbeard.WEBSERVER.start() return - if __name__ == "__main__": if sys.hexversion >= 0x020600F0: freeze_support() diff --git a/sickbeard/__init__.py b/sickbeard/__init__.py index e12ca08d..cf093fc2 100644 --- a/sickbeard/__init__.py +++ b/sickbeard/__init__.py @@ -77,6 +77,7 @@ PIDFILE = '' DAEMON = None NO_RESIZE = False +WEBSERVER = None maintenanceScheduler = None dailySearchScheduler = None @@ -1290,23 +1291,11 @@ def saveAll(): def saveAndShutdown(restart=False): + global WEBSERVER halt() saveAll() - # Shutdown tornado - logger.log('Shutting down tornado') - - def shutdown(): - try: - IOLoop.current().stop() - except RuntimeError: - pass - except: - logger.log('Failed shutting down the server: %s' % traceback.format_exc(), logger.ERROR) - - IOLoop.current().add_callback(shutdown) - if CREATEPID: logger.log(u"Removing pidfile " + str(PIDFILE)) remove_pid_file(PIDFILE) @@ -1337,6 +1326,14 @@ 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 fbd59ee4..2b79b49b 100644 --- a/sickbeard/webserveInit.py +++ b/sickbeard/webserveInit.py @@ -1,7 +1,4 @@ import os -import threading -import time -import traceback import datetime import sickbeard import webserve @@ -42,7 +39,7 @@ class webserverInit(): self.abort = False self.server = None - self.thread = None + self.ioloop = IOLoop.instance() self.options = options self.options.setdefault('port', 8081) @@ -113,7 +110,8 @@ class webserverInit(): debug=False, gzip=True, cookie_secret='61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=', - login_url='/login' + login_url='/login', + autoreload=True ) # Index Handler @@ -147,9 +145,10 @@ class webserverInit(): logger.log(u"Starting SickRage on " + protocol + "://" + str(self.options['host']) + ":" + str( self.options['port']) + "/") - self.server.listen(self.options['port'], self.options['host']) - def start(self): - if self.thread == None or not self.thread.isAlive(): - self.thread = threading.Thread(target=IOLoop.current().start) - self.thread.start() \ No newline at end of file + 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