This repository has been archived on 2020-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
producer/communicator.py

109 lines
3.8 KiB
Python
Raw Normal View History

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
"""
import logging
import requests
2020-04-22 01:48:29 +02:00
import requests.exceptions
2020-05-14 20:09:41 +02:00
from consumerinformation import ConsumerInformation
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"
logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__)
2020-04-29 12:31:33 +02:00
class Communicator:
2020-03-29 16:26:16 +02:00
"""
Class handling low level communication with consumers.
"""
2020-05-14 20:09:41 +02:00
def __init__(self, currentconsumer: str, uuid: str, consumerinformation: ConsumerInformation):
2020-04-08 19:45:05 +02:00
"""**Constructor:**
Initializes the object.
2020-05-14 20:09:41 +02:00
:param currentconsumer: the current consumer's IP address as a string
2020-04-06 20:48:36 +02:00
:param uuid: string typed UUID.
"""
2020-05-14 20:09:41 +02:00
self.currenctconsumer = currentconsumer
self.uuid = uuid
2020-05-14 20:09:41 +02:00
self.consumerinformation = consumerinformation
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-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-05-14 20:09:41 +02:00
currentconsumer = self.currenctconsumer
2020-04-01 02:05:15 +02:00
LOGGER.info(f"Sending message to {currentconsumer}")
2020-05-14 20:09:41 +02:00
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}")
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-06 20:48:36 +02:00
:return: list of consumers' IP addresses
2020-03-29 16:26:16 +02:00
"""
try:
currentconsumer = self.currenctconsumer
2020-04-22 01:48:29 +02:00
response = requests.get(f'http://{currentconsumer}/consumers', timeout=5)
json = response.json()
2020-04-01 02:05:15 +02:00
LOGGER.info(f"List of currently available consumers: {json}")
return json
2020-04-01 02:15:01 +02:00
except Exception as e:
2020-05-14 20:09:41 +02:00
LOGGER.warning("Could not query available consumer list!")
# LOGGER.exception(e)
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-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-05-14 20:09:41 +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}")
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-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-05-14 20:09:41 +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}")
return isavailable
2020-03-30 19:33:09 +02:00
2020-05-14 20:09:41 +02:00
def set_currentconsumer(self, currenctconsumer) -> None:
2020-04-06 20:48:36 +02:00
"""Set current consumer
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
"""
2020-05-14 20:09:41 +02:00
self.currenctconsumer = currenctconsumer