blob: 9b02d086f8aba2c087c9f3349f207d5f059ee4ed [file] [log] [blame]
Sapan Bhatia26d40bc2014-05-12 15:28:02 -04001import threading
2import requests, json
3
4from planetstack.config import Config
5
6import uuid
7import os
8import imp
9import inspect
10import base64
11from fofum import Fofum
12import json
13import traceback
14
Sapan Bhatiab7262192014-07-22 00:30:16 -040015random_client_id=None
16def get_random_client_id():
17 global random_client_id
18
19 if (random_client_id is None) and os.path.exists("/opt/planetstack/random_client_id"):
20 # try to use the last one we used, if we saved it
21 try:
22 random_client_id = open("/opt/planetstack/random_client_id","r").readline().strip()
23 print "get_random_client_id: loaded %s" % random_client_id
24 except:
25 print "get_random_client_id: failed to read /opt/planetstack/random_client_id"
26
27 if random_client_id is None:
28 random_client_id = base64.urlsafe_b64encode(os.urandom(12))
29 print "get_random_client_id: generated new id %s" % random_client_id
30
31 # try to save it for later (XXX: could race with another client here)
32 try:
33 open("/opt/planetstack/random_client_id","w").write("%s\n" % random_client_id)
34 except:
35 print "get_random_client_id: failed to write /opt/planetstack/random_client_id"
36
37 return random_client_id
38
39# decorator that marks dispatachable event methods
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040040def event(func):
41 setattr(func, 'event', func.__name__)
Sapan Bhatiab7262192014-07-22 00:30:16 -040042 return func
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040043
44class EventHandler:
45 # This code is currently not in use.
46 def __init__(self):
Sapan Bhatiab7262192014-07-22 00:30:16 -040047 pass
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040048
49 @staticmethod
50 def get_events():
51 events = []
52 for name in dir(EventHandler):
53 attribute = getattr(EventHandler, name)
54 if hasattr(attribute, 'event'):
55 events.append(getattr(attribute, 'event'))
56 return events
57
58 def dispatch(self, event, *args, **kwds):
59 if hasattr(self, event):
60 return getattr(self, event)(*args, **kwds)
Sapan Bhatiab7262192014-07-22 00:30:16 -040061
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040062
63class EventSender:
64 def __init__(self,user=None,clientid=None):
65 try:
66 user = Config().feefie_client_user
67 except:
68 user = 'pl'
69
70 try:
71 clid = Config().feefie_client_id
72 except:
Sapan Bhatiab7262192014-07-22 00:30:16 -040073 clid = get_random_client_id()
74 print "EventSender: no feefie_client_id configured. Using random id %s" % clid
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040075
76 self.fofum = Fofum(user=user)
77 self.fofum.make(clid)
78
79 def fire(self,**kwargs):
Sapan Bhatiab7262192014-07-22 00:30:16 -040080 kwargs["uuid"] = str(uuid.uuid1())
Sapan Bhatia26d40bc2014-05-12 15:28:02 -040081 self.fofum.fire(json.dumps(kwargs))
82
83class EventListener:
84 def __init__(self,wake_up=None):
85 self.handler = EventHandler()
86 self.wake_up = wake_up
Sapan Bhatiab7262192014-07-22 00:30:16 -040087 self.deleters = {}
88 self.load_deleter_modules()
89
90 def load_deleter_modules(self, deleter_dir=None):
91 if deleter_dir is None:
92 if hasattr(Config(), "observer_deleters_dir"):
93 deleter_dir = Config().observer_deleters_dir
94 else:
95 deleter_dir = "/opt/planetstack/observer/deleters"
96
97 for fn in os.listdir(deleter_dir):
98 pathname = os.path.join(deleter_dir,fn)
99 if os.path.isfile(pathname) and fn.endswith(".py") and (fn!="__init__.py"):
100 module = imp.load_source(fn[:-3],pathname)
101 for classname in dir(module):
102 c = getattr(module, classname, None)
103
104 # make sure 'c' is a descendent of Deleter and has a
105 # provides field (this eliminates the abstract base classes
106 # since they don't have a provides)
107
108 if inspect.isclass(c) and issubclass(c, Deleter) and hasattr(c,"model") and c.model!=None:
109 modelName = c.model
110 if not modelName in self.deleters:
111 self.deleters[modelName] = []
112 if not (c in self.deleters[modelName]):
113 self.deleters[modelName].append(c)
114 print 'loaded deleters: %s' % ",".join(self.deleters.keys())
115
Sapan Bhatia26d40bc2014-05-12 15:28:02 -0400116
117 def handle_event(self, payload):
118 payload_dict = json.loads(payload)
119
Sapan Bhatia26d40bc2014-05-12 15:28:02 -0400120 try:
Sapan Bhatiab7262192014-07-22 00:30:16 -0400121 deletion = payload_dict.get('delete_flag', False)
122 if (deletion):
123 model = payload_dict['model']
124 pk = payload_dict['pk']
125 model_dict = payload_dict['model_dict']
126
127 for deleter in self.deleters[model]:
128 try:
129 deleter()(pk, model_dict)
130 except:
131 # something is silently eating these
132 # exceptions...
133 traceback.print_exc()
134 raise
135 except:
136 deletion = False
137
138 if (not deletion and self.wake_up):
139 self.wake_up()
Sapan Bhatia26d40bc2014-05-12 15:28:02 -0400140
141 def run(self):
142 # This is our unique client id, to be used when firing and receiving events
143 # It needs to be generated once and placed in the config file
144
145 try:
146 user = Config().feefie_client_user
147 except:
148 user = 'pl'
149
150 try:
151 clid = Config().feefie_client_id
152 except:
Sapan Bhatiab7262192014-07-22 00:30:16 -0400153 clid = get_random_client_id()
154 print "EventListener: no feefie_client_id configured. Using random id %s" % clid
Sapan Bhatia26d40bc2014-05-12 15:28:02 -0400155
156 f = Fofum(user=user)
157
158 listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event))
159 listener_thread.start()