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 18:26:19 +02:00
|
|
|
import random
|
2020-03-29 16:26:16 +02:00
|
|
|
import requests
|
2020-03-29 20:00:27 +02:00
|
|
|
from consumerlocator import ConsumerLocator
|
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
|
|
|
"""
|
|
|
|
|
|
|
|
__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 17:15:59 +02:00
|
|
|
def __init__(self, consumerlocator: ConsumerLocator, uuid: str):
|
2020-03-29 20:00:27 +02:00
|
|
|
"""
|
|
|
|
Initialize object
|
|
|
|
:param consumerlocator:
|
|
|
|
:param uuid:
|
|
|
|
"""
|
|
|
|
self.consumerlocator=consumerlocator
|
|
|
|
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-29 20:00:27 +02:00
|
|
|
currentconsumer=self.consumerlocator.getcurrentconsumer()
|
|
|
|
LOGGER.debug(f"Sending message to {currentconsumer}")
|
|
|
|
requests.post(f'http://{currentconsumer}/log', json={'uuid': self.uuid, 'message': message})
|
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-03-29 20:00:27 +02:00
|
|
|
currentconsumer = self.consumerlocator.getcurrentconsumer()
|
|
|
|
response = requests.get(f'http://{currentconsumer}/consumer')
|
|
|
|
json = response.json()
|
|
|
|
LOGGER.debug(f"List of currently available consumers: {json}")
|
|
|
|
return json
|
2020-03-29 16:26:16 +02:00
|
|
|
|
|
|
|
def isconsumeravailable(self) -> bool:
|
|
|
|
"""
|
|
|
|
Readiness probe primary consumer.
|
|
|
|
:return:
|
|
|
|
"""
|
2020-03-29 20:00:27 +02:00
|
|
|
currentconsumer = self.consumerlocator.getcurrentconsumer()
|
|
|
|
response = requests.get(f'http://{currentconsumer}/consumer')
|
|
|
|
isavailable = response.status_code == 200
|
|
|
|
LOGGER.debug(f"Current consumer availability: {isavailable}")
|
|
|
|
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-29 20:00:27 +02:00
|
|
|
response = requests.get(f'http://{consumer}/consumer')
|
|
|
|
isavailable = response.status_code == 200
|
|
|
|
LOGGER.debug(f"Consumer {consumer} availability: {isavailable}")
|
|
|
|
return isavailable
|