add consumerinformation

This commit is contained in:
2020-05-14 20:09:41 +02:00
parent 20a1aa6dfa
commit 6bdcb40220
6 changed files with 142 additions and 49 deletions

View File

@@ -7,6 +7,7 @@ Communicator module
import logging
import requests
import requests.exceptions
from consumerinformation import ConsumerInformation
__author__ = "@tormakris"
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
@@ -22,15 +23,16 @@ class Communicator:
Class handling low level communication with consumers.
"""
def __init__(self, currentconsumer: str, uuid: str):
def __init__(self, currentconsumer: str, uuid: str, consumerinformation: ConsumerInformation):
"""**Constructor:**
Initializes the object.
:param consumerlocator: the current consumer's IP address as a string
:param currentconsumer: the current consumer's IP address as a string
:param uuid: string typed UUID.
"""
self.currenctconsumer=currentconsumer
self.currenctconsumer = currentconsumer
self.uuid = uuid
self.consumerinformation = consumerinformation
def sendmessage(self, message: str) -> None:
"""Send message to the current consumer. Logs the process.
@@ -38,13 +40,18 @@ class Communicator:
:param message: the message of type string that will be sent.
:return: None
"""
currentconsumer=self.currenctconsumer
currentconsumer = self.currenctconsumer
LOGGER.info(f"Sending message to {currentconsumer}")
try:
postresponse=requests.post(f'http://{currentconsumer}/log', json={'uuid': self.uuid, 'message': message}, timeout=5)
LOGGER.debug(f"Message status code is:{postresponse.status_code}")
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e:
LOGGER.exception(e) # Fun fact: ez azt jelenti, hogy elveszett az üzenet... ide valami retry kellene inkább más consumerek felé...
for consumer in self.consumerinformation.getconsumerlist():
try:
postresponse = requests.post(f'http://{consumer}/log', json={'uuid': self.uuid, 'message': message},
timeout=5)
LOGGER.debug(f"Message status code is:{postresponse.status_code}")
if postresponse.status_code < 300:
return None
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e:
LOGGER.warning(f"Could not send message to {consumer}")
def discoveravailableconsumers(self) -> list:
"""Get the list of available consumer from the current primary consumer. Logs the received list.
@@ -58,8 +65,8 @@ class Communicator:
LOGGER.info(f"List of currently available consumers: {json}")
return json
except Exception as e:
LOGGER.error("Could not query available consumer list!")
#LOGGER.exception(e)
LOGGER.warning("Could not query available consumer list!")
# LOGGER.exception(e)
return []
def isconsumeravailable(self) -> bool:
@@ -72,7 +79,7 @@ class Communicator:
response = requests.get(f'http://{currentconsumer}/consumers', timeout=5)
isavailable = response.status_code == 200
except Exception as e:
#LOGGER.exception(e)
# LOGGER.exception(e)
isavailable = False
LOGGER.info(f"Current consumer availability: {isavailable}")
return isavailable
@@ -87,15 +94,15 @@ class Communicator:
response = requests.get(f'http://{consumer}/consumers', timeout=5)
isavailable = response.status_code == 200
except Exception as e:
#LOGGER.exception(e)
# LOGGER.exception(e)
isavailable = False
LOGGER.info(f"Consumer {consumer} availability: {isavailable}")
return isavailable
def set_currentconsumer(self,currenctconsumer) -> None:
def set_currentconsumer(self, currenctconsumer) -> None:
"""Set current consumer
:param currenctconsumer: the consumer's IP address
:return: None
"""
self.currenctconsumer=currenctconsumer
self.currenctconsumer = currenctconsumer