Refactor to move logic from SQL functions to Python
- Moved the logic the retrieve the redis connection params to RedisDBConfig class - Moved the logic that retrieve the services configuration to ServicesDBConfig
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
from redis_tools import RedisConnection
|
||||
from redis_tools import RedisConnection, RedisDBConfig
|
||||
from coordinates import Coordinate
|
||||
from polyline import PolyLine
|
||||
|
||||
@@ -1,32 +1,87 @@
|
||||
from redis.sentinel import Sentinel
|
||||
from redis import StrictRedis
|
||||
import json
|
||||
|
||||
|
||||
class RedisConnection:
|
||||
|
||||
REDIS_DEFAULT_USER_DB = 5
|
||||
REDIS_DEFAULT_TIMEOUT = 2 #seconds
|
||||
|
||||
def __init__(self, sentinel_master_id, redis_host, redis_port,
|
||||
redis_db=REDIS_DEFAULT_USER_DB, **kwargs):
|
||||
self.redis_host = redis_host
|
||||
self.redis_port = redis_port
|
||||
self.sentinel_master_id = sentinel_master_id
|
||||
self.timeout = kwargs['timeout'] if 'timeout' in kwargs else self.REDIS_DEFAULT_TIMEOUT
|
||||
self.redis_db = redis_db
|
||||
def __init__(self, config):
|
||||
self._config = config
|
||||
|
||||
def redis_connection(self):
|
||||
return self.__create_redis_connection()
|
||||
|
||||
def __create_redis_connection(self):
|
||||
if self.sentinel_master_id == None:
|
||||
return StrictRedis(host=self.redis_host, port=self.redis_port, db=self.redis_db)
|
||||
else:
|
||||
sentinel = Sentinel([(self.redis_host,
|
||||
self.redis_port)],
|
||||
socket_timeout=self.timeout)
|
||||
if self._config.sentinel_id:
|
||||
sentinel = Sentinel([(self._config.host,
|
||||
self._config.port)],
|
||||
socket_timeout=self._config.timeout)
|
||||
return sentinel.master_for(
|
||||
self.sentinel_master_id,
|
||||
socket_timeout=self.timeout,
|
||||
db=self.redis_db
|
||||
)
|
||||
else:
|
||||
conn = StrictRedis(host=self._config.host, port=self._config.port,
|
||||
db=self._config.db)
|
||||
return conn
|
||||
|
||||
|
||||
class RedisDBConfig:
|
||||
|
||||
DEFAULT_USER_DB = 5
|
||||
DEFAULT_TIMEOUT = 2 # seconds
|
||||
|
||||
def __init__(self, key, db_conn):
|
||||
self._db_conn = db_conn
|
||||
return self._build(key)
|
||||
|
||||
def _build(self, key):
|
||||
conf_query = "SELECT cartodb.CDB_Conf_GetConf('{0}') as conf".format(
|
||||
key)
|
||||
conf = self._db_conn.execute(conf_query)[0]['conf']
|
||||
if conf is None:
|
||||
raise "There is no redis configuration defined"
|
||||
else:
|
||||
params = json.loads(conf)
|
||||
self._host = params['redis_host']
|
||||
self._port = params['redis_port']
|
||||
|
||||
if "timeout" in params:
|
||||
self._timeout = params['timeout']
|
||||
else:
|
||||
self._timeout = self.DEFAULT_TIMEOUT
|
||||
|
||||
if "redis_db" in params:
|
||||
self._db = params['redis_db']
|
||||
else:
|
||||
self._db = self.DEFAULT_USER_DB
|
||||
|
||||
if "sentinel_master_id" in params:
|
||||
self._sentinel_id = params["sentinel_master_id"]
|
||||
else:
|
||||
self._sentinel_id = None
|
||||
|
||||
def __str__(self):
|
||||
return "Host: {0}, Port: {1}, Sentinel id: {2}, DB: {3}".format(
|
||||
self.host, self.port, self.sentinel_id, self.db)
|
||||
|
||||
@property
|
||||
def host(self):
|
||||
return self._host
|
||||
|
||||
@property
|
||||
def port(self):
|
||||
return self._port
|
||||
|
||||
@property
|
||||
def timeout(self):
|
||||
return self._timeout
|
||||
|
||||
@property
|
||||
def db(self):
|
||||
return self._db
|
||||
|
||||
@property
|
||||
def sentinel_id(self):
|
||||
return self._sentinel_id
|
||||
|
||||
Reference in New Issue
Block a user