Redis: observer notify creates resource instance on demand

We don't need now to load all resources, we just create the resource
class instance on demand, update it and then get rid of it.
This commit is contained in:
Przemyslaw Kaminski 2015-06-08 16:04:03 +02:00
parent de50af0ea5
commit 9af20e8328
3 changed files with 28 additions and 4 deletions

View File

@ -1,4 +1,7 @@
from solar.core import signals
from solar.interfaces.db import get_db
db = get_db()
class BaseObserver(object):
@ -14,7 +17,18 @@ class BaseObserver(object):
self.attached_to = attached_to
self.name = name
self.value = value
self.receivers = []
#self.receivers = []
@property
def receivers(self):
from solar.core import resource
signals.CLIENTS = signals.Connections.read_clients()
for receiver_name, receiver_input in signals.Connections.receivers(
self.attached_to.name,
self.name
):
yield resource.load(receiver_name).args[receiver_input]
def log(self, msg):
print '{} {}'.format(self, msg)
@ -62,7 +76,7 @@ class BaseObserver(object):
if self.find_receiver(receiver):
self.log('No multiple subscriptions from {}'.format(receiver))
return
self.receivers.append(receiver)
#self.receivers.append(receiver)
receiver.subscribed(self)
signals.Connections.add(
@ -84,7 +98,7 @@ class BaseObserver(object):
"""
self.log('Unsubscribe {}'.format(receiver))
if self.find_receiver(receiver):
self.receivers.remove(receiver)
#self.receivers.remove(receiver)
receiver.unsubscribed(self)
signals.Connections.remove(

View File

@ -152,6 +152,12 @@ def wrap_resource(raw_resource):
return Resource(name, raw_resource, args, tags=tags)
def load(resource_name):
raw_resource = db.read(resource_name, collection=db.COLLECTIONS.resource)
return wrap_resource(raw_resource)
def load_all():
ret = {}
@ -159,7 +165,7 @@ def load_all():
resource = wrap_resource(raw_resource)
ret[resource.name] = resource
signals.Connections.reconnect_all()
#signals.Connections.reconnect_all()
return ret

View File

@ -100,6 +100,10 @@ class Connections(object):
emitter.args[emitter_input].subscribe(
receiver.args[receiver_input])
@staticmethod
def receivers(emitter_name, emitter_input_name):
return CLIENTS.get(emitter_name, {}).get(emitter_input_name, [])
@staticmethod
def clear():
global CLIENTS