docs #6
14
app.py
14
app.py
@ -1,5 +1,9 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
"""
|
||||||
|
Main entry point, This module builds the producer from the submodules.
|
||||||
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import uuid
|
import uuid
|
||||||
@ -10,10 +14,6 @@ from communicator import Communicator
|
|||||||
from consumerlocator import ConsumerLocator
|
from consumerlocator import ConsumerLocator
|
||||||
from messagesender import MessageSender
|
from messagesender import MessageSender
|
||||||
|
|
||||||
"""
|
|
||||||
Main entry point, This module builds the producer from the submodules.
|
|
||||||
"""
|
|
||||||
|
|
||||||
__author__ = "@tormakris"
|
__author__ = "@tormakris"
|
||||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||||
__module_name__ = "app"
|
__module_name__ = "app"
|
||||||
@ -29,12 +29,12 @@ LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1')
|
KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1')
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
"""
|
"""
|
||||||
This is the producers entry point, initializes all the components (:class:'communicator.Communicator',
|
This is the producers entry point, initializes all the components (:class:`communicator.Communicator`,
|
||||||
:class:'consumerlocator.ConsumerLocator' and :class:'messagesender.MessageSender') and sends infinite random
|
:class:`consumerlocator.ConsumerLocator` and :class:`messagesender.MessageSender`) and sends infinite random
|
||||||
messages.
|
messages.
|
||||||
"""
|
"""
|
||||||
if __name__ == "__main__":
|
|
||||||
LOGGER.info("Producer started")
|
LOGGER.info("Producer started")
|
||||||
generateduuid = str(uuid.uuid4())
|
generateduuid = str(uuid.uuid4())
|
||||||
communicator = Communicator(currentconsumer=KNOWNCONSUMER, uuid=generateduuid)
|
communicator = Communicator(currentconsumer=KNOWNCONSUMER, uuid=generateduuid)
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import logging
|
|
||||||
import requests
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Communicator module
|
Communicator module
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import requests
|
||||||
|
|
||||||
__author__ = "@tormakris"
|
__author__ = "@tormakris"
|
||||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||||
__module_name__ = "messagesender"
|
__module_name__ = "messagesender"
|
||||||
@ -21,6 +22,7 @@ class Communicator:
|
|||||||
|
|
||||||
def __init__(self, currentconsumer: str, uuid: str):
|
def __init__(self, currentconsumer: str, uuid: str):
|
||||||
"""Initialize object
|
"""Initialize object
|
||||||
|
|
||||||
:param consumerlocator: the current consumer's IP address as a string
|
:param consumerlocator: the current consumer's IP address as a string
|
||||||
:param uuid: string typed UUID.
|
:param uuid: string typed UUID.
|
||||||
"""
|
"""
|
||||||
@ -29,6 +31,7 @@ class Communicator:
|
|||||||
|
|
||||||
def sendmessage(self, message: str) -> None:
|
def sendmessage(self, message: str) -> None:
|
||||||
"""Send message to the current consumer. Logs the process.
|
"""Send message to the current consumer. Logs the process.
|
||||||
|
|
||||||
:param message: the message of type string that will be sent.
|
:param message: the message of type string that will be sent.
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
@ -39,6 +42,7 @@ class Communicator:
|
|||||||
|
|
||||||
def discoveravailableconsumers(self) -> list:
|
def discoveravailableconsumers(self) -> list:
|
||||||
"""Get the list of available consumer from the current primary consumer. Logs the received list.
|
"""Get the list of available consumer from the current primary consumer. Logs the received list.
|
||||||
|
|
||||||
:return: list of consumers' IP addresses
|
:return: list of consumers' IP addresses
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
@ -53,6 +57,7 @@ class Communicator:
|
|||||||
|
|
||||||
def isconsumeravailable(self) -> bool:
|
def isconsumeravailable(self) -> bool:
|
||||||
"""Readiness probe current consumer. Logs the result.
|
"""Readiness probe current consumer. Logs the result.
|
||||||
|
|
||||||
:return: True if available, False otherwise
|
:return: True if available, False otherwise
|
||||||
"""
|
"""
|
||||||
currentconsumer = self.currenctconsumer
|
currentconsumer = self.currenctconsumer
|
||||||
@ -67,6 +72,7 @@ class Communicator:
|
|||||||
|
|
||||||
def checkconsumer(self, consumer: str) -> bool:
|
def checkconsumer(self, consumer: str) -> bool:
|
||||||
"""Readiness probe of a particular consumer. Logs the result.
|
"""Readiness probe of a particular consumer. Logs the result.
|
||||||
|
|
||||||
:param consumer: the consumer's IP address
|
:param consumer: the consumer's IP address
|
||||||
:return: True if available, False otherwise
|
:return: True if available, False otherwise
|
||||||
"""
|
"""
|
||||||
@ -81,6 +87,7 @@ class Communicator:
|
|||||||
|
|
||||||
def set_currentconsumer(self,currenctconsumer) -> None:
|
def set_currentconsumer(self,currenctconsumer) -> None:
|
||||||
"""Set current consumer
|
"""Set current consumer
|
||||||
|
|
||||||
:param currenctconsumer: the consumer's IP address
|
:param currenctconsumer: the consumer's IP address
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
"""
|
||||||
|
Consumer locator module, that manages the list of consumers.
|
||||||
|
"""
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
from communicator import Communicator
|
from communicator import Communicator
|
||||||
import os
|
import os
|
||||||
|
|
||||||
"""
|
|
||||||
Consumer locator module, that manages the list of consumers.
|
|
||||||
"""
|
|
||||||
|
|
||||||
__author__ = "@dscharnitzky"
|
__author__ = "@dscharnitzky"
|
||||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||||
@ -18,14 +19,16 @@ KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1')
|
|||||||
class ConsumerLocator:
|
class ConsumerLocator:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Component responsible for managing the list of consumers. Requires an instance of :class:'communicator.Communicator'
|
Component responsible for managing the list of consumers. Requires an instance of :class:`communicator.Communicator`
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, uuid: str, communicator: Communicator):
|
def __init__(self, uuid: str, communicator: Communicator):
|
||||||
"""Initializes the object.
|
"""Initializes the object.
|
||||||
|
|
||||||
Gets the known consumer's IP address from the PRODUCER_KNOWNCONSUMER envar.
|
Gets the known consumer's IP address from the PRODUCER_KNOWNCONSUMER envar.
|
||||||
:param: uuid: Not used
|
|
||||||
:param: communicator: the :class:'communicator.Communicator' instance that will be used for the low level
|
:param str uuid: Not used
|
||||||
|
:param communicator: the :class:'communicator.Communicator' instance that will be used for the low level
|
||||||
communication.
|
communication.
|
||||||
"""
|
"""
|
||||||
self.consumerlist = [{"Host": KNOWNCONSUMER, "State": True, "LastOk": datetime.datetime.now()}]
|
self.consumerlist = [{"Host": KNOWNCONSUMER, "State": True, "LastOk": datetime.datetime.now()}]
|
||||||
@ -34,9 +37,11 @@ class ConsumerLocator:
|
|||||||
|
|
||||||
def learnconsumerlist(self) -> None:
|
def learnconsumerlist(self) -> None:
|
||||||
""""Learns the list of consumers from the current consumer.
|
""""Learns the list of consumers from the current consumer.
|
||||||
Calls :func:'~communicator.Communicator.didiscoveravailableconsumers', adds the learned consumers to the list
|
|
||||||
if are not present, and then calls :func:'~consumerlocator.ConsumerLocator.updateconsumerlist'
|
Calls :func:`communicator.Communicator.didiscoveravailableconsumers`, adds the learned consumers to the list
|
||||||
:return: None
|
if are not present, and then calls :func:`consumerlocator.ConsumerLocator.updateconsumerlist`
|
||||||
|
|
||||||
|
:returns: None
|
||||||
"""
|
"""
|
||||||
recievedconsumerlist = self.communicator.discoveravailableconsumers()
|
recievedconsumerlist = self.communicator.discoveravailableconsumers()
|
||||||
if recievedconsumerlist is None:
|
if recievedconsumerlist is None:
|
||||||
@ -54,8 +59,10 @@ class ConsumerLocator:
|
|||||||
|
|
||||||
def updateconsumerlist(self) -> None:
|
def updateconsumerlist(self) -> None:
|
||||||
""" Updates the consumer list based on their availability.
|
""" Updates the consumer list based on their availability.
|
||||||
|
|
||||||
Marks for each consumer if they are available or not. If a consumer is not available for some time (1 hour),
|
Marks for each consumer if they are available or not. If a consumer is not available for some time (1 hour),
|
||||||
the it will be deleted from the list.
|
the it will be deleted from the list.
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
removelist = []
|
removelist = []
|
||||||
@ -72,9 +79,11 @@ class ConsumerLocator:
|
|||||||
|
|
||||||
def updateconsumer(self):
|
def updateconsumer(self):
|
||||||
"""If the current consumer is not available, checks all the consumers in the list and updates the current one.
|
"""If the current consumer is not available, checks all the consumers in the list and updates the current one.
|
||||||
Calls :func:'~consumerlocator.ConsumerLocator.checkcurrentconsumer' and if needed
|
|
||||||
:func:'~consumerlocator.ConsumerLocator.updateconsumerlist'. Sets the :class:'communicator.Communicator'
|
Calls :func:`consumerlocator.ConsumerLocator.checkcurrentconsumer` and if needed
|
||||||
current instance with :func:'~communicator.Communicator.set_currentconsumer'.
|
:func:`consumerlocator.ConsumerLocator.updateconsumerlist`. Sets the :class:`communicator.Communicator`
|
||||||
|
current instance with :func:`communicator.Communicator.set_currentconsumer`.
|
||||||
|
|
||||||
:return: the current consumer or None if there are no available customers at the moment.
|
:return: the current consumer or None if there are no available customers at the moment.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -98,15 +107,15 @@ class ConsumerLocator:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def getcurrentconsumer(self) -> str:
|
def getcurrentconsumer(self) -> str:
|
||||||
"""
|
"""Returns the currently selected consumer's IP address.
|
||||||
Returns the currently selected consumer's IP address.
|
|
||||||
:return: the current consumer
|
:return: the current consumer
|
||||||
"""
|
"""
|
||||||
return self.currentconsumer["Host"]
|
return self.currentconsumer["Host"]
|
||||||
|
|
||||||
def checkcurrentconsumer(self) -> bool:
|
def checkcurrentconsumer(self) -> bool:
|
||||||
"""
|
"""Check the current consumer's health.
|
||||||
Check the current consumer's health.
|
|
||||||
:return: True if OK, False if fail
|
:return: True if OK, False if fail
|
||||||
"""
|
"""
|
||||||
if self.currentconsumer is None:
|
if self.currentconsumer is None:
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
"""
|
||||||
|
Message sender component.
|
||||||
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
from communicator import Communicator
|
from communicator import Communicator
|
||||||
|
|
||||||
"""
|
|
||||||
Message sender component
|
|
||||||
"""
|
|
||||||
|
|
||||||
__author__ = "@kovacsbence"
|
__author__ = "@kovacsbence"
|
||||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||||
__module_name__ = "messagesender"
|
__module_name__ = "messagesender"
|
||||||
@ -19,18 +20,19 @@ LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
class MessageSender:
|
class MessageSender:
|
||||||
"""
|
"""
|
||||||
Component responsible for sending the messages. Requires an instance of :class:'communicator.Communicator'
|
Component responsible for sending the messages. Requires an instance of :class:`communicator.Communicator`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, communicator: Communicator):
|
def __init__(self, communicator: Communicator):
|
||||||
"""Initializes the object.
|
"""Initializes the object.
|
||||||
:param: communicator: an instance of :class:'communicator.Communicator'
|
|
||||||
|
:param communicator: an instance of :class:`communicator.Communicator`.
|
||||||
"""
|
"""
|
||||||
self.communicator = communicator
|
self.communicator = communicator
|
||||||
|
|
||||||
def randomstring(self, stringlength: int) -> str:
|
def randomstring(self, stringlength: int) -> str:
|
||||||
"""
|
"""Generate a random string of fixed length
|
||||||
Generate a random string of fixed length
|
|
||||||
:param stringlength: the length of the string
|
:param stringlength: the length of the string
|
||||||
:return: the generated string
|
:return: the generated string
|
||||||
"""
|
"""
|
||||||
@ -39,10 +41,12 @@ class MessageSender:
|
|||||||
|
|
||||||
def sendmessage(self, message: str = "") -> None:
|
def sendmessage(self, message: str = "") -> None:
|
||||||
"""Sends the given message.
|
"""Sends the given message.
|
||||||
|
|
||||||
If the message is omitted (empty), then a random message will be generated with length 23 (with
|
If the message is omitted (empty), then a random message will be generated with length 23 (with
|
||||||
:func:'~messagesender.MessageSender.randomstring'. Calls :func:'~communicator.Communicator.sendmessage'
|
:func:`messagesender.MessageSender.randomstring`. Calls :func:`communicator.Communicator.sendmessage`
|
||||||
to send the message.
|
to send the message.
|
||||||
:param message: the message of type string that will be sent
|
|
||||||
|
:param message: the message of type string that will be sent.
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
if not message:
|
if not message:
|
||||||
|
49
test.py
49
test.py
@ -1,13 +1,14 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
"""
|
||||||
|
Unit tests for the producer module.
|
||||||
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import consumerlocator
|
import consumerlocator
|
||||||
import communicator
|
import communicator
|
||||||
import messagesender
|
import messagesender
|
||||||
|
|
||||||
"""
|
|
||||||
Unit tests for producer module.
|
|
||||||
"""
|
|
||||||
|
|
||||||
__author__ = "@tormakris"
|
__author__ = "@tormakris"
|
||||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||||
@ -19,8 +20,9 @@ generateduuid = 'c959ad81-58f9-4445-aab4-8f3d68aee1ad'
|
|||||||
|
|
||||||
def test_generate_string(mocker):
|
def test_generate_string(mocker):
|
||||||
"""
|
"""
|
||||||
Tests :func:'~messagesender.MessageSender.randomstring'.
|
Tests :func:`messagesender.MessageSender.randomstring`.
|
||||||
:param: mocker:
|
|
||||||
|
:param mocker: patches the :class:`communicator.Communicator`.
|
||||||
"""
|
"""
|
||||||
mocker.patch('communicator.Communicator')
|
mocker.patch('communicator.Communicator')
|
||||||
comm = communicator.Communicator(
|
comm = communicator.Communicator(
|
||||||
@ -34,7 +36,8 @@ def test_generate_string(mocker):
|
|||||||
|
|
||||||
def test_sendmessage(httpserver):
|
def test_sendmessage(httpserver):
|
||||||
"""
|
"""
|
||||||
Tests :func:'~communicator.Communicator.sendmessage'.
|
Tests :func:`communicator.Communicator.sendmessage`.
|
||||||
|
|
||||||
:param httpserver: simple HTTP server
|
:param httpserver: simple HTTP server
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
@ -56,8 +59,9 @@ def test_sendmessage(httpserver):
|
|||||||
|
|
||||||
def test_send_message(mocker):
|
def test_send_message(mocker):
|
||||||
"""
|
"""
|
||||||
Tests :func:'~messagesender.MessageSender.sendmessage'.
|
Tests :func:`messagesender.MessageSender.sendmessage`.
|
||||||
:param mocker:
|
|
||||||
|
:param mocker: patches the :class:`communicator.Communicator`.
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
mocker.patch('communicator.Communicator')
|
mocker.patch('communicator.Communicator')
|
||||||
@ -72,7 +76,8 @@ def test_send_message(mocker):
|
|||||||
|
|
||||||
def test_discoveravailableconsumers(httpserver):
|
def test_discoveravailableconsumers(httpserver):
|
||||||
"""
|
"""
|
||||||
Tests :func:'~communicator.Communicator.discoveravailableconsumers'
|
Tests :func:`communicator.Communicator.discoveravailableconsumers`
|
||||||
|
|
||||||
:param httpserver: simple HTTP server
|
:param httpserver: simple HTTP server
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
@ -93,7 +98,8 @@ def test_discoveravailableconsumers(httpserver):
|
|||||||
|
|
||||||
def test_isconsumeravailable(httpserver):
|
def test_isconsumeravailable(httpserver):
|
||||||
"""
|
"""
|
||||||
Tests :func:'~communicator.Communicator.isconsumeravailable'
|
Tests :func:`communicator.Communicator.isconsumeravailable`.
|
||||||
|
|
||||||
:param httpserver: simple HTTP server
|
:param httpserver: simple HTTP server
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
@ -126,7 +132,8 @@ def test_isconsumeravailable(httpserver):
|
|||||||
|
|
||||||
def test_checkconsumer(httpserver):
|
def test_checkconsumer(httpserver):
|
||||||
"""
|
"""
|
||||||
Tests :func:'~communicator.Communicator.checkconsumer'
|
Tests :func:`communicator.Communicator.checkconsumer`.
|
||||||
|
|
||||||
:param httpserver: simple HTTP server
|
:param httpserver: simple HTTP server
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
@ -159,7 +166,8 @@ def test_checkconsumer(httpserver):
|
|||||||
|
|
||||||
def test_setcurrentconsumer():
|
def test_setcurrentconsumer():
|
||||||
"""
|
"""
|
||||||
Tests :func:'~communicator.Communicator.set_currentconsumer'
|
Tests :func:`communicator.Communicator.set_currentconsumer`
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
comm = communicator.Communicator(
|
comm = communicator.Communicator(
|
||||||
@ -171,7 +179,8 @@ def test_setcurrentconsumer():
|
|||||||
|
|
||||||
def test_learnconsumerlist(httpserver):
|
def test_learnconsumerlist(httpserver):
|
||||||
"""
|
"""
|
||||||
Tests :func:'~consumerlocator.ConsumerLocator.learnconsumerlist'
|
Tests :func:`consumerlocator.ConsumerLocator.learnconsumerlist`
|
||||||
|
|
||||||
:param httpserver: simple HTTP server
|
:param httpserver: simple HTTP server
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
@ -194,8 +203,9 @@ def test_learnconsumerlist(httpserver):
|
|||||||
|
|
||||||
def test_getcurrentconsumer(mocker):
|
def test_getcurrentconsumer(mocker):
|
||||||
"""
|
"""
|
||||||
Tests :func:'~consumerlocator.ConsumerLocator.getcurrentconsumer'
|
Tests :func:`consumerlocator.ConsumerLocator.getcurrentconsumer`
|
||||||
:param mocker:
|
|
||||||
|
:param mocker: patches the :class:`communicator.Communicator`.
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
mocker.patch('communicator.Communicator')
|
mocker.patch('communicator.Communicator')
|
||||||
@ -209,7 +219,8 @@ def test_getcurrentconsumer(mocker):
|
|||||||
|
|
||||||
def test_checkcurrentconsumer(httpserver):
|
def test_checkcurrentconsumer(httpserver):
|
||||||
"""
|
"""
|
||||||
Tests :func:'~consumerlocator.ConsumerLocator.checkcurrentconsumer'
|
Tests :func:`consumerlocator.ConsumerLocator.checkcurrentconsumer`
|
||||||
|
|
||||||
:param httpserver: simple HTTP server
|
:param httpserver: simple HTTP server
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
@ -232,7 +243,8 @@ def test_checkcurrentconsumer(httpserver):
|
|||||||
|
|
||||||
def test_updateconsumer(httpserver):
|
def test_updateconsumer(httpserver):
|
||||||
"""
|
"""
|
||||||
Tests :func:'~consumerlocator.ConsumerLocator.updateconsumer'
|
Tests :func:`consumerlocator.ConsumerLocator.updateconsumer`
|
||||||
|
|
||||||
:param httpserver: simple HTTP server
|
:param httpserver: simple HTTP server
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
@ -256,7 +268,8 @@ def test_updateconsumer(httpserver):
|
|||||||
|
|
||||||
def test_updateconsumerlist(httpserver):
|
def test_updateconsumerlist(httpserver):
|
||||||
"""
|
"""
|
||||||
Tests :func:'~consumerlocator.ConsumerLocator.updateconsumerlist'
|
Tests :func:`consumerlocator.ConsumerLocator.updateconsumerlist`
|
||||||
|
|
||||||
:param httpserver: simple HTTP server
|
:param httpserver: simple HTTP server
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
Reference in New Issue
Block a user