Browse Source

Rework program shutdown and restart to always use the same path

pull/1755/head
Safihre 4 years ago
parent
commit
44d74924e6
  1. 7
      SABnzbd.py
  2. 57
      sabnzbd/__init__.py
  3. 5
      sabnzbd/misc.py
  4. 4
      sabnzbd/sabtray.py
  5. 9
      sabnzbd/utils/systrayiconthread.py

7
SABnzbd.py

@ -1582,13 +1582,6 @@ def main():
else: else:
cherrypy.engine._do_execv() cherrypy.engine._do_execv()
config.save_config()
if sabnzbd.WINTRAY:
sabnzbd.WINTRAY.terminate = True
if sabnzbd.WIN32:
del_connection_info()
# Send our final goodbyes! # Send our final goodbyes!
notifier.send_notification("SABnzbd", T("SABnzbd shutdown finished"), "startup") notifier.send_notification("SABnzbd", T("SABnzbd shutdown finished"), "startup")
logging.info("Leaving SABnzbd") logging.info("Leaving SABnzbd")

57
sabnzbd/__init__.py

@ -362,7 +362,11 @@ def halt():
# Stop the windows tray icon # Stop the windows tray icon
if sabnzbd.WINTRAY: if sabnzbd.WINTRAY:
sabnzbd.WINTRAY.terminate = True sabnzbd.WINTRAY.stop()
# Remove registry information
if sabnzbd.WIN32:
del_connection_info()
sabnzbd.zconfig.remove_server() sabnzbd.zconfig.remove_server()
sabnzbd.utils.ssdp.stop_ssdp() sabnzbd.utils.ssdp.stop_ssdp()
@ -393,7 +397,6 @@ def halt():
except: except:
pass pass
# Stop Required Objects
logging.debug("Stopping downloader") logging.debug("Stopping downloader")
sabnzbd.Downloader.stop() sabnzbd.Downloader.stop()
try: try:
@ -438,31 +441,31 @@ def halt():
sabnzbd.__INITIALIZED__ = False sabnzbd.__INITIALIZED__ = False
def notify_shutdown_loop():
""" Trigger the main loop to wake up"""
with sabnzbd.SABSTOP_CONDITION:
sabnzbd.SABSTOP_CONDITION.notify()
def shutdown_program():
""" Stop program after halting and saving """
if not sabnzbd.SABSTOP:
logging.info("[%s] Performing SABnzbd shutdown", misc.caller_name())
sabnzbd.halt()
cherrypy.engine.exit()
sabnzbd.SABSTOP = True
notify_shutdown_loop()
def trigger_restart(timeout=None): def trigger_restart(timeout=None):
""" Trigger a restart by setting a flag an shutting down CP """ """ Trigger a restart by setting a flag an shutting down CP """
# Sometimes we need to wait a bit to send good-bye to the browser # Sometimes we need to wait a bit to send good-bye to the browser
if timeout: if timeout:
time.sleep(timeout) time.sleep(timeout)
if sabnzbd.WIN32: # Set the flag and wake up the main loop
# Remove connection info for faster restart sabnzbd.TRIGGER_RESTART = True
del_connection_info() notify_shutdown_loop()
# Leave the harder restarts to the polling in SABnzbd.py
if hasattr(sys, "frozen"):
sabnzbd.TRIGGER_RESTART = True
else:
# Add extra arguments
if sabnzbd.Downloader.paused:
sabnzbd.RESTART_ARGS.append("-p")
sys.argv = sabnzbd.RESTART_ARGS
# Stop all services
sabnzbd.halt()
cherrypy.engine.exit()
# Do the restart right now
cherrypy.engine._do_execv()
############################################################################## ##############################################################################
@ -556,6 +559,7 @@ def add_url(url, pp=None, script=None, cat=None, priority=None, nzbname=None, pa
def save_state(): def save_state():
""" Save all internal bookkeeping to disk """ """ Save all internal bookkeeping to disk """
config.save_config()
sabnzbd.ArticleCache.flush_articles() sabnzbd.ArticleCache.flush_articles()
sabnzbd.NzbQueue.save() sabnzbd.NzbQueue.save()
sabnzbd.BPSMeter.save() sabnzbd.BPSMeter.save()
@ -773,17 +777,6 @@ def system_standby():
powersup.linux_standby() powersup.linux_standby()
def shutdown_program():
""" Stop program after halting and saving """
if not sabnzbd.SABSTOP:
logging.info("[%s] Performing SABnzbd shutdown", misc.caller_name())
sabnzbd.halt()
cherrypy.engine.exit()
sabnzbd.SABSTOP = True
with sabnzbd.SABSTOP_CONDITION:
sabnzbd.SABSTOP_CONDITION.notify()
def restart_program(): def restart_program():
""" Restart program (used by scheduler) """ """ Restart program (used by scheduler) """
logging.info("Scheduled restart request") logging.info("Scheduled restart request")

5
sabnzbd/misc.py

@ -64,6 +64,9 @@ if sabnzbd.WIN32:
except ImportError: except ImportError:
pass pass
if sabnzbd.DARWIN:
from PyObjCTools import AppHelper
def time_format(fmt): def time_format(fmt):
""" Return time-format string adjusted for 12/24 hour clock setting """ """ Return time-format string adjusted for 12/24 hour clock setting """
@ -556,8 +559,6 @@ def exit_sab(value):
sys.stderr.flush() sys.stderr.flush()
sys.stdout.flush() sys.stdout.flush()
if hasattr(sys, "frozen") and sabnzbd.DARWIN: if hasattr(sys, "frozen") and sabnzbd.DARWIN:
from PyObjCTools import AppHelper
AppHelper.stopEventLoop() AppHelper.stopEventLoop()
sys.exit(value) sys.exit(value)

4
sabnzbd/sabtray.py

@ -21,6 +21,7 @@ sabtray.py - Systray icon for SABnzbd on Windows, contributed by Jan Schejbal
import os import os
import logging import logging
from threading import Thread
from time import sleep from time import sleep
import sabnzbd import sabnzbd
@ -188,7 +189,8 @@ class SABTrayThread(SysTrayIconThread):
def shutdown(self, icon): def shutdown(self, icon):
self.hover_text = T("Shutdown") self.hover_text = T("Shutdown")
sabnzbd.shutdown_program() # In seperate thread, because the shutdown also stops the tray icon
Thread(target=sabnzbd.shutdown_program).start()
def pause(self): def pause(self):
sabnzbd.Scheduler.plan_resume(0) sabnzbd.Scheduler.plan_resume(0)

9
sabnzbd/utils/systrayiconthread.py

@ -21,12 +21,11 @@ from time import sleep
class SysTrayIconThread(Thread): class SysTrayIconThread(Thread):
QUIT = "QUIT" QUIT = "QUIT"
SPECIAL_ACTIONS = [QUIT] SPECIAL_ACTIONS = [QUIT]
FIRST_ID = 1023 FIRST_ID = 1023
terminate = False
def __init__(self, icon, hover_text, menu_options, on_quit=None, default_menu_index=None, window_class_name=None): def __init__(self, icon, hover_text, menu_options, on_quit=None, default_menu_index=None, window_class_name=None):
super().__init__() super().__init__()
self.terminate = False
self.icon = icon self.icon = icon
self.icons = {} self.icons = {}
self.hover_text = hover_text self.hover_text = hover_text
@ -80,12 +79,16 @@ class SysTrayIconThread(Thread):
self.notify_id = None self.notify_id = None
self.refresh_icon() self.refresh_icon()
def stop(self):
self.terminate = True
sleep(0.15)
def run(self): def run(self):
self.initialize() self.initialize()
while not self.terminate: while not self.terminate:
win32gui.PumpWaitingMessages() win32gui.PumpWaitingMessages()
self.doUpdates() self.doUpdates()
sleep(0.100) sleep(0.1)
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, (self.hwnd, 0)) win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, (self.hwnd, 0))
# Override this # Override this

Loading…
Cancel
Save