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