SIGHUP, use mpd connection without subcommand
beside being ugly, using "mpc idle" subprocess was making the whole thing unable to terminate & release HTTP port when closing.
This commit is contained in:
parent
bd6ccaf0be
commit
6f9e4b199b
3 changed files with 36 additions and 8 deletions
37
mpc.py
37
mpc.py
|
@ -6,12 +6,12 @@ import logging
|
||||||
logging.basicConfig(level=logging.INFO,
|
logging.basicConfig(level=logging.INFO,
|
||||||
format='%(asctime)s %(message)s',
|
format='%(asctime)s %(message)s',
|
||||||
datefmt='%H:%M:%S')
|
datefmt='%H:%M:%S')
|
||||||
from subprocess import check_output
|
import signal
|
||||||
|
|
||||||
import gevent
|
import gevent
|
||||||
from gevent.queue import Queue
|
from gevent.queue import Queue
|
||||||
from gevent.wsgi import WSGIServer
|
from gevent.wsgi import WSGIServer
|
||||||
|
from mpd import MPDClient
|
||||||
|
|
||||||
from eventutils import ParentedLet, CeleryTask, Timer
|
from eventutils import ParentedLet, CeleryTask, Timer
|
||||||
from task import create as create_continous
|
from task import create as create_continous
|
||||||
|
@ -19,12 +19,20 @@ import rpc
|
||||||
|
|
||||||
|
|
||||||
class MpcWatcher(ParentedLet):
|
class MpcWatcher(ParentedLet):
|
||||||
def __init__(self, queue):
|
def __init__(self, queue, client=None):
|
||||||
ParentedLet.__init__(self, queue)
|
ParentedLet.__init__(self, queue)
|
||||||
|
if client is None:
|
||||||
|
self.client = MPDClient()
|
||||||
|
# TODO: use config values
|
||||||
|
self.client.connect("localhost", 6600)
|
||||||
|
else:
|
||||||
|
self.client = client # assume it is already connected
|
||||||
|
|
||||||
def do_business(self):
|
def do_business(self):
|
||||||
while True:
|
while True:
|
||||||
status = check_output(['mpc', 'idle']).decode('utf-8').strip()
|
# status = check_output(['mpc', 'idle']).decode('utf-8').strip()
|
||||||
|
status = self.client.idle()[0]
|
||||||
|
logging.info(status)
|
||||||
yield ('mpc', status)
|
yield ('mpc', status)
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,17 +43,18 @@ class Player(gevent.Greenlet):
|
||||||
self.q = Queue()
|
self.q = Queue()
|
||||||
|
|
||||||
def check_playlist(self):
|
def check_playlist(self):
|
||||||
out = check_output(['mpc', 'playlist']).decode('utf-8').strip()
|
mpd_client = MPDClient()
|
||||||
songs = out.split('\n')
|
# TODO: use config values
|
||||||
|
mpd_client.connect("localhost", 6600)
|
||||||
|
songs = mpd_client.playlist()
|
||||||
if(len(songs) >= self.min_playlist_length):
|
if(len(songs) >= self.min_playlist_length):
|
||||||
|
|
||||||
return
|
return
|
||||||
logging.info('need to add new songs')
|
logging.info('need to add new songs')
|
||||||
CeleryTask(create_continous, self.q).start()
|
CeleryTask(create_continous, self.q).start()
|
||||||
CeleryTask(create_continous, self.q).start()
|
CeleryTask(create_continous, self.q).start()
|
||||||
|
|
||||||
def _run(self):
|
def _run(self):
|
||||||
MpcWatcher(self.q).start()
|
MpcWatcher(self.q, client=None).start()
|
||||||
Timer(6000, self.q).start()
|
Timer(6000, self.q).start()
|
||||||
http_server = WSGIServer(('', 5000), rpc.create_app(self.q))
|
http_server = WSGIServer(('', 5000), rpc.create_app(self.q))
|
||||||
http_server.start()
|
http_server.start()
|
||||||
|
@ -65,7 +74,19 @@ class Player(gevent.Greenlet):
|
||||||
logging.info(str(value))
|
logging.info(str(value))
|
||||||
|
|
||||||
|
|
||||||
|
def on_player_crash(*args, **kwargs):
|
||||||
|
print('A crash occurred in "main" greenlet. Aborting...')
|
||||||
|
import sys
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
p = Player()
|
p = Player()
|
||||||
p.start()
|
p.start()
|
||||||
|
p.link_exception(on_player_crash)
|
||||||
|
|
||||||
|
def sig(*args):
|
||||||
|
print('invoked sig', args)
|
||||||
|
p.q.put('signal', *args)
|
||||||
|
gevent.signal(signal.SIGHUP, sig, signal.SIGHUP)
|
||||||
gevent.wait()
|
gevent.wait()
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
Flask==0.10.1
|
||||||
|
Jinja2==2.7.3
|
||||||
|
MarkupSafe==0.23
|
||||||
|
Werkzeug==0.9.6
|
||||||
amqp==1.4.6
|
amqp==1.4.6
|
||||||
anyjson==0.3.3
|
anyjson==0.3.3
|
||||||
billiard==3.3.0.18
|
billiard==3.3.0.18
|
||||||
|
@ -5,10 +9,12 @@ celery==3.1.16
|
||||||
eventlet==0.15.2
|
eventlet==0.15.2
|
||||||
gevent==1.0.1
|
gevent==1.0.1
|
||||||
greenlet==0.4.5
|
greenlet==0.4.5
|
||||||
|
itsdangerous==0.24
|
||||||
kombu==3.0.23
|
kombu==3.0.23
|
||||||
py==1.4.26
|
py==1.4.26
|
||||||
pyinotify==0.9.4
|
pyinotify==0.9.4
|
||||||
pytest==2.6.4
|
pytest==2.6.4
|
||||||
|
python-mpd2==0.5.3
|
||||||
pytz==2014.7
|
pytz==2014.7
|
||||||
redis==2.10.3
|
redis==2.10.3
|
||||||
wsgiref==0.1.2
|
wsgiref==0.1.2
|
||||||
|
|
1
rpc.py
1
rpc.py
|
@ -5,6 +5,7 @@ rpc = Blueprint('rpc', __name__, url_prefix='/api')
|
||||||
|
|
||||||
|
|
||||||
def send_to_parent(kind, *args):
|
def send_to_parent(kind, *args):
|
||||||
|
'''similar to the behaviour of a ParentedLet'''
|
||||||
if not hasattr(current_app, 'queue'):
|
if not hasattr(current_app, 'queue'):
|
||||||
logging.debug('no parent queue; aborting send')
|
logging.debug('no parent queue; aborting send')
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue