Merge pull request 'dev-consumer-timeout-fix' (#7) from dev-consumer-timeout-fix into dev
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Füleki Fábián 2020-05-08 20:05:33 +02:00
commit 2e8bc421c6
4 changed files with 28 additions and 13 deletions

View File

@ -19,6 +19,8 @@ sentry_sdk.init("https://0a106e104e114bc9a3fa47f9cb0db2f4@sentry.kmlabz.com/10")
app = Flask(__name__) app = Flask(__name__)
app.config['REDIS_URL'] = os.environ['REDIS_URL'] app.config['REDIS_URL'] = os.environ['REDIS_URL']
app.config['LOCAL_UUID'] = os.environ['LOCAL_UUID'] 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) redis_client.init_app(app)

View File

@ -8,6 +8,13 @@ from flask_classful import FlaskView
class ConsumersView(FlaskView): class ConsumersView(FlaskView):
def get(self): 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()])) keys = redis_client.keys('consumer_*')
return jsonify([v['ip'] for k, v in consumer_list.items()])
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)

View File

@ -23,7 +23,7 @@ class LogView(FlaskView):
# update expirity # update expirity
redis_client.set(prod_key, remote_ip.encode('utf-8')) 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 # print out message
current_app.logger.info(f"New message: {request.json['message']}") current_app.logger.info(f"New message: {request.json['message']}")

View File

@ -1,4 +1,5 @@
import json import json
import time
from flask import request, current_app, jsonify from flask import request, current_app, jsonify
from flask_classful import FlaskView from flask_classful import FlaskView
from db import redis_client # ez nagyon otvar from db import redis_client # ez nagyon otvar
@ -10,20 +11,25 @@ class SyncView(FlaskView):
remote_uuid = request.json['uuid'] remote_uuid = request.json['uuid']
remote_ip = request.remote_addr remote_ip = request.remote_addr
consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8')) cust_key = f"consumer_{remote_uuid}"
# Log something about it last_known_info = json.loads((redis_client.get(cust_key) or b"{}").decode('utf-8'))
if remote_uuid not in consumer_list.keys():
if not last_known_info:
current_app.logger.info(f"New consumer registered (unknown UUID): {remote_uuid} at {remote_ip}") 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}") current_app.logger.info(f"Address of consumer {remote_uuid} changed to {remote_ip}")
consumer_list.update( info = {
{remote_uuid: {"ip": remote_ip}} "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, current_app.config["CUSTOMER_TIMEOUT"])
response = { response = {
"uuid": current_app.config['LOCAL_UUID'] "uuid": current_app.config['LOCAL_UUID']