diff --git a/SickBeard.py b/SickBeard.py index 9c182bef..bd9f3121 100755 --- a/SickBeard.py +++ b/SickBeard.py @@ -91,41 +91,33 @@ def loadShowsFromDB(): sickbeard.showList.append(curShow) except Exception, e: logger.log( - u"There was an error creating the show in " + sqlShow["location"] + ": " + str(e).decode('utf-8'), + u"There was an error creating the show in " + sqlShow["location"] + ": " + str(e).decode('utf-8'), logger.ERROR) logger.log(traceback.format_exc(), logger.DEBUG) # TODO: update the existing shows if the showlist has something in it - def daemonize(): - """ - Fork off as a daemon - """ - - # pylint: disable=E1101 - # Make a non-session-leader child process try: - pid = os.fork() # @UndefinedVariable - only available in UNIX - if pid != 0: - os._exit(0) - except OSError, e: - sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror)) + pid = os.fork() + if pid > 0: + sys.exit(0) + except OSError: + print "fork() failed" sys.exit(1) - os.setsid() # unix - + os.chdir(sickbeard.PROG_DIR) + os.setsid() # Make sure I can read my own files and shut out others - prev = os.umask(0) - os.umask(prev and int('077', 8)) + prev= os.umask(0) + os.umask(prev and int('077',8)) - # Make the child a session-leader by detaching from the terminal try: - pid = os.fork() # @UndefinedVariable - only available in UNIX - if pid != 0: - os._exit(0) - except OSError, e: - sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) + pid = os.fork() + if pid > 0: + sys.exit(0) + except OSError: + print "fork() failed" sys.exit(1) # Write pid @@ -139,25 +131,14 @@ def daemonize(): u"Unable to write PID file: " + sickbeard.PIDFILE + " Error: " + str(e.strerror) + " [" + str( e.errno) + "]") - # Redirect all output - sys.stdout.flush() - sys.stderr.flush() - - devnull = getattr(os, 'devnull', '/dev/null') - stdin = file(devnull, 'r') - stdout = file(devnull, 'a+') - stderr = file(devnull, 'a+') - os.dup2(stdin.fileno(), sys.stdin.fileno()) - os.dup2(stdout.fileno(), sys.stdout.fileno()) - os.dup2(stderr.fileno(), sys.stderr.fileno()) + dev_null = file('/dev/null', 'r') + os.dup2(dev_null.fileno(), sys.stdin.fileno()) def main(): """ TV for me """ - io_loop = IOLoop.current() - # do some preliminary stuff sickbeard.MY_FULLNAME = os.path.normpath(os.path.abspath(__file__)) sickbeard.MY_NAME = os.path.basename(sickbeard.MY_FULLNAME) @@ -327,12 +308,6 @@ def main(): sickbeard.showList = [] - if sickbeard.DAEMON: - daemonize() - - # Use this PID for everything - sickbeard.PID = os.getpid() - if forcedPort: logger.log(u"Forcing web server to port " + str(forcedPort)) startPort = forcedPort @@ -387,6 +362,9 @@ def main(): if forceUpdate or sickbeard.UPDATE_SHOWS_ON_START: sickbeard.showUpdateScheduler.action.run(force=True) # @UndefinedVariable + # get ioloop + io_loop = IOLoop.current() + # init startup tasks io_loop.add_timeout(datetime.timedelta(seconds=5), startup) @@ -395,6 +373,12 @@ def main(): if sickbeard.AUTO_UPDATE: tornado.autoreload.start(io_loop) + if sickbeard.DAEMON: + daemonize() + + # Use this PID for everything + sickbeard.PID = os.getpid() + # start IOLoop. io_loop.start() sickbeard.saveAndShutdown() diff --git a/sickbeard/webserve.py b/sickbeard/webserve.py index 9ce0da01..333dc257 100644 --- a/sickbeard/webserve.py +++ b/sickbeard/webserve.py @@ -83,14 +83,7 @@ from Cheetah.Template import Template from tornado import gen from tornado.web import RequestHandler, HTTPError, asynchronous -# def _handle_reverse_proxy(): -# if sickbeard.HANDLE_REVERSE_PROXY: -# cherrypy.lib.cptools.proxy() - -# cherrypy.tools.handle_reverse_proxy = cherrypy.Tool('before_handler', _handle_reverse_proxy) - req_headers = None - def authenticated(handler_class): def wrap_execute(handler_execute): def basicauth(handler, transforms, *args, **kwargs): diff --git a/sickbeard/webserveInit.py b/sickbeard/webserveInit.py index 859203c8..5e2326f5 100644 --- a/sickbeard/webserveInit.py +++ b/sickbeard/webserveInit.py @@ -67,6 +67,7 @@ def initWebServer(options={}): app = Application([], debug=sickbeard.DEBUG, gzip=True, + xheaders=sickbeard.HANDLE_REVERSE_PROXY, cookie_secret='61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=' )