From 3831e56e3d2c65114725e210b5ef5a76a1a1f691 Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Mon, 26 Feb 2024 19:20:06 +0100 Subject: [PATCH] Fix for crashing process and restarting --- config_example/config.py | 2 +- spotify.py | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/config_example/config.py b/config_example/config.py index f4e3725..4637079 100644 --- a/config_example/config.py +++ b/config_example/config.py @@ -16,7 +16,7 @@ DEVICE_NAME = "Multimedia" __BASEPATH__ = os.path.abspath(os.path.dirname(__file__)) APP_NAME = "spotify" LOGTARGET = 'stdout' # possible choices are: 'logfile' or 'stdout' -LOGLVL = 'DEBUG' +LOGLVL = 'INFO' LOGHOST = 'cutelog' LOGPORT = 19996 diff --git a/spotify.py b/spotify.py index 7dab3de..15ad2be 100644 --- a/spotify.py +++ b/spotify.py @@ -28,11 +28,7 @@ class librespot(object): logger.info("Starting Librespot...") self.__state_callback__ = state_callback self.__title_callback__ = title_callback - self.__process__ = subprocess.Popen(["librespot", "-v", "--name", config.DEVICE_NAME], - shell=False, - # We pipe the output to an internal pipe - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT) + self.__start_process__() self.__state__ = None self.__preload_state__ = False self.__title__ = None @@ -41,18 +37,27 @@ class librespot(object): self.set_state(False) self.set_title("") + def __start_process__(self): + self.__process__ = subprocess.Popen(["librespot", "-v", "--name", config.DEVICE_NAME], + shell=False, + # We pipe the output to an internal pipe + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + def run(self): while True: output = self.__process__.stdout.readline() # Polling returns None when the program is still running, return_code otherwise return_code = self.__process__.poll() if return_code is not None: - self.__process__.close() + #self.__process__.close() # Program ended, get exit/return code - raise RuntimeError("Command '{}' finished with exit code {}".format(command, return_code)) - + #raise RuntimeError + logger.exception(("Command '{}' finished with exit code {}".format(command, return_code))) # If the output is not empty, feed it to the function, strip the newline first - if output: + self.__start_process__() + output = None + elif output: out_txt = output.decode('utf-8').strip('\n').strip() out_txt = out_txt[out_txt.find(']') + 2:] #logger.debug("librespot output: %s", out_txt)