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 consumerlocator import ConsumerLocator
from messagesender import MessageSender
from redisconnector import RedisConnector
__author__ = "@tormakris"
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
@ -39,7 +40,8 @@ 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)
consumerlocator = ConsumerLocator(uuid=generateduuid, communicator=communicator,
redisconnector=RedisConnector())
messagesender = MessageSender(communicator=communicator)
consumerlocator.learnconsumerlist()

View File

@ -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
View File

@ -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