Merge pull request 'docs' (#6) from docs into master
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:
commit
e910d91f06
@ -39,10 +39,9 @@ steps:
|
||||
- name: make_docs
|
||||
image: python:3.8
|
||||
commands:
|
||||
- pip3 install Sphinx
|
||||
- pip3 install Sphinx sphinx_rtd_theme
|
||||
- pip3 install -r requirements.txt
|
||||
- cd docs
|
||||
- sphinx-apidoc -o source/ ../
|
||||
- make html
|
||||
|
||||
- name: build_docs
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -131,4 +131,3 @@ dmypy.json
|
||||
#Pycharm
|
||||
.idea/
|
||||
*.iml
|
||||
docs/source/
|
||||
|
7
README.rst
Normal file
7
README.rst
Normal file
@ -0,0 +1,7 @@
|
||||
============
|
||||
P2P Producer
|
||||
============
|
||||
|
||||
Produced by GoldenPogácsa Inc.
|
||||
|
||||
TODO
|
18
app.py
18
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')
|
||||
|
||||
"""
|
||||
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__":
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
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"
|
||||
@ -20,7 +21,9 @@ class Communicator:
|
||||
"""
|
||||
|
||||
def __init__(self, currentconsumer: str, uuid: str):
|
||||
"""Initialize object
|
||||
"""**Constructor:**
|
||||
Initializes the object.
|
||||
|
||||
:param consumerlocator: the current consumer's IP address as a string
|
||||
:param uuid: string typed UUID.
|
||||
"""
|
||||
@ -29,6 +32,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 +43,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 +58,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 +73,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 +88,7 @@ class Communicator:
|
||||
|
||||
def set_currentconsumer(self,currenctconsumer) -> None:
|
||||
"""Set current consumer
|
||||
|
||||
:param currenctconsumer: the consumer's IP address
|
||||
:return: None
|
||||
"""
|
||||
|
@ -1,13 +1,13 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import datetime
|
||||
from communicator import Communicator
|
||||
import os
|
||||
|
||||
"""
|
||||
Consumer locator module, that manages the list of consumers.
|
||||
"""
|
||||
|
||||
import datetime
|
||||
from communicator import Communicator
|
||||
import os
|
||||
|
||||
__author__ = "@dscharnitzky"
|
||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||
__module_name__ = "consumerlocator"
|
||||
@ -18,15 +18,17 @@ 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.
|
||||
"""**Constructor:**
|
||||
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
|
||||
communication.
|
||||
|
||||
:param 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()}]
|
||||
self.currentconsumer = self.consumerlist[0]
|
||||
@ -34,9 +36,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 +58,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 +78,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 +106,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:
|
||||
|
@ -44,7 +44,9 @@ exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
|
||||
# The theme to use for HTML and HTML Help pages. See the documentation for
|
||||
# a list of builtin themes.
|
||||
#
|
||||
html_theme = 'alabaster'
|
||||
html_theme = 'sphinx_rtd_theme'
|
||||
|
||||
autoclass_content = 'both'
|
||||
|
||||
# Add any paths that contain custom static files (such as style sheets) here,
|
||||
# relative to this directory. They are copied after the builtin static files,
|
||||
|
@ -10,6 +10,8 @@ Welcome to Producer's documentation!
|
||||
:maxdepth: 2
|
||||
:caption: Contents:
|
||||
|
||||
readme
|
||||
source/modules
|
||||
|
||||
|
||||
Indices and tables
|
||||
@ -18,3 +20,5 @@ Indices and tables
|
||||
* :ref:`genindex`
|
||||
* :ref:`modindex`
|
||||
* :ref:`search`
|
||||
* `Git repository <https://git.kmlabz.com/GoldenPogacsa/producer>`_
|
||||
|
||||
|
2
docs/readme.rst
Normal file
2
docs/readme.rst
Normal file
@ -0,0 +1,2 @@
|
||||
.. include:: ../README.rst
|
||||
|
7
docs/source/app.rst
Normal file
7
docs/source/app.rst
Normal file
@ -0,0 +1,7 @@
|
||||
app module
|
||||
==========
|
||||
|
||||
.. automodule:: app
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
7
docs/source/communicator.rst
Normal file
7
docs/source/communicator.rst
Normal file
@ -0,0 +1,7 @@
|
||||
communicator module
|
||||
===================
|
||||
|
||||
.. automodule:: communicator
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
7
docs/source/consumerlocator.rst
Normal file
7
docs/source/consumerlocator.rst
Normal file
@ -0,0 +1,7 @@
|
||||
consumerlocator module
|
||||
======================
|
||||
|
||||
.. automodule:: consumerlocator
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
7
docs/source/messagesender.rst
Normal file
7
docs/source/messagesender.rst
Normal file
@ -0,0 +1,7 @@
|
||||
messagesender module
|
||||
====================
|
||||
|
||||
.. automodule:: messagesender
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
11
docs/source/modules.rst
Normal file
11
docs/source/modules.rst
Normal file
@ -0,0 +1,11 @@
|
||||
producer
|
||||
========
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 4
|
||||
|
||||
app
|
||||
communicator
|
||||
consumerlocator
|
||||
messagesender
|
||||
test
|
7
docs/source/test.rst
Normal file
7
docs/source/test.rst
Normal file
@ -0,0 +1,7 @@
|
||||
test module
|
||||
===========
|
||||
|
||||
.. automodule:: test
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
@ -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,20 @@ 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'
|
||||
"""**Constructor:**
|
||||
Initializes the object.
|
||||
|
||||
: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 +42,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