Browse Source

Implement direct restarts (when possible)

pull/745/head
Safihre 9 years ago
parent
commit
d7a0147191
  1. 49
      SABnzbd.py
  2. 43
      sabnzbd/__init__.py
  3. 8
      sabnzbd/interface.py
  4. 9
      sabnzbd/osxmenu.py
  5. 9
      sabnzbd/sabtray.py
  6. 3
      sabnzbd/sabtraylinux.py

49
SABnzbd.py

@ -884,7 +884,7 @@ def main():
repair = 0 repair = 0
api_url = None api_url = None
no_login = False no_login = False
re_argv = [sys.argv[0]] sabnzbd.RESTART_ARGS = [sys.argv[0]]
pid_path = None pid_path = None
pid_file = None pid_file = None
new_instance = False new_instance = False
@ -902,11 +902,11 @@ def main():
fork = True fork = True
autobrowser = False autobrowser = False
sabnzbd.DAEMON = True sabnzbd.DAEMON = True
re_argv.append(opt) sabnzbd.RESTART_ARGS.append(opt)
elif opt in ('-f', '--config-file'): elif opt in ('-f', '--config-file'):
inifile = arg inifile = arg
re_argv.append(opt) sabnzbd.RESTART_ARGS.append(opt)
re_argv.append(arg) sabnzbd.RESTART_ARGS.append(arg)
elif opt in ('-h', '--help'): elif opt in ('-h', '--help'):
print_help() print_help()
exit_sab(0) exit_sab(0)
@ -950,11 +950,11 @@ def main():
pause = True pause = True
elif opt in ('--force',): elif opt in ('--force',):
force_web = True force_web = True
re_argv.append(opt) sabnzbd.RESTART_ARGS.append(opt)
elif opt in ('--https',): elif opt in ('--https',):
https_port = int(arg) https_port = int(arg)
re_argv.append(opt) sabnzbd.RESTART_ARGS.append(opt)
re_argv.append(arg) sabnzbd.RESTART_ARGS.append(arg)
elif opt in ('--repair',): elif opt in ('--repair',):
repair = 1 repair = 1
pause = True pause = True
@ -967,19 +967,19 @@ def main():
no_login = True no_login = True
elif opt in ('--pid',): elif opt in ('--pid',):
pid_path = arg pid_path = arg
re_argv.append(opt) sabnzbd.RESTART_ARGS.append(opt)
re_argv.append(arg) sabnzbd.RESTART_ARGS.append(arg)
elif opt in ('--pidfile',): elif opt in ('--pidfile',):
pid_file = arg pid_file = arg
re_argv.append(opt) sabnzbd.RESTART_ARGS.append(opt)
re_argv.append(arg) sabnzbd.RESTART_ARGS.append(arg)
elif opt in ('--new',): elif opt in ('--new',):
new_instance = True new_instance = True
elif opt in ('--sessions',): elif opt in ('--sessions',):
re_argv.append(opt) sabnzbd.RESTART_ARGS.append(opt)
force_sessions = True force_sessions = True
elif opt in ('--console',): elif opt in ('--console',):
re_argv.append(opt) sabnzbd.RESTART_ARGS.append(opt)
osx_console = True osx_console = True
elif opt in ('--ipv6_hosting',): elif opt in ('--ipv6_hosting',):
ipv6_hosting = arg ipv6_hosting = arg
@ -1617,7 +1617,7 @@ def main():
# Check the threads # Check the threads
if not sabnzbd.check_all_tasks(): if not sabnzbd.check_all_tasks():
autorestarted = True autorestarted = True
cherrypy.engine.execv = True sabnzbd.TRIGGER_RESTART = True
# Notify guardian # Notify guardian
if sabnzbd.WIN_SERVICE and mail: if sabnzbd.WIN_SERVICE and mail:
mail.send('active') mail.send('active')
@ -1626,20 +1626,19 @@ def main():
# 3 sec polling tasks # 3 sec polling tasks
# Check for auto-restart request # Check for auto-restart request
if cherrypy.engine.execv: # Or special restart cases like Mac and WindowsService
if sabnzbd.SCHED_RESTART: if sabnzbd.TRIGGER_RESTART:
scheduler.abort() # Shutdown
sabnzbd.halt() cherrypy.engine.exit()
else: sabnzbd.halt()
scheduler.stop()
sabnzbd.halt()
cherrypy.engine.exit()
sabnzbd.SABSTOP = True sabnzbd.SABSTOP = True
if sabnzbd.downloader.Downloader.do.paused: if sabnzbd.downloader.Downloader.do.paused:
re_argv.append('-p') sabnzbd.RESTART_ARGS.append('-p')
if autorestarted: if autorestarted:
re_argv.append('--autorestarted') sabnzbd.RESTART_ARGS.append('--autorestarted')
sys.argv = re_argv sys.argv = sabnzbd.RESTART_ARGS
os.chdir(org_dir) os.chdir(org_dir)
if sabnzbd.DARWIN: if sabnzbd.DARWIN:
# When executing from sources on osx, after a restart, process is detached from console # When executing from sources on osx, after a restart, process is detached from console

