Переглянути джерело

Wait for MPD to be ready before starting

boyska 9 роки тому
батько
коміт
066377e589
3 змінених файлів з 25 додано та 4 видалено
  1. 2 0
      larigira/config.py
  2. 15 1
      larigira/larigira.py
  3. 8 3
      larigira/mpc.py

+ 2 - 0
larigira/config.py

@@ -17,6 +17,8 @@ def get_conf(prefix='LARIGIRA_'):
     conf['DB_URI'] = 'larigira.db'
     conf['BOOTSTRAP_SERVE_LOCAL'] = True
     conf['SECRET_KEY'] = 'Please replace me!'
+    conf['MPD_WAIT_START'] = True
+    conf['MPD_WAIT_START_RETRYSECS'] = 5
     conf.update(from_envvars(prefix=prefix))
     return conf
 

+ 15 - 1
larigira/larigira.py

@@ -7,6 +7,7 @@ monkey.patch_all(subprocess=True)
 
 import sys
 import signal
+from time import sleep
 import logging
 FORMAT = '%(asctime)s|%(levelname)s[%(name)s:%(lineno)d] %(message)s'
 logging.basicConfig(level=logging.INFO,
@@ -16,7 +17,7 @@ logging.basicConfig(level=logging.INFO,
 import gevent
 from gevent.wsgi import WSGIServer
 
-from .mpc import Player
+from .mpc import Player, get_mpd_client
 from .event import Monitor
 from .config import get_conf
 from .rpc import create_app
@@ -49,6 +50,19 @@ class Larigira(object):
 
 def main():
     logging.basicConfig(level=logging.DEBUG)
+    if(get_conf()['MPD_WAIT_START']):
+        while True:
+            try:
+                get_mpd_client(get_conf())
+            except Exception:
+                logging.debug("Could not connect to MPD, waiting")
+                sleep(int(get_conf()['MPD_WAIT_START_RETRYSECS']))
+            else:
+                logging.info("MPD ready!")
+                break
+
+
+
     larigira = Larigira()
     larigira.start()
 

+ 8 - 3
larigira/mpc.py

@@ -9,15 +9,20 @@ from eventutils import ParentedLet, Timer
 from audiogen import audiogenerate
 
 
+def get_mpd_client(conf):
+    client = MPDClient()
+    client.connect(conf['MPD_HOST'], conf['MPD_PORT'])
+
+    return client
+
+
 class MpcWatcher(ParentedLet):
     def __init__(self, queue, conf, client=None):
         ParentedLet.__init__(self, queue)
         self.log = logging.getLogger(self.__class__.__name__)
         self.conf = conf
         if client is None:
-            self.client = MPDClient()
-            # TODO: use config values
-            self.client.connect(self.conf['MPD_HOST'], self.conf['MPD_PORT'])
+            self.client = get_mpd_client(self.conf)
         else:
             self.client = client  # assume it is already connected