1
0
mirror of https://github.com/moparisthebest/SickRage synced 2024-11-05 17:05:03 -05:00

Moved Tornado IOLoop to main() to resolve shutdown issues plus we now use IOLoop to control checks for startup tasks and background commands.

This commit is contained in:
echel0n 2014-06-15 05:43:53 -07:00
parent 20e2ae2f86
commit af6cd334a0
3 changed files with 45 additions and 45 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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()
self.server.listen(self.options['port'], self.options['host'])
self.ioloop.start()
def stop(self):
self.server.stop()
self.ioloop.stop()