communicator requests api implementation done
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
06671e29b8
commit
fe3537ff31
40
app.py
40
app.py
@ -1,12 +1,16 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
import uuid
|
||||||
|
import logging
|
||||||
import sentry_sdk
|
import sentry_sdk
|
||||||
import communicator
|
from communicator import Communicator
|
||||||
import consumerlocator
|
from consumerlocator import ConsumerLocator
|
||||||
import messagesender
|
from messagesender import MessageSender
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Main Flask RESTful API
|
Main entrypoint
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__author__ = "@tormakris"
|
__author__ = "@tormakris"
|
||||||
@ -16,20 +20,18 @@ __version__text__ = "1"
|
|||||||
|
|
||||||
sentry_sdk.init("https://3fa5ae886ba1489092ad49a93cb419c1@sentry.kmlabz.com/9")
|
sentry_sdk.init("https://3fa5ae886ba1489092ad49a93cb419c1@sentry.kmlabz.com/9")
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("Producer: init")
|
LOGGER.info("Producer started")
|
||||||
comm = communicator.Communicator()
|
generateduuid = str(uuid)
|
||||||
conslist = consumerlocator.ConsumerLocator()
|
LOGGER.debug(f"My uuid is {generateduuid}")
|
||||||
conslist.initCommunicator(comm)
|
consumerlocator = ConsumerLocator()
|
||||||
message = messagesender.MessageSender()
|
communicator = Communicator(consumerlocator=consumerlocator,uuid=uuid)
|
||||||
|
messagesender = MessageSender(communicator=communicator)
|
||||||
|
|
||||||
conslist.learnconsumerlist()
|
while True:
|
||||||
print("Producer: started sending")
|
LOGGER.info("Sending message to consumer")
|
||||||
for i in range(30):
|
messagesender.sendmessage()
|
||||||
print("Producer: send %i message", i)
|
time.sleep(random.random())
|
||||||
msg = message.CreateMessage(20)
|
|
||||||
if not conslist.checkcurrentconsumer():
|
|
||||||
conslist.updateconsumer()
|
|
||||||
comm.sendmessage(msg)
|
|
||||||
|
|
||||||
print("Producer: finished")
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
import logging
|
||||||
import random
|
import random
|
||||||
import requests
|
import requests
|
||||||
from singleton import Singleton
|
from consumerlocator import ConsumerLocator
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Communicator module
|
Communicator module
|
||||||
@ -13,32 +13,54 @@ __copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
|||||||
__module_name__ = "messagesender"
|
__module_name__ = "messagesender"
|
||||||
__version__text__ = "1"
|
__version__text__ = "1"
|
||||||
|
|
||||||
class Communicator(Singleton):
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
class Communicator:
|
||||||
"""
|
"""
|
||||||
Class handling low level communication with consumers.
|
Class handling low level communication with consumers.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def __init__(self, consumerlocator: ConsumerLocator, uuid):
|
||||||
|
"""
|
||||||
|
Initialize object
|
||||||
|
:param consumerlocator:
|
||||||
|
:param uuid:
|
||||||
|
"""
|
||||||
|
self.consumerlocator=consumerlocator
|
||||||
|
self.uuid = uuid
|
||||||
|
|
||||||
def sendmessage(self, message: str) -> None:
|
def sendmessage(self, message: str) -> None:
|
||||||
"""
|
"""
|
||||||
Send message to consumer.
|
Send message to consumer.
|
||||||
:param message:
|
:param message:
|
||||||
:return: none
|
:return: none
|
||||||
"""
|
"""
|
||||||
return None
|
currentconsumer=self.consumerlocator.getcurrentconsumer()
|
||||||
|
LOGGER.debug(f"Sending message to {currentconsumer}")
|
||||||
|
requests.post(f'http://{currentconsumer}/log', json={'uuid': self.uuid, 'message': message})
|
||||||
|
|
||||||
def discoveravailableconsumers(self) -> list:
|
def discoveravailableconsumers(self) -> list:
|
||||||
"""
|
"""
|
||||||
Get the list of available consumer from the current primary consumer.
|
Get the list of available consumer from the current primary consumer.
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
return ["10.69.42.1","10.42.69.1","10.10.10.10","10.6.66.1"]
|
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
|
||||||
|
|
||||||
def isconsumeravailable(self) -> bool:
|
def isconsumeravailable(self) -> bool:
|
||||||
"""
|
"""
|
||||||
Readiness probe primary consumer.
|
Readiness probe primary consumer.
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
return bool(random.getrandbits(1))
|
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
|
||||||
|
|
||||||
def checkconsumer(self, consumer: str) -> bool:
|
def checkconsumer(self, consumer: str) -> bool:
|
||||||
"""
|
"""
|
||||||
@ -46,4 +68,7 @@ class Communicator(Singleton):
|
|||||||
:param consumer:
|
:param consumer:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
return bool(random.getrandbits(1))
|
response = requests.get(f'http://{consumer}/consumer')
|
||||||
|
isavailable = response.status_code == 200
|
||||||
|
LOGGER.debug(f"Consumer {consumer} availability: {isavailable}")
|
||||||
|
return isavailable
|
||||||
|
@ -1,48 +1,46 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
import logging
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
from communicator import Communicator
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Main Flask RESTful API
|
Message sender component
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__author__ = "@tormakris"
|
__author__ = "@kovacsbence"
|
||||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||||
__module_name__ = "messagesender"
|
__module_name__ = "messagesender"
|
||||||
__version__text__ = "1"
|
__version__text__ = "1"
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class MessageSender:
|
class MessageSender:
|
||||||
"""
|
"""
|
||||||
Üzenetek küldéséért felelős komponens.
|
Üzenetek küldéséért felelős komponens.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, communicator: Communicator):
|
||||||
"""
|
"""
|
||||||
Inicializálja az osztályt.
|
Inicializálja az osztályt.
|
||||||
"""
|
"""
|
||||||
pass
|
self.communicator = communicator
|
||||||
|
|
||||||
|
def randomstring(self, stringLength) -> str:
|
||||||
def randomString(self, stringLength):
|
|
||||||
"""Generate a random string of fixed length """
|
"""Generate a random string of fixed length """
|
||||||
letters = string.ascii_lowercase
|
letters = string.ascii_lowercase
|
||||||
return ''.join(random.choice(letters) for i in range(stringLength))
|
return ''.join(random.choice(letters) for i in range(stringLength))
|
||||||
|
|
||||||
|
def sendmessage(self, message: str = "") -> None:
|
||||||
def createMessage(self, p) -> str:
|
|
||||||
"""
|
"""
|
||||||
Ez egy metodus
|
Uzenet letrehozasa
|
||||||
:param szam:
|
:param message:
|
||||||
:param szoveg:
|
|
||||||
:return: str tipus
|
:return: str tipus
|
||||||
"""
|
"""
|
||||||
data = self.randomString(p)
|
if not message:
|
||||||
|
data = self.randomstring(32)
|
||||||
return str(data)
|
else:
|
||||||
pass
|
data = message
|
||||||
|
self.communicator.sendmessage(data)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
temp = MessageSender()
|
|
||||||
print(temp.createMessage(20))
|
|
||||||
|
30
singleton.py
30
singleton.py
@ -1,30 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
|
|
||||||
"""
|
|
||||||
Singleton meta module
|
|
||||||
"""
|
|
||||||
|
|
||||||
__author__ = "@tormakris"
|
|
||||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
|
||||||
__module_name__ = "singleton"
|
|
||||||
__version__text__ = "1"
|
|
||||||
|
|
||||||
|
|
||||||
class Singleton(object):
|
|
||||||
"""
|
|
||||||
Singleton metaclass
|
|
||||||
"""
|
|
||||||
_instances = {}
|
|
||||||
|
|
||||||
def __new__(class_, *args, **kwargs):
|
|
||||||
"""
|
|
||||||
New
|
|
||||||
:param args:
|
|
||||||
:param kwargs:
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
if class_ not in class_._instances:
|
|
||||||
class_._instances[class_] = super(
|
|
||||||
Singleton, class_).__new__(
|
|
||||||
class_, *args, **kwargs)
|
|
||||||
return class_._instances[class_]
|
|
Reference in New Issue
Block a user