-
Notifications
You must be signed in to change notification settings - Fork 21
/
webed-sphinx.py
executable file
·311 lines (224 loc) · 9.88 KB
/
webed-sphinx.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!bin/python
###############################################################################
###############################################################################
from flask.ext.script import Manager, Command, Option
from werkzeug.serving import run_with_reloader
from zmq.devices import ThreadDevice
from datetime import datetime
from webed.views.sphinx import backend
from webed.app import app
import zmq
import base64
import select
###############################################################################
###############################################################################
context = zmq.Context (1)
###############################################################################
###############################################################################
manager = Manager (app)
###############################################################################
###############################################################################
class ZmqPing (Command):
"""ZMQ Ping: Sends a request message"""
def get_options (self):
return [
Option ('-n', '--number', dest='number', default=1, type=int),
Option ('-m', '--message', dest='message', default='PING'),
Option ('-a', '--address', dest='address', default=
'tcp://localhost:8080'),
]
def run (self, *args, **kwargs):
number = kwargs['number']
assert number
message = kwargs['message']
assert message
address = kwargs['address']
assert address
sock = context.socket (zmq.REQ)
sock.connect (address)
for n in range (number):
try:
timestamp = datetime.now ()
sock.send (message)
print '[%s] %s' % (timestamp, sock.recv ())
except KeyboardInterrupt:
break
manager.add_command ('zmq-ping', ZmqPing ())
###############################################################################
###############################################################################
class ZmqEcho (Command):
"""ZMQ Echo: Responds with an echo message to a request"""
def get_options (self):
return [
Option ('-b64', '--base64', dest='base64', action='store_true'),
Option ('-a', '--address', dest='address', default=
'tcp://localhost:8181'),
]
def run (self, *args, **kwargs):
b64flag = kwargs['base64']
address = kwargs['address']
assert address
sock = context.socket (zmq.REP)
sock.connect (address)
while True:
try:
message = sock.recv()
sock.send (message)
print '[%s] %s' % (
datetime.now (), base64.encodestring (message)
if b64flag else message)
except KeyboardInterrupt:
break
manager.add_command ('zmq-echo', ZmqEcho ())
###############################################################################
###############################################################################
class ZmqQueue (Command):
"""ZMQ Queue: Connects frontend clients with backend services"""
def get_options (self):
return [
Option ('-f', '--frontend-address', dest='frontend-address',
default='tcp://*:8080'),
Option ('-b', '--backend-address', dest='backend-address',
default='tcp://*:8181'),
]
def run (self, *args, **kwargs):
frontend_address = kwargs['frontend-address']
assert frontend_address
backend_address = kwargs['backend-address']
assert backend_address
frontend = context.socket (zmq.ROUTER)
frontend.IDENTITY = 'ROUTER'
frontend.bind (frontend_address)
backend = context.socket (zmq.DEALER)
frontend.IDENTITY = 'DEALER'
backend.bind (backend_address)
try:
zmq.device (zmq.QUEUE, frontend, backend)
except KeyboardInterrupt:
pass
class ZmqQueueThread (ZmqQueue):
"""ZMQ Q-Thread: Connects frontend clients with backend services"""
def run (self, *args, **kwargs):
frontend_address = kwargs['frontend-address']
assert frontend_address
backend_address = kwargs['backend-address']
assert backend_address
device = ThreadDevice (zmq.QUEUE, zmq.ROUTER, zmq.DEALER)
device.bind_in (frontend_address)
device.setsockopt_in (zmq.IDENTITY, 'ROUTER')
device.bind_out (backend_address)
device.setsockopt_out (zmq.IDENTITY, 'DEALER')
device.start ()
manager.add_command ('zmq-queue', ZmqQueue ())
###############################################################################
###############################################################################
class PingQueue (ZmqQueue):
"""Ping Queue: Connects frontend clients with backend services"""
def get_options (self):
return [
Option ('-pfa', '--ping-frontend-address',
dest='ping-frontend-address',
default=app.config['PING_QUEUE_ADDRESS']),
Option ('-pba', '--ping-backend-address',
dest='ping-backend-address',
default=app.config['PING_QUEUE_BACKEND_ADDRESS']),
]
def run (self, *args, **kwargs):
frontend_address = kwargs['ping-frontend-address']
assert frontend_address
backend_address = kwargs['ping-backend-address']
assert backend_address
kwargs['frontend-address'] = frontend_address
kwargs['backend-address'] = backend_address
super (PingQueue, self).run (*args, **kwargs)
class PingQueueThread (PingQueue, ZmqQueueThread):
"""Ping Q-Thread: Connects frontend clients with backend services"""
def run (self, *args, **kwargs):
super (PingQueueThread, self).run (*args, **kwargs)
class DataQueue (ZmqQueue):
"""Data Queue: Connects frontend clients with backend services"""
def get_options (self):
return [
Option ('-dfa', '--data-frontend-address',
dest='data-frontend-address',
default=app.config['DATA_QUEUE_ADDRESS']),
Option ('-dba', '--data-backend-address',
dest='data-backend-address',
default=app.config['DATA_QUEUE_BACKEND_ADDRESS']),
]
def run (self, *args, **kwargs):
frontend_address = kwargs['data-frontend-address']
assert frontend_address
backend_address = kwargs['data-backend-address']
assert backend_address
kwargs['frontend-address'] = frontend_address
kwargs['backend-address'] = backend_address
super (DataQueue, self).run (*args, **kwargs)
class DataQueueThread (DataQueue, ZmqQueueThread):
"""Data Q-Thread: Connects frontend clients with backend services"""
def run (self, *args, **kwargs):
super (DataQueueThread, self).run (*args, **kwargs)
class Queue (Command):
"""Queue: Connects frontend clients with backend services"""
def get_options (self):
return PingQueue ().get_options () + DataQueue ().get_options ()
def run (self, *args, **kwargs):
PingQueueThread ().run (*args, **kwargs)
DataQueueThread ().run (*args, **kwargs)
try:
select.select ([], [], [])
except KeyboardInterrupt:
pass
###############################################################################
manager.add_command ('queue-ping', PingQueue ())
manager.add_command ('queue-data', DataQueue ())
manager.add_command ('queue', Queue ())
###############################################################################
###############################################################################
class Converter (Command):
"""Converter: processes projects to reports (PDF, HTML or LaTex)"""
def get_options (self):
return [
Option ('-w', '--worker-threads', dest='worker-threads',
default=1, type=int),
Option ('-p', '--ping-address', dest='ping-address',
default=app.config['PING_BACKEND_ADDRESS']),
Option ('-d', '--data-address', dest='data-address',
default=app.config['DATA_BACKEND_ADDRESS']),
Option ('-t', '--poll-timeout', dest='poll-timeout',
default=app.config['POLL_TIMEOUT'], type=int),
]
def run (self, *args, **kwargs):
worker_threads = kwargs['worker-threads']
assert worker_threads >= 0
ping_address = kwargs['ping-address']
assert ping_address
data_address = kwargs['data-address']
assert data_address
poll_timeout = kwargs['poll-timeout']
assert poll_timeout
workers = []
def start ():
while any (map (lambda w: not w.stopped, workers)):
for worker in workers: worker.stop ()
while len (workers) > 0:
workers.pop ()
for _ in range (worker_threads):
workers.append (backend.Worker (
ping_address=ping_address,
data_address=data_address,
poll_timeout=poll_timeout
))
workers[-1].start ()
try:
run_with_reloader (start)
except KeyboardInterrupt:
while any (map (lambda w: not w.stopped, workers)):
for worker in workers: worker.stop ()
manager.add_command ('converter', Converter ())
###############################################################################
###############################################################################
if __name__ == '__main__':
manager.run ()
###############################################################################
###############################################################################