From 25dbfd3a0e864c3ce377dfa055cb1324f3f4d496 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Wed, 9 Jan 2019 15:46:22 -0600 Subject: [PATCH] Fix service restart --- resources/lib/entrypoint/service.py | 2 ++ service.py | 46 ++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/resources/lib/entrypoint/service.py b/resources/lib/entrypoint/service.py index a60d2639..3ee145d6 100644 --- a/resources/lib/entrypoint/service.py +++ b/resources/lib/entrypoint/service.py @@ -128,6 +128,8 @@ class Service(xbmc.Monitor): self.shutdown() + raise Exception("ExitService") + def start_default(self): try: diff --git a/service.py b/service.py index 0cc7aacc..66f85103 100644 --- a/service.py +++ b/service.py @@ -4,6 +4,7 @@ import logging import os +import threading import sys import xbmc @@ -40,6 +41,30 @@ DELAY = int(settings('startupDelay') or 0) ################################################################################################# +class ServiceManager(threading.Thread): + + ''' Service thread. + To allow to restart and reload modules internally. + ''' + exception = None + + def __init__(self): + threading.Thread.__init__(self) + + def run(self): + + service = Service() + + try: + service.service() + except Exception as error: + + if not 'ExitService' in error: + service.shutdown() + + self.exception = error + + if __name__ == "__main__": LOG.warn("-->[ service ]") @@ -48,28 +73,21 @@ if __name__ == "__main__": while True: try: - session = Service() + if DELAY and xbmc.Monitor().waitForAbort(DELAY): + raise Exception("Aborted during startup delay") - try: - if DELAY and xbmc.Monitor().waitForAbort(DELAY): - raise Exception("Aborted during startup delay") + session = ServiceManager() + session.start() + session.join() # Block until the thread exits. - session.service() - except Exception as error: # TODO, build exceptions - - LOG.exception(error) - session.shutdown() - - if 'RestartService' in error: - continue + if 'RestartService' in session.exception: + continue except Exception as error: ''' Issue initializing the service. ''' LOG.exception(error) - break - break LOG.warn("--<[ service ]")