Merged some changes that SCott made to event_manager
diff --git a/planetstack/ec2_observer/event_loop.py b/planetstack/ec2_observer/event_loop.py
index dd81e24..e985007 100644
--- a/planetstack/ec2_observer/event_loop.py
+++ b/planetstack/ec2_observer/event_loop.py
@@ -19,7 +19,7 @@
from ec2_observer.steps import *
from syncstep import SyncStep
from toposort import toposort
-from ec2_observer.error_mapper import error_mapper
+from ec2_observer.error_mapper import *
debug_mode = False
diff --git a/planetstack/ec2_observer/event_manager.py b/planetstack/ec2_observer/event_manager.py
index 19d9e25..9b02d08 100644
--- a/planetstack/ec2_observer/event_manager.py
+++ b/planetstack/ec2_observer/event_manager.py
@@ -12,15 +12,39 @@
import json
import traceback
-# decorator that marks dispatachable event methods
+random_client_id=None
+def get_random_client_id():
+ global random_client_id
+
+ if (random_client_id is None) and os.path.exists("/opt/planetstack/random_client_id"):
+ # try to use the last one we used, if we saved it
+ try:
+ random_client_id = open("/opt/planetstack/random_client_id","r").readline().strip()
+ print "get_random_client_id: loaded %s" % random_client_id
+ except:
+ print "get_random_client_id: failed to read /opt/planetstack/random_client_id"
+
+ if random_client_id is None:
+ random_client_id = base64.urlsafe_b64encode(os.urandom(12))
+ print "get_random_client_id: generated new id %s" % random_client_id
+
+ # try to save it for later (XXX: could race with another client here)
+ try:
+ open("/opt/planetstack/random_client_id","w").write("%s\n" % random_client_id)
+ except:
+ print "get_random_client_id: failed to write /opt/planetstack/random_client_id"
+
+ return random_client_id
+
+# decorator that marks dispatachable event methods
def event(func):
setattr(func, 'event', func.__name__)
- return func
+ return func
class EventHandler:
# This code is currently not in use.
def __init__(self):
- pass
+ pass
@staticmethod
def get_events():
@@ -34,7 +58,7 @@
def dispatch(self, event, *args, **kwds):
if hasattr(self, event):
return getattr(self, event)(*args, **kwds)
-
+
class EventSender:
def __init__(self,user=None,clientid=None):
@@ -46,33 +70,73 @@
try:
clid = Config().feefie_client_id
except:
- clid = self.random_client_id()
-
+ clid = get_random_client_id()
+ print "EventSender: no feefie_client_id configured. Using random id %s" % clid
self.fofum = Fofum(user=user)
self.fofum.make(clid)
def fire(self,**kwargs):
- kwargs["uuid"] = str(uuid.uuid1())
+ kwargs["uuid"] = str(uuid.uuid1())
self.fofum.fire(json.dumps(kwargs))
class EventListener:
def __init__(self,wake_up=None):
self.handler = EventHandler()
self.wake_up = wake_up
+ self.deleters = {}
+ self.load_deleter_modules()
+
+ def load_deleter_modules(self, deleter_dir=None):
+ if deleter_dir is None:
+ if hasattr(Config(), "observer_deleters_dir"):
+ deleter_dir = Config().observer_deleters_dir
+ else:
+ deleter_dir = "/opt/planetstack/observer/deleters"
+
+ for fn in os.listdir(deleter_dir):
+ pathname = os.path.join(deleter_dir,fn)
+ if os.path.isfile(pathname) and fn.endswith(".py") and (fn!="__init__.py"):
+ module = imp.load_source(fn[:-3],pathname)
+ for classname in dir(module):
+ c = getattr(module, classname, None)
+
+ # make sure 'c' is a descendent of Deleter and has a
+ # provides field (this eliminates the abstract base classes
+ # since they don't have a provides)
+
+ if inspect.isclass(c) and issubclass(c, Deleter) and hasattr(c,"model") and c.model!=None:
+ modelName = c.model
+ if not modelName in self.deleters:
+ self.deleters[modelName] = []
+ if not (c in self.deleters[modelName]):
+ self.deleters[modelName].append(c)
+ print 'loaded deleters: %s' % ",".join(self.deleters.keys())
+
def handle_event(self, payload):
payload_dict = json.loads(payload)
- if (self.wake_up):
- self.wake_up()
-
- def random_client_id(self):
try:
- return self.client_id
- except AttributeError:
- self.client_id = base64.urlsafe_b64encode(os.urandom(12))
- return self.client_id
+ deletion = payload_dict.get('delete_flag', False)
+ if (deletion):
+ model = payload_dict['model']
+ pk = payload_dict['pk']
+ model_dict = payload_dict['model_dict']
+
+ for deleter in self.deleters[model]:
+ try:
+ deleter()(pk, model_dict)
+ except:
+ # something is silently eating these
+ # exceptions...
+ traceback.print_exc()
+ raise
+ except:
+ deletion = False
+
+ if (not deletion and self.wake_up):
+ self.wake_up()
def run(self):
# This is our unique client id, to be used when firing and receiving events
@@ -86,7 +150,8 @@
try:
clid = Config().feefie_client_id
except:
- clid = self.random_client_id()
+ clid = get_random_client_id()
+ print "EventListener: no feefie_client_id configured. Using random id %s" % clid
f = Fofum(user=user)