2020-03-29 15:46:00 +02:00
|
|
|
#!/usr/bin/env python
|
2020-03-29 20:00:27 +02:00
|
|
|
import logging
|
2020-03-29 16:26:16 +02:00
|
|
|
import requests
|
|
|
|
|
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
|
|
|
"""
|
|
|
|
|
|
|
|
__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__)
|
|
|
|
|
|
|
|
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-03-29 20:00:27 +02:00
|
|
|
"""
|
|
|
|
Initialize object
|
|
|
|
:param consumerlocator:
|
|
|
|
:param uuid:
|
|
|
|
"""
|
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:
|
|
|
|
"""
|
|
|
|
Send message to consumer.
|
|
|
|
:param message:
|
|
|
|
:return: none
|
|
|
|
"""
|
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}")
|
|
|
|
postresponse=requests.post(f'http://{currentconsumer}/log', json={'uuid': self.uuid, 'message': message})
|
|
|
|
LOGGER.debug(f"Message status code is:{postresponse.status_code}")
|
2020-03-29 16:26:16 +02:00
|
|
|
|
|
|
|
def discoveravailableconsumers(self) -> list:
|
|
|
|
"""
|
|
|
|
Get the list of available consumer from the current primary consumer.
|
|
|
|
:return:
|
|
|
|
"""
|
2020-04-01 01:02:33 +02:00
|
|
|
try:
|
|
|
|
currentconsumer = self.currenctconsumer
|
|
|
|
response = requests.get(f'http://{currentconsumer}/consumer')
|
|
|
|
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:
|
|
|
|
LOGGER.exception(e)
|
2020-04-01 01:02:33 +02:00
|
|
|
return []
|
2020-03-29 16:26:16 +02:00
|
|
|
|
|
|
|
def isconsumeravailable(self) -> bool:
|
|
|
|
"""
|
|
|
|
Readiness probe primary consumer.
|
|
|
|
:return:
|
|
|
|
"""
|
2020-03-30 19:33:09 +02:00
|
|
|
currentconsumer = self.currenctconsumer
|
|
|
|
try:
|
|
|
|
response = requests.get(f'http://{currentconsumer}/consumer')
|
|
|
|
isavailable = response.status_code == 200
|
|
|
|
except Exception as e:
|
|
|
|
LOGGER.exception(e)
|
|
|
|
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:
|
|
|
|
"""
|
|
|
|
Readiness probe of a prticular consumer.
|
|
|
|
:param consumer:
|
|
|
|
:return:
|
|
|
|
"""
|
2020-03-30 19:33:09 +02:00
|
|
|
try:
|
|
|
|
response = requests.get(f'http://{consumer}/consumer')
|
|
|
|
isavailable = response.status_code == 200
|
|
|
|
except Exception as e:
|
|
|
|
LOGGER.exception(e)
|
|
|
|
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
|
|
|
|
|
|
|
def set_currentconsumer(self,currenctconsumer):
|
|
|
|
"""
|
|
|
|
Set current consumer
|
|
|
|
:param currenctconsumer:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
self.currenctconsumer=currenctconsumer
|