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

92 lines
2.6 KiB
Python
Raw Normal View History

2020-03-29 15:46:00 +02:00
#!/usr/bin/env python
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"
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):
"""
Initialize object
:param consumerlocator:
:param uuid:
"""
2020-03-30 19:33:09 +02:00
self.currenctconsumer=currentconsumer
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
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:
"""
try:
currentconsumer = self.currenctconsumer
response = requests.get(f'http://{currentconsumer}/consumer')
json = response.json()
LOGGER.debug(f"List of currently available consumers: {json}")
return json
except Exception:
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
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-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
LOGGER.debug(f"Consumer {consumer} availability: {isavailable}")
return isavailable
2020-03-30 19:33:09 +02:00
def set_currentconsumer(self,currenctconsumer):
"""
Set current consumer
:param currenctconsumer:
:return:
"""
self.currenctconsumer=currenctconsumer