More robust HTTP backend in case Redis wasn't configured
This commit is contained in:
parent
bc895dd984
commit
0b4cfec8e7
1 changed files with 12 additions and 4 deletions
|
@ -112,8 +112,8 @@ class HttpBackend(Backend):
|
||||||
self.disable_websocket = disable_websocket
|
self.disable_websocket = disable_websocket
|
||||||
self.websocket_thread = None
|
self.websocket_thread = None
|
||||||
self.redis_thread = None
|
self.redis_thread = None
|
||||||
|
self.redis = None
|
||||||
self.active_websockets = set()
|
self.active_websockets = set()
|
||||||
self.redis = self._get_redis()
|
|
||||||
|
|
||||||
|
|
||||||
def _get_redis(self):
|
def _get_redis(self):
|
||||||
|
@ -142,7 +142,8 @@ class HttpBackend(Backend):
|
||||||
thread_id=self.redis_thread.ident)
|
thread_id=self.redis_thread.ident)
|
||||||
|
|
||||||
redis = self._get_redis()
|
redis = self._get_redis()
|
||||||
redis.rpush(self.redis_queue, stop_evt)
|
if redis:
|
||||||
|
redis.rpush(self.redis_queue, stop_evt)
|
||||||
|
|
||||||
if self.server_proc:
|
if self.server_proc:
|
||||||
self.server_proc.terminate()
|
self.server_proc.terminate()
|
||||||
|
@ -169,6 +170,9 @@ class HttpBackend(Backend):
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
redis = self._get_redis()
|
redis = self._get_redis()
|
||||||
|
if not redis:
|
||||||
|
continue
|
||||||
|
|
||||||
msg = redis.blpop(self.redis_queue)
|
msg = redis.blpop(self.redis_queue)
|
||||||
msg = Message.build(json.loads(msg[1].decode('utf-8')))
|
msg = Message.build(json.loads(msg[1].decode('utf-8')))
|
||||||
|
|
||||||
|
@ -243,7 +247,8 @@ class HttpBackend(Backend):
|
||||||
return str(response)
|
return str(response)
|
||||||
elif isinstance(msg, Event):
|
elif isinstance(msg, Event):
|
||||||
redis = self._get_redis()
|
redis = self._get_redis()
|
||||||
redis.rpush(self.redis_queue, msg)
|
if redis:
|
||||||
|
redis.rpush(self.redis_queue, msg)
|
||||||
|
|
||||||
return jsonify({ 'status': 'ok' })
|
return jsonify({ 'status': 'ok' })
|
||||||
|
|
||||||
|
@ -276,7 +281,8 @@ class HttpBackend(Backend):
|
||||||
widget=widget, **(json.loads(http_request.data.decode('utf-8'))))
|
widget=widget, **(json.loads(http_request.data.decode('utf-8'))))
|
||||||
|
|
||||||
redis = self._get_redis()
|
redis = self._get_redis()
|
||||||
redis.rpush(self.redis_queue, event)
|
if redis:
|
||||||
|
redis.rpush(self.redis_queue, event)
|
||||||
return jsonify({ 'status': 'ok' })
|
return jsonify({ 'status': 'ok' })
|
||||||
|
|
||||||
@app.route('/static/<path>', methods=['GET'])
|
@app.route('/static/<path>', methods=['GET'])
|
||||||
|
@ -392,6 +398,8 @@ class HttpBackend(Backend):
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
super().run()
|
super().run()
|
||||||
|
os.putenv('FLASK_APP', 'platypush')
|
||||||
|
os.putenv('FLASK_ENV', 'development')
|
||||||
self.logger.info('Initialized HTTP backend on port {}'.format(self.port))
|
self.logger.info('Initialized HTTP backend on port {}'.format(self.port))
|
||||||
|
|
||||||
webserver = self.webserver()
|
webserver = self.webserver()
|
||||||
|
|
Loading…
Reference in a new issue