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] 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']