43
sabnzbd/__init__.py

@ -48,6 +48,7 @@ KERNEL32 = None
if os.name == 'nt': if os.name == 'nt':
WIN32 = True WIN32 = True
from util.apireg import del_connection_info
try: try:
import ctypes import ctypes
KERNEL32 = ctypes.windll.LoadLibrary("Kernel32.dll") KERNEL32 = ctypes.windll.LoadLibrary("Kernel32.dll")
@ -132,6 +133,7 @@ START = datetime.datetime.now()
MY_NAME = None MY_NAME = None
MY_FULLNAME = None MY_FULLNAME = None
RESTART_ARGS = []
NEW_VERSION = None NEW_VERSION = None
DIR_HOME = None DIR_HOME = None
DIR_APPDATA = None DIR_APPDATA = None
@ -167,7 +169,7 @@ SABSTOP = False
RESTART_REQ = False RESTART_REQ = False
PAUSED_ALL = False PAUSED_ALL = False
OLD_QUEUE = False OLD_QUEUE = False
SCHED_RESTART = False # Set when restarted through scheduler TRIGGER_RESTART = False # To trigger restart for Scheduler, WinService and Mac
WINTRAY = None # Thread for the Windows SysTray icon WINTRAY = None # Thread for the Windows SysTray icon
WEBUI_READY = False WEBUI_READY = False
LAST_WARNING = None LAST_WARNING = None
@ -388,6 +390,10 @@ def halt():
logging.info('SABnzbd shutting down...') logging.info('SABnzbd shutting down...')
__SHUTTING_DOWN__ = True __SHUTTING_DOWN__ = True
# Stop the windows tray icon
if sabnzbd.WINTRAY:
sabnzbd.WINTRAY.terminate = True
sabnzbd.zconfig.remove_server() sabnzbd.zconfig.remove_server()
rss.stop() rss.stop()
@ -437,9 +443,6 @@ def halt():
except: except:
logging.error(T('Fatal error at saving state'), exc_info=True) logging.error(T('Fatal error at saving state'), exc_info=True)
# Stop the windows tray icon
if sabnzbd.WINTRAY:
sabnzbd.WINTRAY.terminate = True
# The Scheduler cannot be stopped when the stop was scheduled. # The Scheduler cannot be stopped when the stop was scheduled.
# Since all warm-restarts have been removed, it's not longer # Since all warm-restarts have been removed, it's not longer
@ -452,6 +455,28 @@ def halt():
__INITIALIZED__ = False __INITIALIZED__ = False
def trigger_restart():
""" Trigger a restart by setting a flag an shutting down CP """
if sabnzbd.downloader.Downloader.do.paused:
sabnzbd.RESTART_ARGS.append('-p')
sys.argv = sabnzbd.RESTART_ARGS
# Stop all services
sabnzbd.halt()
cherrypy.engine.exit()
if sabnzbd.WIN32:
# Remove connection info for faster restart
del_connection_info()
# Leave the harder restarts to the polling in SABnzbd.py
if sabnzbd.WIN_SERVICE or sabnzbd.DARWIN:
sabnzbd.TRIGGER_RESTART = True
else:
# Do the restart right now
cherrypy.engine._do_execv()
############################################################################## ##############################################################################
# Misc Wrappers # Misc Wrappers
############################################################################## ##############################################################################
@ -742,19 +767,17 @@ def system_standby():
def shutdown_program(): def shutdown_program():
""" Stop program after halting and saving """ """ Stop program after halting and saving """
logging.info("Performing sabnzbd shutdown") logging.info("Performing sabnzbd shutdown")
Thread(target=halt).start() sabnzbd.halt()
while __INITIALIZED__: cherrypy.engine.exit()
time.sleep(1.0) sabnzbd.SABSTOP = True
os._exit(0)
def restart_program(): def restart_program():
""" Restart program (used by scheduler) """ """ Restart program (used by scheduler) """
global SCHED_RESTART
logging.info("Scheduled restart request") logging.info("Scheduled restart request")
# Just set the stop flag, because stopping CherryPy from # Just set the stop flag, because stopping CherryPy from
# the scheduler is not reliable # the scheduler is not reliable
cherrypy.engine.execv = SCHED_RESTART = True sabnzbd.TRIGGER_RESTART = True
def change_queue_complete_action(action, new=True): def change_queue_complete_action(action, new=True):

