Added redis to consumerlocator

This commit is contained in:
Scharnitzky Donát 2020-04-17 16:47:27 +02:00
parent eff5f4191c
commit 4a3fc3b1ba
3 changed files with 33 additions and 23 deletions

4
app.py
View File

@ -13,6 +13,7 @@ import time
from communicator import Communicator from communicator import Communicator
from consumerlocator import ConsumerLocator from consumerlocator import ConsumerLocator
from messagesender import MessageSender from messagesender import MessageSender
from redisconnector import RedisConnector
__author__ = "@tormakris" __author__ = "@tormakris"
__copyright__ = "Copyright 2020, GoldenPogácsa Team" __copyright__ = "Copyright 2020, GoldenPogácsa Team"
@ -39,7 +40,8 @@ if __name__ == "__main__":
generateduuid = str(uuid.uuid4()) generateduuid = str(uuid.uuid4())
communicator = Communicator(currentconsumer=KNOWNCONSUMER, uuid=generateduuid) communicator = Communicator(currentconsumer=KNOWNCONSUMER, uuid=generateduuid)
LOGGER.debug(f"My uuid is {generateduuid}") LOGGER.debug(f"My uuid is {generateduuid}")
consumerlocator = ConsumerLocator(uuid=generateduuid, communicator=communicator) consumerlocator = ConsumerLocator(uuid=generateduuid, communicator=communicator,
redisconnector=RedisConnector())
messagesender = MessageSender(communicator=communicator) messagesender = MessageSender(communicator=communicator)
consumerlocator.learnconsumerlist() consumerlocator.learnconsumerlist()

View File

@ -7,13 +7,14 @@ 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
from redisconnector import RedisConnector
__author__ = "@dscharnitzky" __author__ = "@dscharnitzky"
__copyright__ = "Copyright 2020, GoldenPogácsa Team" __copyright__ = "Copyright 2020, GoldenPogácsa Team"
__module_name__ = "consumerlocator" __module_name__ = "consumerlocator"
__version__text__ = "1" __version__text__ = "1"
KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1') KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER", '10.69.42.1')
class ConsumerLocator: class ConsumerLocator:
@ -22,7 +23,7 @@ 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, redisconnector: RedisConnector):
"""**Constructor:** """**Constructor:**
Initializes the object. Initializes the object.
@ -31,8 +32,9 @@ class ConsumerLocator:
:param uuid: Not used :param uuid: Not used
:param communicator: the :class:'communicator.Communicator' instance that will be used for the low level communication. :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.red = redisconnector
self.currentconsumer = self.consumerlist[0] self.red.consumerlist = [{"Host": KNOWNCONSUMER, "State": True, "LastOk": datetime.datetime.now()}]
self.red.currentconsumer = self.red.consumerlist[0]
self.communicator = communicator self.communicator = communicator
def learnconsumerlist(self) -> None: def learnconsumerlist(self) -> None:
@ -48,12 +50,12 @@ class ConsumerLocator:
return return
for recconsumer in recievedconsumerlist: for recconsumer in recievedconsumerlist:
contains = False contains = False
for consumer in self.consumerlist: for consumer in self.red.consumerlist:
if consumer["Host"] == recconsumer: if consumer["Host"] == recconsumer:
contains = True contains = True
if not contains: 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() self.updateconsumerlist()
@ -66,7 +68,7 @@ class ConsumerLocator:
:return: None :return: None
""" """
removelist = [] removelist = []
for consumer in self.consumerlist: for consumer in self.red.consumerlist:
if not self.communicator.checkconsumer(consumer["Host"]): if not self.communicator.checkconsumer(consumer["Host"]):
consumer["State"] = False consumer["State"] = False
if datetime.datetime.now() - consumer["LastOk"] > datetime.timedelta(hours=1): if datetime.datetime.now() - consumer["LastOk"] > datetime.timedelta(hours=1):
@ -75,7 +77,7 @@ class ConsumerLocator:
consumer["LastOk"] = datetime.datetime.now() consumer["LastOk"] = datetime.datetime.now()
consumer["State"] = True consumer["State"] = True
for rem in removelist: for rem in removelist:
self.consumerlist.remove(rem) self.red.consumerlist.remove(rem)
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.
@ -91,18 +93,18 @@ class ConsumerLocator:
self.updateconsumerlist() self.updateconsumerlist()
newcurrentconsumer = None newcurrentconsumer = None
for consumer in self.consumerlist: for consumer in self.red.consumerlist:
if consumer["State"]: if consumer["State"]:
newcurrentconsumer = consumer newcurrentconsumer = consumer
break break
self.currentconsumer = newcurrentconsumer self.red.currentconsumer = newcurrentconsumer
if self.currentconsumer is not None: if self.red.currentconsumer is not None:
self.learnconsumerlist() self.learnconsumerlist()
if self.currentconsumer is not None: if self.red.currentconsumer is not None:
self.communicator.set_currentconsumer(self.currentconsumer["Host"]) self.communicator.set_currentconsumer(self.red.currentconsumer["Host"])
return self.currentconsumer["Host"] return self.red.currentconsumer["Host"]
else: else:
return None return None
@ -111,13 +113,13 @@ class ConsumerLocator:
:return: the current consumer :return: the current consumer
""" """
return self.currentconsumer["Host"] return self.red.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.red.currentconsumer is None:
return False return False
return self.communicator.checkconsumer(self.currentconsumer["Host"]) return self.communicator.checkconsumer(self.red.currentconsumer["Host"])

