This commit is contained in:
@ -8,11 +8,12 @@ from flask_classful import FlaskView
|
||||
class ConsumersView(FlaskView):
|
||||
|
||||
def get(self):
|
||||
# load the currently available consumer list from the redis database
|
||||
consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8'))
|
||||
keys = redis_client.keys('consumer_*')
|
||||
|
||||
# jsonify and return the list of active consumers
|
||||
if 'full' in request.args:
|
||||
return jsonify(consumer_list)
|
||||
else:
|
||||
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)
|
||||
|
@ -19,15 +19,16 @@ class LogView(FlaskView):
|
||||
if not last_known_remote_ip:
|
||||
current_app.logger.info(f"New producer {remote_uuid} at {remote_ip}")
|
||||
elif last_known_remote_ip != remote_ip:
|
||||
current_app.logger.info(f"IP address of producer {remote_uuid} have changed: {last_known_remote_ip} -> {remote_ip}")
|
||||
current_app.logger.info(
|
||||
f"IP address of producer {remote_uuid} have changed: {last_known_remote_ip} -> {remote_ip}")
|
||||
|
||||
# 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']}")
|
||||
|
||||
# return HTTP 204 - No content message
|
||||
return Response(status=204)
|
||||
return Response(status = 204)
|
||||
|
||||
|
@ -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
|
||||
@ -10,22 +11,25 @@ class SyncView(FlaskView):
|
||||
remote_uuid = request.json['uuid']
|
||||
remote_ip = request.remote_addr
|
||||
|
||||
# load the currently available consumer list from the redis database
|
||||
consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8'))
|
||||
cust_key = f"consumer_{remote_uuid}"
|
||||
|
||||
if remote_uuid not in consumer_list.keys():
|
||||
# display newly registered consumer
|
||||
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:
|
||||
if consumer_list[remote_uuid]['ip'] != remote_ip:
|
||||
# log address changes
|
||||
if last_known_info['ip'] != remote_ip:
|
||||
current_app.logger.info(f"Address of consumer {remote_uuid} changed to {remote_ip}")
|
||||
|
||||
# update consumer list redis databasse
|
||||
consumer_list.update(
|
||||
{remote_uuid: {"ip": remote_ip}}
|
||||
)
|
||||
redis_client.set("consumer_list", json.dumps(consumer_list).encode('utf-8'))
|
||||
info = {
|
||||
"uuid": remote_uuid,
|
||||
"ip": remote_ip,
|
||||
"last_seen": time.time()
|
||||
}
|
||||
|
||||
redis_client.set(cust_key, json.dumps(info).encode('utf-8'))
|
||||
redis_client.expire(cust_key, current_app.config["CUSTOMER_TIMEOUT"])
|
||||
|
||||
# return with the current UUID
|
||||
response = {
|
||||
|
Reference in New Issue
Block a user