Redis: connections are now saved to Redis DB

This commit is contained in:
Przemyslaw Kaminski 2015-06-08 13:48:06 +02:00
parent 8db397d365
commit de50af0ea5
4 changed files with 59 additions and 19 deletions

View File

@ -252,7 +252,7 @@ def deploy():
def undeploy(): def undeploy():
db = get_db() db = get_db()
resources = map(resource.wrap_resource, db.get_list(collection_name=db.COLLECTIONS.resource)) resources = map(resource.wrap_resource, db.get_list(collection=db.COLLECTIONS.resource))
resources = {r.name: r for r in resources} resources = {r.name: r for r in resources}
actions.resource_action(resources['glance_api_endpoint'], 'remove') actions.resource_action(resources['glance_api_endpoint'], 'remove')

View File

@ -116,7 +116,7 @@ class Resource(object):
for k, v in self.args_dict().items(): for k, v in self.args_dict().items():
metadata['input'][k]['value'] = v metadata['input'][k]['value'] = v
db.save(self.name, metadata, collection_name=db.COLLECTIONS.resource) db.save(self.name, metadata, collection=db.COLLECTIONS.resource)
def create(name, base_path, args, tags=[], connections={}): def create(name, base_path, args, tags=[], connections={}):
@ -155,7 +155,7 @@ def wrap_resource(raw_resource):
def load_all(): def load_all():
ret = {} ret = {}
for raw_resource in db.get_list(collection_name=db.COLLECTIONS.resource): for raw_resource in db.get_list(collection=db.COLLECTIONS.resource):
resource = wrap_resource(raw_resource) resource = wrap_resource(raw_resource)
ret[resource.name] = resource ret[resource.name] = resource

View File

