From 0b4cfec8e72d9373377b7589da572d21673c0398 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 20 Jul 2018 11:13:44 +0200 Subject: [PATCH] More robust HTTP backend in case Redis wasn't configured --- platypush/backend/http/__init__.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/platypush/backend/http/__init__.py b/platypush/backend/http/__init__.py index 1a52c452..b2fd7939 100644 --- a/platypush/backend/http/__init__.py +++ b/platypush/backend/http/__init__.py @@ -112,8 +112,8 @@ class HttpBackend(Backend): self.disable_websocket = disable_websocket self.websocket_thread = None self.redis_thread = None + self.redis = None self.active_websockets = set() - self.redis = self._get_redis() def _get_redis(self): @@ -142,7 +142,8 @@ class HttpBackend(Backend): thread_id=self.redis_thread.ident) redis = self._get_redis() - redis.rpush(self.redis_queue, stop_evt) + if redis: + redis.rpush(self.redis_queue, stop_evt) if self.server_proc: self.server_proc.terminate() @@ -169,6 +170,9 @@ class HttpBackend(Backend): while True: redis = self._get_redis() + if not redis: + continue + msg = redis.blpop(self.redis_queue) msg = Message.build(json.loads(msg[1].decode('utf-8'))) @@ -243,7 +247,8 @@ class HttpBackend(Backend): return str(response) elif isinstance(msg, Event): redis = self._get_redis() - redis.rpush(self.redis_queue, msg) + if redis: + redis.rpush(self.redis_queue, msg) return jsonify({ 'status': 'ok' }) @@ -276,7 +281,8 @@ class HttpBackend(Backend): widget=widget, **(json.loads(http_request.data.decode('utf-8')))) redis = self._get_redis() - redis.rpush(self.redis_queue, event) + if redis: + redis.rpush(self.redis_queue, event) return jsonify({ 'status': 'ok' }) @app.route('/static/', methods=['GET']) @@ -392,6 +398,8 @@ class HttpBackend(Backend): def run(self): super().run() + os.putenv('FLASK_APP', 'platypush') + os.putenv('FLASK_ENV', 'development') self.logger.info('Initialized HTTP backend on port {}'.format(self.port)) webserver = self.webserver()