From 386355e1302404bfb239efc8324cb41d9a80f3fc Mon Sep 17 00:00:00 2001 From: echel0n Date: Mon, 23 Jun 2014 08:18:01 -0700 Subject: [PATCH] Updates/Restarts now use the same process instead of spawning a new one so the PID remains the same. Fixed issue for restarts/updates on windows. --- SickBeard.py | 18 +++++++++++++++++- gui/slick/js/restart.js | 8 ++++---- sickbeard/__init__.py | 38 ++------------------------------------ sickbeard/webserve.py | 8 ++++---- sickbeard/webserveInit.py | 5 +++-- 5 files changed, 30 insertions(+), 47 deletions(-) diff --git a/SickBeard.py b/SickBeard.py index f583e10c..bcb9b6e0 100755 --- a/SickBeard.py +++ b/SickBeard.py @@ -53,6 +53,7 @@ import threading import signal import traceback import getopt +import time import sickbeard @@ -380,6 +381,9 @@ def main(): loadShowsFromDB() def startup(): + if sickbeard.restarted: + sickbeard.restarted = False + # Fire up all our threads sickbeard.start() @@ -408,4 +412,16 @@ def main(): if __name__ == "__main__": if sys.hexversion >= 0x020600F0: freeze_support() - main() \ No newline at end of file + + while(True): + main() + + # check if restart was requested + if not sickbeard.restarted: + if sickbeard.CREATEPID: + logger.log(u"Removing pidfile " + str(sickbeard.PIDFILE)) + sickbeard.remove_pid_file(sickbeard.PIDFILE) + break + + # restart + logger.log("Restarting SickRage, please stand by...") \ No newline at end of file diff --git a/gui/slick/js/restart.js b/gui/slick/js/restart.js index edc4074f..9ff51ab0 100644 --- a/gui/slick/js/restart.js +++ b/gui/slick/js/restart.js @@ -13,7 +13,7 @@ else var base_url = window.location.protocol+'//'+window.location.host+sbRoot; var is_alive_url = sbRoot+'/home/is_alive'; var timeout_id; -var current_pid = ''; +var restarted = ''; var num_restart_waits = 0; function is_alive() { @@ -28,9 +28,9 @@ function is_alive() { setTimeout('is_alive()', 1000); } else { // if this is before we've even shut down then just try again later - if (current_pid == '' || data.msg == current_pid) { - current_pid = data.msg; - setTimeout(is_alive, 1000); + if (restarted == '' || data.restarted == restarted) { + restarted = data.restarted; + setTimeout('is_alive()', 1000); // if we're ready to go then redirect to new url } else { diff --git a/sickbeard/__init__.py b/sickbeard/__init__.py index c9c6824b..cf80c3b3 100644 --- a/sickbeard/__init__.py +++ b/sickbeard/__init__.py @@ -477,7 +477,7 @@ def initialize(consoleLogging=True): USE_FAILED_DOWNLOADS, DELETE_FAILED, ANON_REDIRECT, LOCALHOST_IP, REMOTE_IP, TMDB_API_KEY, DEBUG, PROXY_SETTING, \ AUTOPOSTPROCESSER_FREQUENCY, DEFAULT_AUTOPOSTPROCESSER_FREQUENCY, MIN_AUTOPOSTPROCESSER_FREQUENCY, \ ANIME_DEFAULT, NAMING_ANIME, ANIMESUPPORT, USE_ANIDB, ANIDB_USERNAME, ANIDB_PASSWORD, ANIDB_USE_MYLIST, \ - ANIME_SPLIT_HOME, maintenanceScheduler, SCENE_DEFAULT + ANIME_SPLIT_HOME, maintenanceScheduler, SCENE_DEFAULT, RES if __INITIALIZED__: return False @@ -1309,44 +1309,9 @@ def cleanup_tornado_sockets(io_loop): pass def saveAndShutdown(): - halt() saveAll() - cleanup_tornado_sockets(IOLoop.current()) - - if CREATEPID: - logger.log(u"Removing pidfile " + str(PIDFILE)) - remove_pid_file(PIDFILE) - - if restarted: - install_type = versionCheckScheduler.action.install_type - - popen_list = [] - - if install_type in ('git', 'source'): - popen_list = [sys.executable, MY_FULLNAME] - elif install_type == 'win': - if hasattr(sys, 'frozen'): - # c:\dir\to\updater.exe 12345 c:\dir\to\sickbeard.exe - popen_list = [os.path.join(PROG_DIR, 'updater.exe'), str(PID), sys.executable] - else: - logger.log(u"Unknown SB launch method, please file a bug report about this", logger.ERROR) - popen_list = [sys.executable, os.path.join(PROG_DIR, 'updater.py'), str(PID), sys.executable, - MY_FULLNAME] - - if popen_list: - popen_list += MY_ARGS - if '--nolaunch' not in popen_list: - popen_list += ['--nolaunch'] - - logger.log(u"Restarting SickRage with " + str(popen_list)) - logger.close() - - subprocess.Popen(popen_list, cwd=os.getcwd()) - - os._exit(0) - def invoke_command(to_call, *args, **kwargs): def delegate(): @@ -1375,6 +1340,7 @@ def restart(soft=True): initialize() else: restarted=True + time.sleep(5) webserveInit.shutdown() diff --git a/sickbeard/webserve.py b/sickbeard/webserve.py index 9d19d19b..261f08de 100644 --- a/sickbeard/webserve.py +++ b/sickbeard/webserve.py @@ -3004,9 +3004,9 @@ class Home(MainHandler): self.set_header('Access-Control-Allow-Headers', 'x-requested-with') if sickbeard.started: - return callback + '(' + json.dumps({"msg": str(sickbeard.PID)}) + ');' + return callback + '(' + json.dumps({"msg": str(sickbeard.PID), "restarted": str(sickbeard.restarted)}) + ');' else: - return callback + '(' + json.dumps({"msg": "nope"}) + ');' + return callback + '(' + json.dumps({"msg": "nope", "restarted": str(sickbeard.restarted)}) + ');' def index(self, *args, **kwargs): @@ -3322,8 +3322,8 @@ class Home(MainHandler): t = PageTemplate(file="restart.tmpl") t.submenu = HomeMenu() - # do a soft restart - threading.Timer(2, sickbeard.invoke_restart, [False]).start() + # restart + threading.Timer(5, sickbeard.invoke_restart, [False]).start() return _munge(t) diff --git a/sickbeard/webserveInit.py b/sickbeard/webserveInit.py index 565fe935..c7c3c57f 100644 --- a/sickbeard/webserveInit.py +++ b/sickbeard/webserveInit.py @@ -1,5 +1,6 @@ import os import traceback +import time import sickbeard import webserve import webapi @@ -114,10 +115,10 @@ def initWebServer(options={}): def shutdown(): global server - logger.log('Shutting down tornado') + logger.log('Shutting down tornado io loop') try: IOLoop.current().stop() except RuntimeError: pass except: - logger.log('Failed shutting down the server: %s' % traceback.format_exc(), logger.ERROR) \ No newline at end of file + logger.log('Failed shutting down tornado io loop: %s' % traceback.format_exc(), logger.ERROR) \ No newline at end of file