Browse Source

Use SABHelper also to guard the SABnzbd service.

Auto-restart when SABnzbd is no longer running and didn't sign-off properly.
tags/0.6.0
ShyPike 15 years ago
parent
commit
90d168cf3c
  1. 28
      SABHelper.py
  2. 28
      SABnzbd.py

28
SABHelper.py

@ -43,25 +43,43 @@ def HandleCommandLine(allow_service=True):
""" Handle command line for a Windows Service
Prescribed name that will be called by Py2Exe.
You MUST set 'cmdline_style':'custom' in the package.py!
Returns True when any service commands were detected.
"""
win32serviceutil.HandleCommandLine(SABHelper)
def start_sab():
return subprocess.Popen('net start SABnzbd', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).stdout.read()
#------------------------------------------------------------------------------
def main():
mail = MailSlot()
if not mail.create(200):
if not mail.create(10):
return '- Cannot create Mailslot'
active = False # SABnzbd should be running
counter = 0 # Time allowed for SABnzbd to be silent
while True:
msg = mail.receive()
if msg == 'restart':
time.sleep(1.0)
res = subprocess.Popen('net start SABnzbd', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).stdout.read()
counter = 0
start_sab()
elif msg == 'stop':
active = False
elif msg == 'active':
active = True
counter = 0
if active:
counter += 1
if counter > 120: # 120 seconds
counter = 0
start_sab()
rc = win32event.WaitForMultipleObjects((WIN_SERVICE.hWaitStop,
WIN_SERVICE.overlapped.hEvent), 0, 500)
WIN_SERVICE.overlapped.hEvent), 0, 1000)
if rc == win32event.WAIT_OBJECT_0:
mail.disconnect()
return ''
@ -79,7 +97,7 @@ class SABHelper(win32serviceutil.ServiceFramework):
_svc_display_name_ = 'SABnzbd Helper'
_svc_deps_ = ["EventLog", "Tcpip"]
_svc_description_ = 'Automated downloading from Usenet. ' \
'This service helps SABnzbdcd.. to restart itself.'
'This service helps SABnzbd to restart itself.'
def __init__(self, args):
global WIN_SERVICE

28
SABnzbd.py

@ -1228,6 +1228,17 @@ def main():
check_latest_version()
autorestarted = False
mail = None
if sabnzbd.WIN_SERVICE:
mail = MailSlot()
if mail.connect():
logging.info('Connected to the SABHelper service')
mail.send('active')
else:
logging.error('Cannot reach the SABHelper service')
mail = None
# Have to keep this running, otherwise logging will terminate
timer = 0
while not sabnzbd.SABSTOP:
@ -1263,6 +1274,9 @@ def main():
if not sabnzbd.check_all_tasks():
autorestarted = True
cherrypy.engine.execv = True
# Notify guardian
if sabnzbd.WIN_SERVICE and mail:
mail.send('active')
else:
timer += 1
@ -1293,20 +1307,18 @@ def main():
pid = os.fork()
if pid == 0:
os.execv(sys.executable, args)
elif sabnzbd.WIN_SERVICE:
logging.info('Asking the SABnzbdHelper service for a restart')
mail = MailSlot()
if mail.connect():
mail.send('restart')
mail.disconnect()
else:
logging.error('Cannot reach the SABnzbdHelper service')
elif sabnzbd.WIN_SERVICE and mail:
logging.info('Asking the SABHelper service for a restart')
mail.send('restart')
mail.disconnect()
return
else:
cherrypy.engine._do_execv()
config.save_config()
if sabnzbd.WIN_SERVICE and mail:
mail.send('stop')
notify("SAB_Shutdown", None)
osx.sendGrowlMsg('SABnzbd',T('grwl-shutdown-end-msg'),osx.NOTIFICATION['startup'])
logging.info('Leaving SABnzbd')

Loading…
Cancel
Save