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():
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}
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():
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={}):
@ -155,7 +155,7 @@ def wrap_resource(raw_resource):
def load_all():
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)
ret[resource.name] = resource

View File

@ -11,12 +11,48 @@ from solar.interfaces.db import get_db
db = get_db()
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):
"""
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
def add(emitter, src, receiver, dst):
if src not in emitter.args:
@ -31,7 +67,8 @@ class Connections(object):
if [receiver.name, dst] not in CLIENTS[emitter.name][src]:
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
def remove(emitter, src, receiver, dst):
@ -40,7 +77,8 @@ class Connections(object):
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
def reconnect_all():
@ -52,12 +90,12 @@ class Connections(object):
for emitter_name, dest_dict in CLIENTS.items():
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 receiver_name, receiver_input in destinations:
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(
receiver.args[receiver_input])
@ -75,9 +113,11 @@ class Connections(object):
@staticmethod
def flush():
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)
@ -141,7 +181,7 @@ def disconnect_receiver_by_input(receiver, input):
:return:
"""
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)
@ -163,7 +203,7 @@ def notify(source, key, value):
if key in CLIENTS[source.name]:
for client, r_key in CLIENTS[source.name][key]:
resource = wrap_resource(
db.read(client, collection_name=db.COLLECTIONS.resource)
db.read(client, collection=db.COLLECTIONS.resource)
)
print 'Resource found', client
if resource:

View File

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