Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 1 | import threading |
| 2 | import requests, json |
| 3 | |
| 4 | from planetstack.config import Config |
| 5 | |
| 6 | import uuid |
| 7 | import os |
| 8 | import imp |
| 9 | import inspect |
| 10 | import base64 |
| 11 | from fofum import Fofum |
| 12 | import json |
| 13 | import traceback |
| 14 | |
Sapan Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame^] | 15 | random_client_id=None |
| 16 | def 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 Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 40 | def event(func): |
| 41 | setattr(func, 'event', func.__name__) |
Sapan Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame^] | 42 | return func |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 43 | |
| 44 | class EventHandler: |
| 45 | # This code is currently not in use. |
| 46 | def __init__(self): |
Sapan Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame^] | 47 | pass |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 48 | |
| 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 Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame^] | 61 | |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 62 | |
| 63 | class 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 Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame^] | 73 | clid = get_random_client_id() |
| 74 | print "EventSender: no feefie_client_id configured. Using random id %s" % clid |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 75 | |
| 76 | self.fofum = Fofum(user=user) |
| 77 | self.fofum.make(clid) |
| 78 | |
| 79 | def fire(self,**kwargs): |
Sapan Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame^] | 80 | kwargs["uuid"] = str(uuid.uuid1()) |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 81 | self.fofum.fire(json.dumps(kwargs)) |
| 82 | |
| 83 | class EventListener: |
| 84 | def __init__(self,wake_up=None): |
| 85 | self.handler = EventHandler() |
| 86 | self.wake_up = wake_up |
Sapan Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame^] | 87 | 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 Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 116 | |
| 117 | def handle_event(self, payload): |
| 118 | payload_dict = json.loads(payload) |
| 119 | |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 120 | try: |
Sapan Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame^] | 121 | 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 Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 140 | |
| 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 Bhatia | b726219 | 2014-07-22 00:30:16 -0400 | [diff] [blame^] | 153 | clid = get_random_client_id() |
| 154 | print "EventListener: no feefie_client_id configured. Using random id %s" % clid |
Sapan Bhatia | 26d40bc | 2014-05-12 15:28:02 -0400 | [diff] [blame] | 155 | |
| 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() |