Merge branch 'master' of gitea:GoldenPogacsa/producer
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
commit
96a91c4154
@ -2,8 +2,10 @@ FROM python:3.8
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY . ./
|
||||
COPY requirements.txt ./
|
||||
|
||||
RUN pip3 install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY . ./
|
||||
|
||||
CMD ["python3", "app.py"]
|
||||
|
4
app.py
4
app.py
@ -13,6 +13,7 @@ import time
|
||||
from communicator import Communicator
|
||||
from consumerlocator import ConsumerLocator
|
||||
from messagesender import MessageSender
|
||||
from redisconnector import RedisConnector
|
||||
|
||||
__author__ = "@tormakris"
|
||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||
@ -39,8 +40,9 @@ if __name__ == "__main__":
|
||||
generateduuid = str(uuid.uuid4())
|
||||
communicator = Communicator(currentconsumer=KNOWNCONSUMER, uuid=generateduuid)
|
||||
LOGGER.debug(f"My uuid is {generateduuid}")
|
||||
consumerlocator = ConsumerLocator(uuid=generateduuid, communicator=communicator)
|
||||
messagesender = MessageSender(communicator=communicator, uuid=generateduuid)
|
||||
consumerlocator = ConsumerLocator(uuid=generateduuid, communicator=communicator,
|
||||
redisconnector=RedisConnector())
|
||||
consumerlocator.learnconsumerlist()
|
||||
|
||||
while True:
|
||||
|
@ -7,13 +7,14 @@ Consumer locator module, that manages the list of consumers.
|
||||
import datetime
|
||||
from communicator import Communicator
|
||||
import os
|
||||
from redisconnector import RedisConnector
|
||||
|
||||
__author__ = "@dscharnitzky"
|
||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||
__module_name__ = "consumerlocator"
|
||||
__version__text__ = "1"
|
||||
|
||||
KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1')
|
||||
KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER", '10.69.42.1')
|
||||
|
||||
|
||||
class ConsumerLocator:
|
||||
@ -22,7 +23,7 @@ class ConsumerLocator:
|
||||
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, redisconnector: RedisConnector):
|
||||
"""**Constructor:**
|
||||
Initializes the object.
|
||||
|
||||
@ -31,8 +32,9 @@ class ConsumerLocator:
|
||||
: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]
|
||||
self.red = redisconnector
|
||||
self.red.consumerlist = [{"Host": KNOWNCONSUMER, "State": True, "LastOk": datetime.datetime.now()}]
|
||||
self.red.currentconsumer = self.red.consumerlist[0]
|
||||
self.communicator = communicator
|
||||
|
||||
def learnconsumerlist(self) -> None:
|
||||
@ -48,12 +50,12 @@ class ConsumerLocator:
|
||||
return
|
||||
for recconsumer in recievedconsumerlist:
|
||||
contains = False
|
||||
for consumer in self.consumerlist:
|
||||
for consumer in self.red.consumerlist:
|
||||
if consumer["Host"] == recconsumer:
|
||||
contains = True
|
||||
|
||||
if not contains:
|
||||
self.consumerlist.append({"Host": recconsumer, "State": True, "LastOk": datetime.datetime.now()})
|
||||
self.red.consumerlist.append({"Host": recconsumer, "State": True, "LastOk": datetime.datetime.now()})
|
||||
|
||||
self.updateconsumerlist()
|
||||
|
||||
@ -66,7 +68,7 @@ class ConsumerLocator:
|
||||
:return: None
|
||||
"""
|
||||
removelist = []
|
||||
for consumer in self.consumerlist:
|
||||
for consumer in self.red.consumerlist:
|
||||
if not self.communicator.checkconsumer(consumer["Host"]):
|
||||
consumer["State"] = False
|
||||
if datetime.datetime.now() - consumer["LastOk"] > datetime.timedelta(hours=1):
|
||||
@ -75,7 +77,7 @@ class ConsumerLocator:
|
||||
consumer["LastOk"] = datetime.datetime.now()
|
||||
consumer["State"] = True
|
||||
for rem in removelist:
|
||||
self.consumerlist.remove(rem)
|
||||
self.red.consumerlist.remove(rem)
|
||||
|
||||
def updateconsumer(self):
|
||||
"""If the current consumer is not available, checks all the consumers in the list and updates the current one.
|
||||
@ -91,18 +93,18 @@ class ConsumerLocator:
|
||||
self.updateconsumerlist()
|
||||
newcurrentconsumer = None
|
||||
|
||||
for consumer in self.consumerlist:
|
||||
for consumer in self.red.consumerlist:
|
||||
if consumer["State"]:
|
||||
newcurrentconsumer = consumer
|
||||
break
|
||||
|
||||
self.currentconsumer = newcurrentconsumer
|
||||
if self.currentconsumer is not None:
|
||||
self.red.currentconsumer = newcurrentconsumer
|
||||
if self.red.currentconsumer is not None:
|
||||
self.learnconsumerlist()
|
||||
|
||||
if self.currentconsumer is not None:
|
||||
self.communicator.set_currentconsumer(self.currentconsumer["Host"])
|
||||
return self.currentconsumer["Host"]
|
||||
if self.red.currentconsumer is not None:
|
||||
self.communicator.set_currentconsumer(self.red.currentconsumer["Host"])
|
||||
return self.red.currentconsumer["Host"]
|
||||
else:
|
||||
return None
|
||||
|
||||
@ -111,13 +113,13 @@ class ConsumerLocator:
|
||||
|
||||
:return: the current consumer
|
||||
"""
|
||||
return self.currentconsumer["Host"]
|
||||
return self.red.currentconsumer["Host"]
|
||||
|
||||
def checkcurrentconsumer(self) -> bool:
|
||||
"""Check the current consumer's health.
|
||||
|
||||
:return: True if OK, False if fail
|
||||
"""
|
||||
if self.currentconsumer is None:
|
||||
if self.red.currentconsumer is None:
|
||||
return False
|
||||
return self.communicator.checkconsumer(self.currentconsumer["Host"])
|
||||
return self.communicator.checkconsumer(self.red.currentconsumer["Host"])
|
||||
|
16
test.py
16
test.py
@ -8,6 +8,7 @@ import re
|
||||
import consumerlocator
|
||||
import communicator
|
||||
import messagesender
|
||||
import redisconnector
|
||||
|
||||
|
||||
__author__ = "@tormakris"
|
||||
@ -196,7 +197,8 @@ def test_learnconsumerlist(httpserver):
|
||||
uuid=generateduuid)
|
||||
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
|
||||
locator = consumerlocator.ConsumerLocator(
|
||||
uuid=generateduuid, communicator=comm)
|
||||
uuid=generateduuid, communicator=comm,
|
||||
redisconnector=redisconnector.RedisConnector())
|
||||
ret = locator.learnconsumerlist()
|
||||
assert ret is None
|
||||
|
||||
@ -213,7 +215,8 @@ def test_getcurrentconsumer(mocker):
|
||||
currentconsumer="127.0.0.1",
|
||||
uuid=generateduuid)
|
||||
locator = consumerlocator.ConsumerLocator(
|
||||
uuid=generateduuid, communicator=comm)
|
||||
uuid=generateduuid, communicator=comm,
|
||||
redisconnector=redisconnector.RedisConnector())
|
||||
assert locator.getcurrentconsumer() == consumerlocator.KNOWNCONSUMER
|
||||
|
||||
|
||||
@ -236,7 +239,8 @@ def test_checkcurrentconsumer(httpserver):
|
||||
uuid=generateduuid)
|
||||
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
|
||||
locator = consumerlocator.ConsumerLocator(
|
||||
uuid=generateduuid, communicator=comm)
|
||||
uuid=generateduuid, communicator=comm,
|
||||
redisconnector=redisconnector.RedisConnector())
|
||||
ret = locator.checkcurrentconsumer()
|
||||
assert ret == True
|
||||
|
||||
@ -260,7 +264,8 @@ def test_updateconsumer(httpserver):
|
||||
uuid=generateduuid)
|
||||
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
|
||||
locator = consumerlocator.ConsumerLocator(
|
||||
uuid=generateduuid, communicator=comm)
|
||||
uuid=generateduuid, communicator=comm,
|
||||
redisconnector=redisconnector.RedisConnector())
|
||||
assert locator.currentconsumer is not None
|
||||
ret = locator.updateconsumer()
|
||||
assert ret == f"127.0.0.1:{port}"
|
||||
@ -285,6 +290,7 @@ def test_updateconsumerlist(httpserver):
|
||||
uuid=generateduuid)
|
||||
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
|
||||
locator = consumerlocator.ConsumerLocator(
|
||||
uuid=generateduuid, communicator=comm)
|
||||
uuid=generateduuid, communicator=comm,
|
||||
redisconnector=redisconnector.RedisConnector())
|
||||
ret = locator.updateconsumerlist()
|
||||
assert ret is None
|
||||
|
Reference in New Issue
Block a user