Browse Source

Fix for crashing process and restarting

master
Dirk Alders 8 months ago
parent
commit
3831e56e3d
2 changed files with 15 additions and 10 deletions
  1. 1
    1
      config_example/config.py
  2. 14
    9
      spotify.py

+ 1
- 1
config_example/config.py View File

16
 __BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
16
 __BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
17
 APP_NAME = "spotify"
17
 APP_NAME = "spotify"
18
 LOGTARGET = 'stdout'   # possible choices are: 'logfile' or 'stdout'
18
 LOGTARGET = 'stdout'   # possible choices are: 'logfile' or 'stdout'
19
-LOGLVL = 'DEBUG'
19
+LOGLVL = 'INFO'
20
 
20
 
21
 LOGHOST = 'cutelog'
21
 LOGHOST = 'cutelog'
22
 LOGPORT = 19996
22
 LOGPORT = 19996

+ 14
- 9
spotify.py View File

28
         logger.info("Starting Librespot...")
28
         logger.info("Starting Librespot...")
29
         self.__state_callback__ = state_callback
29
         self.__state_callback__ = state_callback
30
         self.__title_callback__ = title_callback
30
         self.__title_callback__ = title_callback
31
-        self.__process__ = subprocess.Popen(["librespot", "-v", "--name", config.DEVICE_NAME],
32
-                               shell=False,
33
-                               # We pipe the output to an internal pipe
34
-                               stdout=subprocess.PIPE,
35
-                               stderr=subprocess.STDOUT)
31
+        self.__start_process__()
36
         self.__state__ = None
32
         self.__state__ = None
37
         self.__preload_state__ = False
33
         self.__preload_state__ = False
38
         self.__title__ = None
34
         self.__title__ = None
41
         self.set_state(False)
37
         self.set_state(False)
42
         self.set_title("")
38
         self.set_title("")
43
 
39
 
40
+    def __start_process__(self):
41
+        self.__process__ = subprocess.Popen(["librespot", "-v", "--name", config.DEVICE_NAME],
42
+                               shell=False,
43
+                               # We pipe the output to an internal pipe
44
+                               stdout=subprocess.PIPE,
45
+                               stderr=subprocess.STDOUT)
46
+
44
     def run(self):
47
     def run(self):
45
         while True:
48
         while True:
46
             output = self.__process__.stdout.readline()
49
             output = self.__process__.stdout.readline()
47
             # Polling returns None when the program is still running, return_code otherwise
50
             # Polling returns None when the program is still running, return_code otherwise
48
             return_code = self.__process__.poll()
51
             return_code = self.__process__.poll()
49
             if return_code is not None:
52
             if return_code is not None:
50
-                self.__process__.close()
53
+                #self.__process__.close()
51
                 # Program ended, get exit/return code
54
                 # Program ended, get exit/return code
52
-                raise RuntimeError("Command '{}' finished with exit code {}".format(command, return_code))
53
-        
55
+                #raise RuntimeError
56
+                logger.exception(("Command '{}' finished with exit code {}".format(command, return_code)))
54
                 # If the output is not empty, feed it to the function, strip the newline first
57
                 # If the output is not empty, feed it to the function, strip the newline first
55
-            if output:
58
+                self.__start_process__()
59
+                output = None
60
+            elif output:
56
                 out_txt = output.decode('utf-8').strip('\n').strip()
61
                 out_txt = output.decode('utf-8').strip('\n').strip()
57
                 out_txt = out_txt[out_txt.find(']') + 2:]
62
                 out_txt = out_txt[out_txt.find(']') + 2:]
58
                 #logger.debug("librespot output: %s", out_txt)
63
                 #logger.debug("librespot output: %s", out_txt)

Loading…
Cancel
Save