From 58d7de6073b20f03b0d3fa3dbf4bf1316f218d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=BCleki=20F=C3=A1bi=C3=A1n?= Date: Fri, 8 May 2020 19:34:29 +0200 Subject: [PATCH 1/5] updated redis structure --- consumer_api/views/sync_view.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/consumer_api/views/sync_view.py b/consumer_api/views/sync_view.py index a6f8eb5..1b034f1 100644 --- a/consumer_api/views/sync_view.py +++ b/consumer_api/views/sync_view.py @@ -1,4 +1,5 @@ import json +import time from flask import request, current_app, jsonify from flask_classful import FlaskView from db import redis_client # ez nagyon otvar @@ -10,20 +11,25 @@ class SyncView(FlaskView): remote_uuid = request.json['uuid'] remote_ip = request.remote_addr - consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8')) + cust_key = f"customer_{remote_uuid}" - # Log something about it - if remote_uuid not in consumer_list.keys(): + last_known_info = json.loads((redis_client.get("cust_key") or b"{}").decode('utf-8')) + + if not last_known_info: current_app.logger.info(f"New consumer registered (unknown UUID): {remote_uuid} at {remote_ip}") - else: # known - if consumer_list[remote_uuid]['ip'] != remote_ip: + + else: + if last_known_info['ip'] != remote_ip: current_app.logger.info(f"Address of consumer {remote_uuid} changed to {remote_ip}") - consumer_list.update( - {remote_uuid: {"ip": remote_ip}} - ) + info = { + "uuid": remote_uuid, + "ip": remote_ip, + "last_seen": time.time() + } - redis_client.set("consumer_list", json.dumps(consumer_list).encode('utf-8')) + redis_client.set(cust_key, json.dumps(info).encode('utf-8')) + redis_client.expire(cust_key, 30) response = { "uuid": current_app.config['LOCAL_UUID'] From eb034743923248e10e18f39ff9777701ed7f5da2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=BCleki=20F=C3=A1bi=C3=A1n?= Date: Fri, 8 May 2020 19:44:42 +0200 Subject: [PATCH 2/5] Configure timeouts from envvar --- consumer_api/app.py | 2 ++ consumer_api/views/log_view.py | 2 +- consumer_api/views/sync_view.py | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/consumer_api/app.py b/consumer_api/app.py index dbedea3..3f5282f 100644 --- a/consumer_api/app.py +++ b/consumer_api/app.py @@ -19,6 +19,8 @@ sentry_sdk.init("https://0a106e104e114bc9a3fa47f9cb0db2f4@sentry.kmlabz.com/10") app = Flask(__name__) app.config['REDIS_URL'] = os.environ['REDIS_URL'] app.config['LOCAL_UUID'] = os.environ['LOCAL_UUID'] +app.config['CUSTOMER_TIMEOUT'] = int(os.environ.get('CUSTOMER_TIMEOUT', 30)) +app.config['PRODUCER_TIMEOUT'] = int(os.environ.get('PRODUCER_TIMEOUT', 60)) redis_client.init_app(app) diff --git a/consumer_api/views/log_view.py b/consumer_api/views/log_view.py index d2a4d15..2ebf4ff 100644 --- a/consumer_api/views/log_view.py +++ b/consumer_api/views/log_view.py @@ -23,7 +23,7 @@ class LogView(FlaskView): # update expirity redis_client.set(prod_key, remote_ip.encode('utf-8')) - redis_client.expire(prod_key, 240) + redis_client.expire(prod_key, current_app.config["PRODUCER_TIMEOUT"]) # print out message current_app.logger.info(f"New message: {request.json['message']}") diff --git a/consumer_api/views/sync_view.py b/consumer_api/views/sync_view.py index 1b034f1..071da44 100644 --- a/consumer_api/views/sync_view.py +++ b/consumer_api/views/sync_view.py @@ -29,7 +29,7 @@ class SyncView(FlaskView): } redis_client.set(cust_key, json.dumps(info).encode('utf-8')) - redis_client.expire(cust_key, 30) + redis_client.expire(cust_key, current_app.config["CUSTOMER_TIMEOUT"]) response = { "uuid": current_app.config['LOCAL_UUID'] From 67d71fa43ec34edf8143d04f3a00fb64a624c8bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=BCleki=20F=C3=A1bi=C3=A1n?= Date: Fri, 8 May 2020 19:48:31 +0200 Subject: [PATCH 3/5] Fixed customers endpoint --- consumer_api/views/consumers_view.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/consumer_api/views/consumers_view.py b/consumer_api/views/consumers_view.py index 0582bc4..ead5288 100644 --- a/consumer_api/views/consumers_view.py +++ b/consumer_api/views/consumers_view.py @@ -8,6 +8,13 @@ from flask_classful import FlaskView class ConsumersView(FlaskView): def get(self): - consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8')) - current_app.logger.warning(jsonify([v['ip'] for k, v in consumer_list.items()])) - return jsonify([v['ip'] for k, v in consumer_list.items()]) + + keys = redis_client.keys('producer_*') + + list_of_customer_ips = [] + + for key in keys: + info = json.loads((redis_client.get(key) or b"{}").decode('utf-8')) + list_of_customer_ips.append(info['ip']) + + return jsonify(list_of_customer_ips) From d34756d9fc12e3db3093de656863a4cef88fb6e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=BCleki=20F=C3=A1bi=C3=A1n?= Date: Fri, 8 May 2020 19:56:09 +0200 Subject: [PATCH 4/5] fixed getting customer instead of producer --- consumer_api/views/consumers_view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consumer_api/views/consumers_view.py b/consumer_api/views/consumers_view.py index ead5288..097c42d 100644 --- a/consumer_api/views/consumers_view.py +++ b/consumer_api/views/consumers_view.py @@ -9,7 +9,7 @@ class ConsumersView(FlaskView): def get(self): - keys = redis_client.keys('producer_*') + keys = redis_client.keys('consumer_*') list_of_customer_ips = [] From 7fbd62659d3370fa74ddbf23d69474525777e06a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=BCleki=20F=C3=A1bi=C3=A1n?= Date: Fri, 8 May 2020 19:57:42 +0200 Subject: [PATCH 5/5] sync view fix --- consumer_api/views/sync_view.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/consumer_api/views/sync_view.py b/consumer_api/views/sync_view.py index 071da44..e0dc13a 100644 --- a/consumer_api/views/sync_view.py +++ b/consumer_api/views/sync_view.py @@ -11,9 +11,9 @@ class SyncView(FlaskView): remote_uuid = request.json['uuid'] remote_ip = request.remote_addr - cust_key = f"customer_{remote_uuid}" + cust_key = f"consumer_{remote_uuid}" - last_known_info = json.loads((redis_client.get("cust_key") or b"{}").decode('utf-8')) + last_known_info = json.loads((redis_client.get(cust_key) or b"{}").decode('utf-8')) if not last_known_info: current_app.logger.info(f"New consumer registered (unknown UUID): {remote_uuid} at {remote_ip}")