Tick on ALRM signal received

This commit is contained in:
boyska 2016-09-08 23:50:29 +02:00
parent cf58424524
commit a2b2a5ebc5
No known key found for this signature in database
GPG key ID: 7395DCAE58289CA9
3 changed files with 20 additions and 11 deletions

View file

@ -171,7 +171,7 @@ class Monitor(ParentedLet):
while True:
value = self.q.get()
kind = value['kind']
if kind == 'timer':
if kind in ('forcetick', 'timer'):
gevent.spawn(self.on_tick)
else:
self.log.warning("Unknown message: %s" % str(value))

View file

@ -16,7 +16,6 @@ import gevent
from gevent.wsgi import WSGIServer
from .mpc import Controller, get_mpd_client
from .event import Monitor
from .config import get_conf
from .rpc import create_app
@ -31,18 +30,12 @@ class Larigira(object):
self.log = logging.getLogger('larigira')
self.conf = get_conf()
self.controller = Controller(self.conf)
if 'DB_URI' in self.conf:
self.monitor = Monitor(self.controller.q, self.conf)
else:
self.monitor = None
self.controller.link_exception(on_main_crash)
self.http_server = WSGIServer(('', 5000),
create_app(self.controller.q, self))
def start(self):
self.controller.start()
if self.monitor is not None:
self.monitor.start()
self.http_server.start()
@ -71,7 +64,8 @@ def main():
def sig(*args):
print('invoked sig', args)
larigira.controller.q.put(dict(kind='signal', args=args))
gevent.signal(signal.SIGHUP, sig, signal.SIGHUP)
for signum in (signal.SIGHUP, signal.SIGALRM):
gevent.signal(signum, sig, signum)
gevent.wait()
if __name__ == '__main__':

View file

@ -1,10 +1,12 @@
from __future__ import print_function
import logging
import signal
import gevent
from gevent.queue import Queue
from mpd import MPDClient, ConnectionError, CommandError
from .event import Monitor
from .eventutils import ParentedLet, Timer
from .audiogen import audiogenerate
@ -96,8 +98,15 @@ class Controller(gevent.Greenlet):
self.conf = conf
self.q = Queue()
self.player = Player(self.conf)
if 'DB_URI' in self.conf:
self.monitor = Monitor(self.q, self.conf)
self.monitor.parent_greenlet = self
else:
self.monitor = None
def _run(self):
if self.monitor is not None:
self.monitor.start()
mw = MpcWatcher(self.q, self.conf, client=None)
mw.parent_greenlet = self
mw.start()
@ -112,7 +121,8 @@ class Controller(gevent.Greenlet):
# emitter = value['emitter']
kind = value['kind']
args = value['args']
if kind == 'timer' or (kind == 'mpc' and args[0] in ('player', 'playlist')):
if kind == 'timer' or (kind == 'mpc' and
args[0] in ('player', 'playlist')):
gevent.Greenlet.spawn(self.player.check_playlist)
elif kind == 'mpc':
pass
@ -122,6 +132,11 @@ class Controller(gevent.Greenlet):
except AssertionError:
raise
except Exception:
self.log.exception("Error while adding to queue; bad audiogen output?")
self.log.exception("Error while adding to queue; "
"bad audiogen output?")
elif kind == 'signal' and args[0] == signal.SIGALRM:
# it's a tick!
self.monitor.q.put(dict(kind='forcetick'))
gevent.Greenlet.spawn(self.player.check_playlist)
else:
self.log.warning("Unknown message: %s" % str(value))