@ -11,12 +11,48 @@ from solar.interfaces.db import get_db
db = get_db() db = get_db()
CLIENTS_CONFIG_KEY = 'clients-data-file' CLIENTS_CONFIG_KEY = 'clients-data-file'
CLIENTS = utils.read_config_file(CLIENTS_CONFIG_KEY) #CLIENTS = utils.read_config_file(CLIENTS_CONFIG_KEY)
CLIENTS = {}
class Connections(object): class Connections(object):
"""
CLIENTS structure is:
emitter_name:
emitter_input_name:
- - dst_name
- dst_input_name
while DB structure is:
emitter_name_key:
emitter: emitter_name
sources:
emitter_input_name:
- - dst_name
- dst_input_name
"""
@staticmethod
def read_clients():
ret = {}
for data in db.get_list(collection=db.COLLECTIONS.connection):
ret[data['emitter']] = data['sources']
return ret
@staticmethod
def save_clients():
for emitter_name, sources in CLIENTS.items():
data = {
'emitter': emitter_name,
'sources': sources,
}
db.save(emitter_name, data, collection=db.COLLECTIONS.connection)
@staticmethod @staticmethod
def add(emitter, src, receiver, dst): def add(emitter, src, receiver, dst):
if src not in emitter.args: if src not in emitter.args:
@ -31,7 +67,8 @@ class Connections(object):
if [receiver.name, dst] not in CLIENTS[emitter.name][src]: if [receiver.name, dst] not in CLIENTS[emitter.name][src]:
CLIENTS[emitter.name][src].append([receiver.name, dst]) CLIENTS[emitter.name][src].append([receiver.name, dst])
utils.save_to_config_file(CLIENTS_CONFIG_KEY, CLIENTS) #utils.save_to_config_file(CLIENTS_CONFIG_KEY, CLIENTS)
Connections.save_clients()
@staticmethod @staticmethod
def remove(emitter, src, receiver, dst): def remove(emitter, src, receiver, dst):
@ -40,7 +77,8 @@ class Connections(object):
if destination != [receiver.name, dst] if destination != [receiver.name, dst]
] ]
utils.save_to_config_file(CLIENTS_CONFIG_KEY, CLIENTS) #utils.save_to_config_file(CLIENTS_CONFIG_KEY, CLIENTS)
Connections.save_clients()
@staticmethod @staticmethod
def reconnect_all(): def reconnect_all():
@ -52,12 +90,12 @@ class Connections(object):
for emitter_name, dest_dict in CLIENTS.items(): for emitter_name, dest_dict in CLIENTS.items():
emitter = wrap_resource( emitter = wrap_resource(
db.read(emitter_name, collection_name=db.COLLECTIONS.resource) db.read(emitter_name, collection=db.COLLECTIONS.resource)
) )
for emitter_input, destinations in dest_dict.items(): for emitter_input, destinations in dest_dict.items():
for receiver_name, receiver_input in destinations: for receiver_name, receiver_input in destinations:
receiver = wrap_resource( receiver = wrap_resource(
db.read(receiver_name, collection_name=db.COLLECTIONS.resource) db.read(receiver_name, collection=db.COLLECTIONS.resource)
) )
emitter.args[emitter_input].subscribe( emitter.args[emitter_input].subscribe(
receiver.args[receiver_input]) receiver.args[receiver_input])
@ -75,9 +113,11 @@ class Connections(object):
@staticmethod @staticmethod
def flush(): def flush():
print 'FLUSHING Connections' print 'FLUSHING Connections'
utils.save_to_config_file(CLIENTS_CONFIG_KEY, CLIENTS) #utils.save_to_config_file(CLIENTS_CONFIG_KEY, CLIENTS)
Connections.save_clients()
CLIENTS = Connections.read_clients()
#atexit.register(Connections.flush) #atexit.register(Connections.flush)
@ -141,7 +181,7 @@ def disconnect_receiver_by_input(receiver, input):
:return: :return:
""" """
for emitter_name, inputs in CLIENTS.items(): for emitter_name, inputs in CLIENTS.items():
emitter = db.read(emitter_name, collection_name=db.COLLECTIONS.resource) emitter = db.read(emitter_name, collection=db.COLLECTIONS.resource)
disconnect_by_src(emitter['id'], input, receiver) disconnect_by_src(emitter['id'], input, receiver)
@ -163,7 +203,7 @@ def notify(source, key, value):
if key in CLIENTS[source.name]: if key in CLIENTS[source.name]:
for client, r_key in CLIENTS[source.name][key]: for client, r_key in CLIENTS[source.name][key]:
resource = wrap_resource( resource = wrap_resource(
db.read(client, collection_name=db.COLLECTIONS.resource) db.read(client, collection=db.COLLECTIONS.resource)
) )
print 'Resource found', client print 'Resource found', client
if resource: if resource:

View File

@ -20,19 +20,19 @@ class RedisDB(object):
self._r = redis.StrictRedis(**self.DB) self._r = redis.StrictRedis(**self.DB)
self.entities = {} self.entities = {}
def read(self, uid, collection_name=COLLECTIONS.resource): def read(self, uid, collection=COLLECTIONS.resource):
return json.loads( return json.loads(
self._r.get(self._make_key(collection_name, uid)) self._r.get(self._make_key(collection, uid))
) )
def save(self, uid, data, collection_name=COLLECTIONS.resource): def save(self, uid, data, collection=COLLECTIONS.resource):
return self._r.set( return self._r.set(
self._make_key(collection_name, uid), self._make_key(collection, uid),
json.dumps(data) json.dumps(data)
) )
def get_list(self, collection_name=COLLECTIONS.resource): def get_list(self, collection=COLLECTIONS.resource):
key_glob = self._make_key(collection_name, '*') key_glob = self._make_key(collection, '*')
for key in self._r.keys(key_glob): for key in self._r.keys(key_glob):
yield json.loads(self._r.get(key)) yield json.loads(self._r.get(key))
@ -41,4 +41,4 @@ class RedisDB(object):
self._r.flushdb() self._r.flushdb()
def _make_key(self, collection, _id): def _make_key(self, collection, _id):
return '{0}-{1}'.format(collection, _id) return '{0}:{1}'.format(collection, _id)