This repository has been archived on 2020-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
producer/app.py

62 lines
2.4 KiB
Python

#!/usr/bin/env python
"""
Main entry point, this module builds the producer from the submodules.
"""
import os
import random
import uuid
import logging
import sentry_sdk
import time
from consumerinformation import ConsumerInformation
from communicator import Communicator
from consumerlocator import ConsumerLocator
from messagesender import MessageSender
from redisconnector import RedisConnector
__author__ = "@tormakris"
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
__module_name__ = "app"
__version__text__ = "1"
sentry_sdk.init("https://3fa5ae886ba1489092ad49a93cb419c1@sentry.kmlabz.com/9")
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
)
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
messages. Basically this is a big loop, learning about consumers ( :func:`consumerlocator.ConsumerLocator.learnconsumerlist` ),
updating the current ( :func:`consumerlocator.ConsumerLocator.updateconsumer` ) one and sending the message
( :func:`messagesender.MessageSender.sendmessage` ).
If the current consumer is unavailable, the update will change to an available one. To not flood the network with
infinite data, we some random time.
"""
LOGGER.info("Producer started")
generateduuid = str(uuid.uuid4())
redisconnector = RedisConnector()
consumerinfomation = ConsumerInformation(redisconnector=redisconnector)
communicator = Communicator(currentconsumer=KNOWNCONSUMER, uuid=generateduuid,
consumerinformation=consumerinfomation)
LOGGER.debug(f"My uuid is {generateduuid}")
messagesender = MessageSender(communicator=communicator, uuid=generateduuid)
consumerlocator = ConsumerLocator(communicator=communicator,
redisconnector=redisconnector)
while True:
consumerlocator.learnconsumerlist()
LOGGER.info(f"Updating consumer list of {generateduuid}")
consumerlocator.updateconsumer()
LOGGER.info("Sending message to consumer")
messagesender.sendmessage()
time.sleep(random.random())