From 2bbbf534eb9f6b4f727768e528e67a3cbf1474e0 Mon Sep 17 00:00:00 2001 From: boyska Date: Thu, 8 Sep 2016 20:45:43 +0200 Subject: [PATCH] controller and player are distinct objects FIX #18 --- larigira/larigira.py | 16 ++++++++-------- larigira/mpc.py | 25 ++++++++++++++++--------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/larigira/larigira.py b/larigira/larigira.py index 5fcc84f..11591d3 100644 --- a/larigira/larigira.py +++ b/larigira/larigira.py @@ -19,13 +19,13 @@ logging.basicConfig(level=logging.INFO, import gevent from gevent.wsgi import WSGIServer -from .mpc import Player, get_mpd_client +from .mpc import Controller, get_mpd_client from .event import Monitor from .config import get_conf from .rpc import create_app -def on_player_crash(*args, **kwargs): +def on_main_crash(*args, **kwargs): print('A crash occurred in "main" greenlet. Aborting...') sys.exit(1) @@ -34,17 +34,17 @@ class Larigira(object): def __init__(self): self.log = logging.getLogger('larigira') self.conf = get_conf() - self.player = Player(self.conf) + self.controller = Controller(self.conf) if 'DB_URI' in self.conf: - self.monitor = Monitor(self.player.q, self.conf) + self.monitor = Monitor(self.controller.q, self.conf) else: self.monitor = None - self.player.link_exception(on_player_crash) + self.controller.link_exception(on_main_crash) self.http_server = WSGIServer(('', 5000), - create_app(self.player.q, self)) + create_app(self.controller.q, self)) def start(self): - self.player.start() + self.controller.start() if self.monitor is not None: self.monitor.start() self.http_server.start() @@ -71,7 +71,7 @@ def main(): def sig(*args): print('invoked sig', args) - larigira.player.q.put('signal', *args) + larigira.controller.q.put('signal', *args) gevent.signal(signal.SIGHUP, sig, signal.SIGHUP) gevent.wait() diff --git a/larigira/mpc.py b/larigira/mpc.py index f734e96..e72daba 100644 --- a/larigira/mpc.py +++ b/larigira/mpc.py @@ -42,13 +42,11 @@ class MpcWatcher(ParentedLet): yield ('mpc', status) -class Player(gevent.Greenlet): +class Player: def __init__(self, conf): - gevent.Greenlet.__init__(self) - self.min_playlist_length = 10 - self.log = logging.getLogger(self.__class__.__name__) - self.q = Queue() self.conf = conf + self.log = logging.getLogger(self.__class__.__name__) + self.min_playlist_length = 10 def _get_mpd(self): mpd_client = MPDClient(use_unicode=True) @@ -90,6 +88,15 @@ class Player(gevent.Greenlet): except CommandError: self.log.exception("Cannot insert song {}".format(uri)) + +class Controller(gevent.Greenlet): + def __init__(self, conf): + gevent.Greenlet.__init__(self) + self.log = logging.getLogger(self.__class__.__name__) + self.conf = conf + self.q = Queue() + self.player = Player(self.conf) + def _run(self): mw = MpcWatcher(self.q, self.conf, client=None) mw.parent_greenlet = self @@ -98,7 +105,7 @@ class Player(gevent.Greenlet): t.parent_greenlet = self t.start() # at the very start, run a check! - gevent.Greenlet.spawn(self.check_playlist) + gevent.Greenlet.spawn(self.player.check_playlist) while True: value = self.q.get() self.log.debug('<- %s' % str(value)) @@ -106,15 +113,15 @@ class Player(gevent.Greenlet): kind = value['kind'] args = value['args'] if kind == 'timer' or (kind == 'mpc' and args[0] in ('player', 'playlist')): - gevent.Greenlet.spawn(self.check_playlist) + gevent.Greenlet.spawn(self.player.check_playlist) elif kind == 'mpc': pass elif kind == 'add': try: - self.enqueue(args[0]) + self.player.enqueue(args[0]) except AssertionError: raise - except Exception as exc: + except Exception: self.log.exception("Error while adding to queue; bad audiogen output?") else: self.log.warning("Unknown message: %s" % str(value))