8
sabnzbd/interface.py

@ -942,8 +942,8 @@ class QueuePage(object):
else: else:
yield "Initiating shutdown..." yield "Initiating shutdown..."
sabnzbd.halt() sabnzbd.halt()
cherrypy.engine.exit()
yield "<br>SABnzbd-%s shutdown finished" % sabnzbd.__version__ yield "<br>SABnzbd-%s shutdown finished" % sabnzbd.__version__
cherrypy.engine.exit()
sabnzbd.SABSTOP = True sabnzbd.SABSTOP = True
@cherrypy.expose @cherrypy.expose
@ -1293,9 +1293,8 @@ class ConfigPage(object):
yield msg yield msg
else: else:
yield T('Initiating restart...<br />') yield T('Initiating restart...<br />')
sabnzbd.halt()
yield T('&nbsp<br />SABnzbd shutdown finished.<br />Wait for about 5 second and then click the button below.<br /><br /><strong><a href="..">Refresh</a></strong><br />') yield T('&nbsp<br />SABnzbd shutdown finished.<br />Wait for about 5 second and then click the button below.<br /><br /><strong><a href="..">Refresh</a></strong><br />')
cherrypy.engine.restart() sabnzbd.trigger_restart()
@cherrypy.expose @cherrypy.expose
def repair(self, **kwargs): def repair(self, **kwargs):
@ -1305,9 +1304,8 @@ class ConfigPage(object):
else: else:
sabnzbd.request_repair() sabnzbd.request_repair()
yield T('Initiating restart...<br />') yield T('Initiating restart...<br />')
sabnzbd.halt()
yield T('&nbsp<br />SABnzbd shutdown finished.<br />Wait for about 5 second and then click the button below.<br /><br /><strong><a href="..">Refresh</a></strong><br />') yield T('&nbsp<br />SABnzbd shutdown finished.<br />Wait for about 5 second and then click the button below.<br /><br /><strong><a href="..">Refresh</a></strong><br />')
cherrypy.engine.restart() sabnzbd.trigger_restart()
@cherrypy.expose @cherrypy.expose
def delete(self, **kwargs): def delete(self, **kwargs):

9
sabnzbd/osxmenu.py

