From 1f6c7aae60ecd1939f665fa5ffb97e634cfbbb97 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 26 Jun 2021 11:14:26 +0200 Subject: [PATCH] get_redis() should be a general utility method --- platypush/backend/music/spotify/connect/__init__.py | 7 ++++--- platypush/backend/music/spotify/connect/event/__init__.py | 6 ------ platypush/backend/music/spotify/connect/event/__main__.py | 5 +++-- platypush/utils/__init__.py | 7 +++++++ 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/platypush/backend/music/spotify/connect/__init__.py b/platypush/backend/music/spotify/connect/__init__.py index b353704e..ee571b86 100644 --- a/platypush/backend/music/spotify/connect/__init__.py +++ b/platypush/backend/music/spotify/connect/__init__.py @@ -11,8 +11,9 @@ from platypush.backend import Backend from platypush.config import Config from platypush.message.event.music import MusicPlayEvent, MusicPauseEvent, MusicStopEvent, \ NewPlayingTrackEvent, SeekChangeEvent, VolumeChangeEvent +from platypush.utils import get_redis -from .event import get_redis, status_queue +from .event import status_queue class MusicSpotifyConnectBackend(Backend): @@ -205,11 +206,11 @@ class MusicSpotifyConnectBackend(Backend): redis = get_redis() while not self.should_stop(): - msg = redis.get(parse=False) + msg = redis.blpop(status_queue, timeout=1) if not msg: continue - self._process_status_msg(json.loads(msg)) + self._process_status_msg(json.loads(msg[1])) return loop diff --git a/platypush/backend/music/spotify/connect/event/__init__.py b/platypush/backend/music/spotify/connect/event/__init__.py index 8cf20930..e0514fa5 100644 --- a/platypush/backend/music/spotify/connect/event/__init__.py +++ b/platypush/backend/music/spotify/connect/event/__init__.py @@ -1,7 +1 @@ -from platypush.bus.redis import RedisBus - status_queue = 'platypush/music/spotify/connect/status' - - -def get_redis() -> RedisBus: - return RedisBus(redis_queue=status_queue) diff --git a/platypush/backend/music/spotify/connect/event/__main__.py b/platypush/backend/music/spotify/connect/event/__main__.py index 7cdb2cfa..c9059f66 100644 --- a/platypush/backend/music/spotify/connect/event/__main__.py +++ b/platypush/backend/music/spotify/connect/event/__main__.py @@ -1,7 +1,8 @@ import json import os -from . import get_redis +from platypush.utils import get_redis +from . import status_queue environ_variables = [ 'PLAYER_EVENT', @@ -14,7 +15,7 @@ environ_variables = [ def on_librespot_event(): - get_redis().post(json.dumps({ + get_redis().rpush(status_queue, json.dumps({ var: os.environ[var] for var in environ_variables if var in os.environ diff --git a/platypush/utils/__init__.py b/platypush/utils/__init__.py index e7ae2222..abb4de70 100644 --- a/platypush/utils/__init__.py +++ b/platypush/utils/__init__.py @@ -12,6 +12,8 @@ import ssl import urllib.request from typing import Optional, Tuple +from redis import Redis + logger = logging.getLogger('utils') @@ -438,4 +440,9 @@ def get_enabled_plugins() -> dict: return plugins +def get_redis() -> Redis: + from platypush.config import Config + return Redis(**(Config.get('backend.redis') or {}).get('redis_args', {})) + + # vim:sw=4:ts=4:et: