Added force ip override setting

This commit is contained in:
2020-05-14 22:25:47 +02:00
parent 3cefcabad2
commit f01001d331
2 changed files with 84 additions and 6 deletions

View File

@@ -55,7 +55,10 @@ class ProducerCommunicator:
try:
response = self._session.post(
f"http://{ip}/ip",
json={'uuid': os.environ['LOCAL_UUID'], 'ip': newip},
json={
'uuid': os.environ['LOCAL_UUID'],
'ip': newip
},
timeout=5
)
logging.debug(f"Pushed update to {key} at {ip}. Response: {response.status_code}")
@@ -69,14 +72,16 @@ class ConsumerCommunicator:
The addresses of consumers are fetched from the `RedisSuperStorage`.
"""
def __init__(self, redis_store: RedisSuperStorage):
def __init__(self, redis_store: RedisSuperStorage, force_ip_override: bool = False):
"""
Upon creating this object. A requests session is created on order to take advantage of keep-alive connections.
:param redis_store: A `RedisSuperStorage` instance.
:param force_ip_override: Include the ip address stored in redis to the sync message (Disable the reciever ip discovery in the other consumer)
"""
self._redis_store = redis_store
self._session = requests.Session()
self._force_ip_override = force_ip_override
def targeted_snyc(self, ip: str):
"""
@@ -97,14 +102,21 @@ class ConsumerCommunicator:
Body::
{
"uuid" : "str: LOCAL_UUID"
"uuid" : "str: LOCAL_UUID",
"ip" : "str: optional: IP override"
}
:param ip: The ip address of the consumer to be synced to.
"""
message = {'uuid': os.environ['LOCAL_UUID']}
if self._force_ip_override:
message['ip'] = self._redis_store.current_ip
try:
# request synchronization
response = self._session.post(f"http://{ip}/sync", json={'uuid': os.environ['LOCAL_UUID']}, timeout=5)
response = self._session.post(f"http://{ip}/sync", json=message, timeout=5)
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:
logging.warning(f"Error while syncing to {ip}: {str(e)}")
@@ -132,15 +144,22 @@ class ConsumerCommunicator:
Body::
{
"uuid" : "str: LOCAL_UUID"
"uuid" : "str: LOCAL_UUID",
"ip" : "str: optional: IP override"
}
"""
message = {'uuid': os.environ['LOCAL_UUID']}
if self._force_ip_override:
message['ip'] = self._redis_store.current_ip
for uuid, info in self._redis_store.get_consumer_list().items():
ip = info['ip']
try:
# request synchronization
response = self._session.post(f"http://{ip}/sync", json={'uuid': os.environ['LOCAL_UUID']}, timeout=5)
response = self._session.post(f"http://{ip}/sync", json=message, timeout=5)
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:
logging.warning(f"Error while syncing to {ip}: {str(e)}")