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/redisconnector.py

64 lines
1.5 KiB
Python

#!/usr/bin/env python
import os
import redis
import json
"""
Redis interaction
"""
__author__ = "@tormakris"
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
__module_name__ = "redis"
__version__text__ = "1"
REDISHOST = os.getenv("PRODUCER_REDIS", 'localhost')
class RedisConnector:
"""
Class abstracting Redis communication
"""
def __init__(self):
"""
Initialize class
"""
self.redisconnection = redis.StrictRedis(host=REDISHOST, port=6379, db=0)
def get_consumerlist(self):
"""
Gets list of consumers stored in Redis.
:return:
"""
return json.loads(self.redisconnection.get('consumerList'))
def set_consumerlist(self, consumerlist):
"""
Sets list of consumers stored in Redis.
:param consumerlist:
:return:
"""
json_list = json.dumps(consumerlist)
self.redisconnection.set('consumerList', json_list)
def get_currentconsumer(self):
"""
Gets currently active consumer.
:return:
"""
return json.loads(self.redisconnection.get('currentConsumer'))
def set_currentconsumer(self, currentconsumer):
"""
Sets currently active consumer
:param currentconsumer:
:return:
"""
json_dict = json.dumps(currentconsumer)
self.redisconnection.set('currentConsumer', json_dict)
consumerlist = property(get_consumerlist, set_consumerlist)
currentconsumer = property(get_currentconsumer, set_currentconsumer)