From 5f2894e727e3086d9187e88aa6f83cc7e8698836 Mon Sep 17 00:00:00 2001 From: marcsello Date: Wed, 22 Apr 2020 01:35:29 +0200 Subject: [PATCH] IP is now saved to redis --- app.py | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/app.py b/app.py index 81d8cc6..2ec42de 100644 --- a/app.py +++ b/app.py @@ -26,7 +26,7 @@ def main(): logging.basicConfig(filename='', level=logging.DEBUG) # connect to redis - r = redis.from_url(os.get('REDIS_URL', "redis://localhost:6379/0")) + r = redis.from_url(os.environ.get('REDIS_URL', "redis://localhost:6379/0")) # set initial consumer addresses ip_list = os.environ['INITIAL_SERVERS'].split(',') @@ -37,13 +37,13 @@ def main(): temp_dict = {} host_name = socket.gethostname() - current_ip = socket.gethostbyname(host_name) for ip in ip_list: try: # request synchronization - response = requests.post(f"http://{ip}/sync", json={'uuid': os.environ['LOCAL_UUID']}) - except requests.exceptions.ConnectionError: + response = requests.post(f"http://{ip}/sync", json={'uuid': os.environ['LOCAL_UUID']}, timeout=5) + except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e: + logging.error(f"Error while syncing to {ip}: {str(e)}") continue if response.status_code == 200: @@ -61,8 +61,9 @@ def main(): ip = info['ip'] try: # request synchronization - response = requests.post(f"http://{ip}/sync", json={'uuid': os.environ['LOCAL_UUID']}) - except requests.exceptions.ConnectionError: + response = requests.post(f"http://{ip}/sync", json={'uuid': os.environ['LOCAL_UUID']}, timeout=5) + except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e: + logging.error(f"Error while syncing to {ip}: {str(e)}") continue if response.status_code == 200: @@ -73,12 +74,18 @@ def main(): r.set('consumer_list', json.dumps(consumer_list_redis).encode('utf-8')) logging.debug('Update redis consumer ip list from answers: Done') - logging.debug('Waiting for next turn') - # wait for the next update time + # Test ip change stuff + + current_ip = r.get('current_ip') + + if not current_ip: # Not set yet. I this case no update required + current_ip = socket.gethostbyname(host_name) if current_ip != socket.gethostbyname(host_name): - logging.debug('IP changed. Pushing updates...') + logging.info('IP changed. Pushing updates...') current_ip = socket.gethostbyname(host_name) + r.set('current_ip', current_ip.encode('utf-8')) + # pushing update... keys = r.keys('producer_*') logging.debug(f'Pushing update to the following producers: ' + ', '.join(k.decode('utf-8') for k in keys)) @@ -91,16 +98,20 @@ def main(): continue try: - response = requests.post(f"http://{ip}/ip", - json={'uuid': os.environ['LOCAL_UUID'], 'ip': current_ip}) + response = requests.post( + f"http://{ip}/ip", + json={'uuid': os.environ['LOCAL_UUID'], 'ip': current_ip}, + timeout=5 + ) logging.debug(f"Pushed update to {key.decode('utf-8')} at {ip}. Response: {response.status_code}") - except requests.exceptions.ConnectionError as e: + except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e: logging.warning(f"Could not push update to {key.decode('utf-8')}: {str(e)}") continue else: logging.debug('IP unchanged.') - time.sleep(30) + logging.debug('Waiting for next turn') + time.sleep(os.environ.get("RUN_INTERVAL", 30)) if __name__ == "__main__":