From b38a9b2fd3157ca2847c62f1ffcca5523b9293ac Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Mon, 16 Nov 2015 14:01:51 +0100 Subject: [PATCH] Added redis config logic --- .../extension/sql/0.0.1/11_redis_helper.sql | 21 ++++++++++++++++++- server/extension/sql/0.0.1/30_admin0.sql | 1 + .../cartodb_geocoder/redis_helper.py | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/server/extension/sql/0.0.1/11_redis_helper.sql b/server/extension/sql/0.0.1/11_redis_helper.sql index 593d9fb..7c7791b 100644 --- a/server/extension/sql/0.0.1/11_redis_helper.sql +++ b/server/extension/sql/0.0.1/11_redis_helper.sql @@ -5,7 +5,26 @@ RETURNS boolean AS $$ return False else: from cartodb_geocoder import redis_helper - redis_connection = redis_helper.RedisHelper('localhost', 6379, 5).redis_connection() + config_params = plpy.execute("select c.host, c.port, c.timeout, c.db from cdb_geocoder_server._get_redis_conf() c;")[0] + redis_connection = redis_helper.RedisHelper(config_params['host'], config_params['port'], config_params['db']).redis_connection() GD[user_id] = {'redis_connection': redis_connection} return True +$$ LANGUAGE plpythonu; + +CREATE TYPE cdb_geocoder_server._redis_conf_params AS ( + host text, + port int, + timeout float, + db text +); + +CREATE OR REPLACE FUNCTION cdb_geocoder_server._get_redis_conf() +RETURNS cdb_geocoder_server._redis_conf_params AS $$ + conf = plpy.execute("SELECT cdb_geocoder_server._config_get('redis_conf') conf")[0]['conf'] + if conf is None: + plpy.error("There is no redis configuration defined") + else: + import json + params = json.loads(conf) + return { "host": params['host'], "port": params['port'], 'timeout': params['timeout'], 'db': params['db'] } $$ LANGUAGE plpythonu; \ No newline at end of file diff --git a/server/extension/sql/0.0.1/30_admin0.sql b/server/extension/sql/0.0.1/30_admin0.sql index e5a5eeb..c7986d4 100644 --- a/server/extension/sql/0.0.1/30_admin0.sql +++ b/server/extension/sql/0.0.1/30_admin0.sql @@ -2,6 +2,7 @@ CREATE OR REPLACE FUNCTION cdb_geocoder_server.geocode_admin0_polygon(user_id name, tx_id bigint, country_name text) RETURNS Geometry AS $$ + from cartodb_geocoder import quota_service plpy.debug('Entering geocode_admin0_polygons') plpy.debug('user_id = %s' % user_id) diff --git a/server/lib/python/cartodb_geocoder/cartodb_geocoder/redis_helper.py b/server/lib/python/cartodb_geocoder/cartodb_geocoder/redis_helper.py index ddaa5af..013ac9d 100644 --- a/server/lib/python/cartodb_geocoder/cartodb_geocoder/redis_helper.py +++ b/server/lib/python/cartodb_geocoder/cartodb_geocoder/redis_helper.py @@ -15,5 +15,6 @@ class RedisHelper: return self.__create_redis_connection() def __create_redis_connection(self): + #TODO Change to use Sentinel pool = redis.ConnectionPool(host=self.host, port=self.port, db=self.db) return redis.Redis(connection_pool=pool) \ No newline at end of file