Wait for MPD to be ready before starting

This commit is contained in:
boyska 2015-05-12 17:40:47 +02:00
parent 16383cf777
commit 066377e589
3 changed files with 25 additions and 4 deletions

View file

@ -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

View file

@ -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()

View file

@ -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