2020-03-29 15:46:00 +02:00
|
|
|
#!/usr/bin/env python
|
2020-03-29 16:26:16 +02:00
|
|
|
|
2020-03-29 15:46:00 +02:00
|
|
|
"""
|
2020-03-29 16:53:50 +02:00
|
|
|
Communicator module
|
2020-03-29 15:46:00 +02:00
|
|
|
"""
|
|
|
|
|
2020-04-08 18:17:39 +02:00
|
|
|
import logging
|
|
|
|
import requests
|
2020-04-22 01:48:29 +02:00
|
|
|
import requests.exceptions
|
2020-04-08 18:17:39 +02:00
|
|
|
|
2020-03-29 15:46:00 +02:00
|
|
|
__author__ = "@tormakris"
|
|
|
|
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
|
|
|
__module_name__ = "messagesender"
|
2020-03-29 16:26:16 +02:00
|
|
|
__version__text__ = "1"
|
|
|
|
|
2020-03-29 20:00:27 +02:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-04-29 12:31:33 +02:00
|
|
|
|
2020-03-29 20:00:27 +02:00
|
|
|
class Communicator:
|
2020-03-29 16:26:16 +02:00
|
|
|
"""
|
|
|
|
Class handling low level communication with consumers.
|
|
|
|
"""
|
|
|
|
|
2020-03-30 19:33:09 +02:00
|
|
|
def __init__(self, currentconsumer: str, uuid: str):
|
2020-04-08 19:45:05 +02:00
|
|
|
"""**Constructor:**
|
|
|
|
Initializes the object.
|
2020-04-08 18:17:39 +02:00
|
|
|
|
2020-04-06 20:48:36 +02:00
|
|
|
:param consumerlocator: the current consumer's IP address as a string
|
|
|
|
:param uuid: string typed UUID.
|
2020-03-29 20:00:27 +02:00
|
|
|
"""
|
2020-03-30 19:33:09 +02:00
|
|
|
self.currenctconsumer=currentconsumer
|
2020-03-29 20:00:27 +02:00
|
|
|
self.uuid = uuid
|
|
|
|
|
2020-03-29 16:26:16 +02:00
|
|
|
def sendmessage(self, message: str) -> None:
|
2020-04-06 20:48:36 +02:00
|
|
|
"""Send message to the current consumer. Logs the process.
|
2020-04-08 18:17:39 +02:00
|
|
|
|
2020-04-06 20:48:36 +02:00
|
|
|
:param message: the message of type string that will be sent.
|
|
|
|
:return: None
|
2020-03-29 16:26:16 +02:00
|
|
|
"""
|
2020-03-30 19:33:09 +02:00
|
|
|
currentconsumer=self.currenctconsumer
|
2020-04-01 02:05:15 +02:00
|
|
|
LOGGER.info(f"Sending message to {currentconsumer}")
|
2020-04-22 01:48:29 +02:00
|
|
|
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é...
|
2020-03-29 16:26:16 +02:00
|
|
|
|
|
|
|
def discoveravailableconsumers(self) -> list:
|
2020-04-06 20:48:36 +02:00
|
|
|
"""Get the list of available consumer from the current primary consumer. Logs the received list.
|
2020-04-08 18:17:39 +02:00
|
|
|
|
2020-04-06 20:48:36 +02:00
|
|
|
:return: list of consumers' IP addresses
|
2020-03-29 16:26:16 +02:00
|
|
|
"""
|
2020-04-01 01:02:33 +02:00
|
|
|
try:
|
|
|
|
currentconsumer = self.currenctconsumer
|
2020-04-22 01:48:29 +02:00
|
|
|
response = requests.get(f'http://{currentconsumer}/consumers', timeout=5)
|
2020-04-01 01:02:33 +02:00
|
|
|
json = response.json()
|
2020-04-01 02:05:15 +02:00
|
|
|
LOGGER.info(f"List of currently available consumers: {json}")
|
2020-04-01 01:02:33 +02:00
|
|
|
return json
|
2020-04-01 02:15:01 +02:00
|
|
|
except Exception as e:
|
2020-04-22 03:41:35 +02:00
|
|
|
LOGGER.error("Could not query available consumer list!")
|
|
|
|
#LOGGER.exception(e)
|
2020-04-01 01:02:33 +02:00
|
|
|
return []
|
2020-03-29 16:26:16 +02:00
|
|
|
|
|
|
|
def isconsumeravailable(self) -> bool:
|
2020-04-06 20:48:36 +02:00
|
|
|
"""Readiness probe current consumer. Logs the result.
|
2020-04-08 18:17:39 +02:00
|
|
|
|
2020-04-06 20:48:36 +02:00
|
|
|
:return: True if available, False otherwise
|
2020-03-29 16:26:16 +02:00
|
|
|
"""
|
2020-03-30 19:33:09 +02:00
|
|
|
currentconsumer = self.currenctconsumer
|
|
|
|
try:
|
2020-04-22 01:48:29 +02:00
|
|
|
response = requests.get(f'http://{currentconsumer}/consumers', timeout=5)
|
2020-03-30 19:33:09 +02:00
|
|
|
isavailable = response.status_code == 200
|
|
|
|
except Exception as e:
|
2020-04-22 03:41:35 +02:00
|
|
|
#LOGGER.exception(e)
|
2020-03-30 19:33:09 +02:00
|
|
|
isavailable = False
|
2020-04-01 02:05:15 +02:00
|
|
|
LOGGER.info(f"Current consumer availability: {isavailable}")
|
2020-03-29 20:00:27 +02:00
|
|
|
return isavailable
|
2020-03-29 16:53:50 +02:00
|
|
|
|
|
|
|
def checkconsumer(self, consumer: str) -> bool:
|
2020-04-06 20:48:36 +02:00
|
|
|
"""Readiness probe of a particular consumer. Logs the result.
|
2020-04-08 18:17:39 +02:00
|
|
|
|
2020-04-06 20:48:36 +02:00
|
|
|
:param consumer: the consumer's IP address
|
|
|
|
:return: True if available, False otherwise
|
2020-03-29 16:53:50 +02:00
|
|
|
"""
|
2020-03-30 19:33:09 +02:00
|
|
|
try:
|
2020-04-22 01:48:29 +02:00
|
|
|
response = requests.get(f'http://{consumer}/consumers', timeout=5)
|
2020-03-30 19:33:09 +02:00
|
|
|
isavailable = response.status_code == 200
|
|
|
|
except Exception as e:
|
2020-04-22 03:41:35 +02:00
|
|
|
#LOGGER.exception(e)
|
2020-03-30 19:33:09 +02:00
|
|
|
isavailable = False
|
2020-04-01 02:05:15 +02:00
|
|
|
LOGGER.info(f"Consumer {consumer} availability: {isavailable}")
|
2020-03-29 20:00:27 +02:00
|
|
|
return isavailable
|
2020-03-30 19:33:09 +02:00
|
|
|
|
2020-04-06 20:48:36 +02:00
|
|
|
def set_currentconsumer(self,currenctconsumer) -> None:
|
|
|
|
"""Set current consumer
|
2020-04-08 18:17:39 +02:00
|
|
|
|
2020-04-06 20:48:36 +02:00
|
|
|
:param currenctconsumer: the consumer's IP address
|
|
|
|
:return: None
|
2020-03-30 19:33:09 +02:00
|
|
|
"""
|
|
|
|
self.currenctconsumer=currenctconsumer
|