This repository was archived by the owner on Nov 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy paththreaded_model_watcher.py
More file actions
99 lines (71 loc) · 2.87 KB
/
Copy paththreaded_model_watcher.py
File metadata and controls
99 lines (71 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""Dispatch model events to another thread, instead of through Celery.
Note that the ModelEventWatcher are Mapper-level flush events, so they cannot
create objects. This pushes the logic on another thread, so we're already
using another thread-specific session."""
from __future__ import print_function
from threading import Thread
from Queue import Queue
from zope import interface
from ..lib.model_watcher import IModelEventWatcher
class ThreadDispatcher(Thread):
"""A thread that will receive CRUD events and hand them to another model watcher."""
singleton = None
daemon = True
"The class of the model watcher"
mw_class = None
@classmethod
def get_instance(cls):
if not cls.singleton:
cls.singleton = cls()
cls.singleton.start()
return cls.singleton
def __init__(self):
super(ThreadDispatcher, self).__init__()
self.queue = Queue()
self.dying = False
self.mw = self.mw_class()
def run(self):
while not self.dying:
print("*"*20)
event = self.queue.get()
print(event)
method_name = event[0]
method = getattr(self.mw, method_name)
method(*event[1:])
@classmethod
def start_dispatcher(cls):
cls.get_instance()
@interface.implementer(IModelEventWatcher)
class ThreadedModelEventWatcher(object):
"""A IModelEventWatcher that will dispatch events to its
:py:class:`ThreadDispatcher`"""
def __init__(self):
self.queue = ThreadDispatcher.get_instance().queue
def processPostCreated(self, id):
self.queue.put(('processPostCreated', id))
def processPostModified(self, id, state_changed):
self.queue.put(('processPostModified', id, state_changed))
def processIdeaCreated(self, id):
self.queue.put(('processIdeaCreated', id))
def processIdeaModified(self, id, version):
self.queue.put(('processIdeaModified', id, version))
def processIdeaDeleted(self, id):
self.queue.put(('processIdeaDeleted', id))
def processExtractCreated(self, id):
self.queue.put(('processExtractCreated', id))
def processExtractModified(self, id, version):
self.queue.put(('processExtractModified', id, version))
def processExtractDeleted(self, id):
self.queue.put(('processExtractDeleted', id))
def processAccountCreated(self, id):
self.queue.put(('processAccountCreated', id))
def processAccountModified(self, id):
self.queue.put(('processAccountModified', id))
def configure_threaded_watcher(settings):
from . import resolver
class_name = settings.get(
'assembl.threadedmodelwatcher',
"assembl.lib.model_watcher.BaseModelEventWatcher")
ThreadDispatcher.mw_class = resolver.resolve(class_name)
def includeme(config):
configure_threaded_watcher(config.get_settings())