@ -721,8 +721,7 @@ class SABnzbdDelegate(NSObject):
def restartAction_(self, sender): def restartAction_(self, sender):
self.setMenuTitle("\n\n%s\n" % (T('Stopping...'))) self.setMenuTitle("\n\n%s\n" % (T('Stopping...')))
sabnzbd.halt() sabnzbd.trigger_restart()
cherrypy.engine.restart()
self.setMenuTitle("\n\n%s\n" % (T('Stopping...'))) self.setMenuTitle("\n\n%s\n" % (T('Stopping...')))
def restartSafeHost_(self, sender): def restartSafeHost_(self, sender):
@ -732,8 +731,7 @@ class SABnzbdDelegate(NSObject):
sabnzbd.cfg.enable_https.set(False) sabnzbd.cfg.enable_https.set(False)
sabnzbd.config.save_config() sabnzbd.config.save_config()
self.setMenuTitle("\n\n%s\n" % (T('Stopping...'))) self.setMenuTitle("\n\n%s\n" % (T('Stopping...')))
sabnzbd.halt() sabnzbd.trigger_restart()
cherrypy.engine.restart()
self.setMenuTitle("\n\n%s\n" % (T('Stopping...'))) self.setMenuTitle("\n\n%s\n" % (T('Stopping...')))
def restartNoLogin_(self, sender): def restartNoLogin_(self, sender):
@ -741,8 +739,7 @@ class SABnzbdDelegate(NSObject):
sabnzbd.cfg.password.set('') sabnzbd.cfg.password.set('')
sabnzbd.config.save_config() sabnzbd.config.save_config()
self.setMenuTitle("\n\n%s\n" % (T('Stopping...'))) self.setMenuTitle("\n\n%s\n" % (T('Stopping...')))
sabnzbd.halt() sabnzbd.trigger_restart()
cherrypy.engine.restart()
self.setMenuTitle("\n\n%s\n" % (T('Stopping...'))) self.setMenuTitle("\n\n%s\n" % (T('Stopping...')))
def application_openFiles_(self, nsapp, filenames): def application_openFiles_(self, nsapp, filenames):

9
sabnzbd/sabtray.py

@ -131,8 +131,7 @@ class SABTrayThread(SysTrayIconThread):
# menu handler # menu handler
def restart(self, icon): def restart(self, icon):
self.hover_text = self.txt_restart self.hover_text = self.txt_restart
sabnzbd.halt() sabnzbd.trigger_restart()
cherrypy.engine.restart()
# menu handler # menu handler
def rss(self, icon): def rss(self, icon):
@ -145,8 +144,7 @@ class SABTrayThread(SysTrayIconThread):
sabnzbd.cfg.password.set('') sabnzbd.cfg.password.set('')
sabnzbd.config.save_config() sabnzbd.config.save_config()
self.hover_text = self.txt_restart self.hover_text = self.txt_restart
sabnzbd.halt() sabnzbd.trigger_restart()
cherrypy.engine.restart()
# menu handler # menu handler
def defhost(self, icon): def defhost(self, icon):
@ -154,8 +152,7 @@ class SABTrayThread(SysTrayIconThread):
sabnzbd.cfg.enable_https.set(False) sabnzbd.cfg.enable_https.set(False)
sabnzbd.config.save_config() sabnzbd.config.save_config()
self.hover_text = self.txt_restart self.hover_text = self.txt_restart
sabnzbd.halt() sabnzbd.trigger_restart()
cherrypy.engine.restart()
# menu handler - adapted from interface.py # menu handler - adapted from interface.py
def shutdown(self, icon): def shutdown(self, icon):

3
sabnzbd/sabtraylinux.py

@ -165,8 +165,7 @@ class StatusIcon(Thread):
def restart(self, icon): def restart(self, icon):
self.hover_text = T('Restart') self.hover_text = T('Restart')
sabnzbd.halt() sabnzbd.trigger_restart()
cherrypy.engine.restart()
def shutdown(self, icon): def shutdown(self, icon):
self.hover_text = T('Shutdown') self.hover_text = T('Shutdown')

Loading…
Cancel
Save