Memcache resolve

This commit is contained in:
Denis Zheleztsov 2016-11-10 17:22:49 +03:00
parent 21eabe0575
commit 7cef7737ba

View File

@ -3,6 +3,7 @@ import sys
import logging import logging
import requests import requests
import memcache import memcache
from .discovery import resolve
# Get old configuration # Get old configuration
@ -66,8 +67,31 @@ def do_reload(service_conf, app_conf):
return stdout return stdout
# Discovery memcached servers
def discovery_memcached(conf):
memcache = conf['memcached']
app_conf = {
"services": [
{
"name": memcache['discovery']['service'],
"group": memcache['discovery']['group']
}
]
}
hosts = resolve(app_conf, conf)
mc_servers = []
for server in hosts[memcache['discovery']['service']]:
mc_server = server['name'] + ':' + server['port']
mc_servers.append(mc_server)
return mc_servers
# !!! NEED REFACTORING !!! # !!! NEED REFACTORING !!!
def reload_conf(service_conf, app_conf, conf, app_hosts): def reload_conf(service_conf, app_conf, conf, app_hosts):
logger = logging.getLogger(__name__)
# Check marathon enabled in configuration # Check marathon enabled in configuration
if conf['marathon']['enabled'] is True: if conf['marathon']['enabled'] is True:
if get_old(app_conf['conf_name'], service_conf) != 1: if get_old(app_conf['conf_name'], service_conf) != 1:
@ -79,16 +103,20 @@ def reload_conf(service_conf, app_conf, conf, app_hosts):
if 'memcached' in conf: if 'memcached' in conf:
if conf['memcached']['enabled'] is True: if conf['memcached']['enabled'] is True:
# Check old servers # Check old servers
mc_hosts = None
if conf['memcached']['discovery']['enabled'] is True: if conf['memcached']['discovery']['enabled'] is True:
logging.warning('Discovery of Memcached not implpemented') logger.info('Discovery memcached hosts')
mc_hosts = discovery_memcached(conf)
else:
mc_hosts = conf['memcached']['hosts']
try: try:
mc = memcache.Client(conf['memcached']['hosts']) mc = memcache.Client(mc_hosts)
if get_old_from_memcache(mc, app_conf['conf_name'], service_conf) != 1: if get_old_from_memcache(mc, app_conf['conf_name'], app_hosts) != 1:
stdout = do_reload(service_conf, app_conf) stdout = do_reload(service_conf, app_conf)
logging.info(stdout) logger.info(stdout)
return True return True
except Exception as e: except Exception as e:
logging.error('Cannot connect to memcached: ' + str(e)) logger.error('Cannot connect to memcached: ' + str(e))
else: else:
logging.warning('DEPRECATED main conf file. Please use new syntax!') logging.warning('DEPRECATED main conf file. Please use new syntax!')
@ -97,7 +125,7 @@ def reload_conf(service_conf, app_conf, conf, app_hosts):
if get_old(app_conf['conf_name'], service_conf) != 1: if get_old(app_conf['conf_name'], service_conf) != 1:
stdout = do_reload(service_conf, app_conf) stdout = do_reload(service_conf, app_conf)
logging.info(stdout) logger.info(stdout)
return True return True
else: else:
if conf['loglevel'] == 'debug': if conf['loglevel'] == 'debug':