Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 1 | import threading |
| 2 | import requests, json |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 3 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 4 | from core.models import * |
smbaker | d39abb6 | 2013-08-05 22:21:09 -0700 | [diff] [blame] | 5 | #from openstack.manager import OpenStackManager |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 6 | from planetstack.config import Config |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 7 | from observer.deleters import deleters |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 8 | |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 9 | import os |
| 10 | import base64 |
Tony Mack | 5c0c455 | 2013-07-03 09:36:51 -0400 | [diff] [blame] | 11 | from fofum import Fofum |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 12 | import json |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 13 | |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 14 | # decorator that marks dispatachable event methods |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 15 | def event(func): |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 16 | setattr(func, 'event', func.__name__) |
| 17 | return func |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 18 | |
| 19 | class EventHandler: |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 20 | # This code is currently not in use. |
| 21 | def __init__(self): |
| 22 | pass #self.manager = OpenStackManager() |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 23 | |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 24 | @staticmethod |
| 25 | def get_events(): |
| 26 | events = [] |
| 27 | for name in dir(EventHandler): |
| 28 | attribute = getattr(EventHandler, name) |
| 29 | if hasattr(attribute, 'event'): |
| 30 | events.append(getattr(attribute, 'event')) |
| 31 | return events |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 32 | |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 33 | def dispatch(self, event, *args, **kwds): |
| 34 | if hasattr(self, event): |
| 35 | return getattr(self, event)(*args, **kwds) |
| 36 | |
| 37 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 38 | class EventSender: |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 39 | def __init__(self,user=None,clientid=None): |
| 40 | try: |
| 41 | clid = Config().feefie_client_id |
| 42 | user = Config().feefie_client_user |
| 43 | except: |
| 44 | clid = 'planetstack_core_team' |
| 45 | user = 'pl' |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 46 | |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 47 | self.fofum = Fofum(user=user) |
| 48 | self.fofum.make(clid) |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 49 | |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 50 | def fire(self,**args): |
| 51 | self.fofum.fire(json.dumps(args)) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 52 | |
| 53 | class EventListener: |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 54 | def __init__(self,wake_up=None): |
| 55 | self.handler = EventHandler() |
| 56 | self.wake_up = wake_up |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 57 | |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 58 | def handle_event(self, payload): |
| 59 | payload_dict = json.loads(payload) |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 60 | |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 61 | try: |
| 62 | deletion = payload_dict['deletion_flag'] |
| 63 | if (deletion): |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 64 | model = payload_dict['model'] |
| 65 | pk = payload_dict['pk'] |
| 66 | |
| 67 | for deleter in deleters[model]: |
| 68 | deleter(pk) |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 69 | except: |
| 70 | deletion = False |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 71 | |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 72 | if (not deletion and self.wake_up): |
| 73 | self.wake_up() |
| 74 | |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 75 | |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 76 | def run(self): |
| 77 | # This is our unique client id, to be used when firing and receiving events |
| 78 | # It needs to be generated once and placed in the config file |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 79 | |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 80 | try: |
| 81 | clid = Config().feefie_client_id |
| 82 | user = Config().feefie_client_user |
| 83 | except: |
| 84 | clid = 'planetstack_core_team' |
| 85 | user = 'pl' |
| 86 | |
| 87 | f = Fofum(user=user) |
| 88 | |
| 89 | listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event)) |
| 90 | listener_thread.start() |