16
test.py
View File

@ -8,6 +8,7 @@ import re
import consumerlocator import consumerlocator
import communicator import communicator
import messagesender import messagesender
import redisconnector
__author__ = "@tormakris" __author__ = "@tormakris"
@ -196,7 +197,8 @@ def test_learnconsumerlist(httpserver):
uuid=generateduuid) uuid=generateduuid)
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}" consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
locator = consumerlocator.ConsumerLocator( locator = consumerlocator.ConsumerLocator(
uuid=generateduuid, communicator=comm) uuid=generateduuid, communicator=comm,
redisconnector=redisconnector.RedisConnector())
ret = locator.learnconsumerlist() ret = locator.learnconsumerlist()
assert ret is None assert ret is None
@ -213,7 +215,8 @@ def test_getcurrentconsumer(mocker):
currentconsumer="127.0.0.1", currentconsumer="127.0.0.1",
uuid=generateduuid) uuid=generateduuid)
locator = consumerlocator.ConsumerLocator( locator = consumerlocator.ConsumerLocator(
uuid=generateduuid, communicator=comm) uuid=generateduuid, communicator=comm,
redisconnector=redisconnector.RedisConnector())
assert locator.getcurrentconsumer() == consumerlocator.KNOWNCONSUMER assert locator.getcurrentconsumer() == consumerlocator.KNOWNCONSUMER
@ -236,7 +239,8 @@ def test_checkcurrentconsumer(httpserver):
uuid=generateduuid) uuid=generateduuid)
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}" consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
locator = consumerlocator.ConsumerLocator( locator = consumerlocator.ConsumerLocator(
uuid=generateduuid, communicator=comm) uuid=generateduuid, communicator=comm,
redisconnector=redisconnector.RedisConnector())
ret = locator.checkcurrentconsumer() ret = locator.checkcurrentconsumer()
assert ret == True assert ret == True
@ -260,7 +264,8 @@ def test_updateconsumer(httpserver):
uuid=generateduuid) uuid=generateduuid)
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}" consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
locator = consumerlocator.ConsumerLocator( locator = consumerlocator.ConsumerLocator(
uuid=generateduuid, communicator=comm) uuid=generateduuid, communicator=comm,
redisconnector=redisconnector.RedisConnector())
assert locator.currentconsumer is not None assert locator.currentconsumer is not None
ret = locator.updateconsumer() ret = locator.updateconsumer()
assert ret == f"127.0.0.1:{port}" assert ret == f"127.0.0.1:{port}"
@ -285,6 +290,7 @@ def test_updateconsumerlist(httpserver):
uuid=generateduuid) uuid=generateduuid)
consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}" consumerlocator.KNOWNCONSUMER = f"127.0.0.1:{port}"
locator = consumerlocator.ConsumerLocator( locator = consumerlocator.ConsumerLocator(
uuid=generateduuid, communicator=comm) uuid=generateduuid, communicator=comm,
redisconnector=redisconnector.RedisConnector())
ret = locator.updateconsumerlist() ret = locator.updateconsumerlist()
assert ret is None assert ret is None