forked from chexov/queueit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
executable file
·351 lines (298 loc) · 11.5 KB
/
__init__.py
File metadata and controls
executable file
·351 lines (298 loc) · 11.5 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
import logging
import time
import beanstalkc
from beanstalkc import * # TODO: remove it
logging.basicConfig(level=logging.DEBUG, format=u'%(asctime)-15s %(name)-12s: %(levelname)-8s %(message)s')
LOG = logging.getLogger('queueit')
LOG.setLevel(logging.DEBUG)
# Loading values from the shell ENV
QHOST = os.environ.get('QUEUEIT_HOST', '127.0.0.1')
QPORT = 11300
QTIMEOUT = None
QPRIORITY = os.environ.get('QUEUEIT_PRIORITY', beanstalkc.DEFAULT_PRIORITY)
try:
QTTR = int(os.environ.get('QUEUEIT_TTR', 120))
except ValueError:
LOG.error("Incorect value for QUEUEIT_TTR. Using '%s' instead" % QTTR)
try:
QPRIORITY = int(os.environ.get('QUEUEIT_PRIORITY', beanstalkc.DEFAULT_PRIORITY))
# print "QUEUEIT_PRIORITY is set to", QPRIORITY
except ValueError:
LOG.error("Incorect value for QUEUEIT_PRIORITY. Using '%s' instead" % QPRIORITY)
try:
QPORT = int(os.environ.get('QUEUEIT_PORT', 11300))
except ValueError:
LOG.error("Incorect value for QUEUEIT_PORT. Using '%s' instead" % QPORT)
try:
QTIMEOUT = os.environ.get('QUEUEIT_TIMEOUT', QTIMEOUT)
if QTIMEOUT:
QTIMEOUT = int(QTIMEOUT)
except ValueError:
LOG.error("Incorect value for QTIMEOUT. Using '%s' instead" % QTIMEOUT)
def _get_qconnection(host, port):
try:
return Connection(host=host, port=port)
except SocketError:
print "Can't connect to %s:%s" % (host, port)
sys.exit(1)
def qget(tube_name, qconn=None):
"""
Reserving job from the tube, printing the job.body and deleting that job.
Be aware that job is already gone from the beanstalkd standpoint
and operator have to deal with it.
"""
if not qconn:
qconn = _get_qconnection(QHOST, QPORT)
qconn.watch(tube_name)
job = qconn.reserve()
print job.body
job.delete()
qconn.close()
def qput(tube_name, messages, qconn=None):
if not qconn:
qconn = _get_qconnection(QHOST, QPORT)
for message in messages:
qconn.use(tube_name)
jobid = qconn.put(str(message), ttr=QTTR, priority=QPRIORITY)
print "OK: message=%s; tube=%s; job.id=%s; ttr=%s; priority=%s" % (message, tube_name, jobid, QTTR, QPRIORITY)
qconn.close()
def qkick(tube_name, count=1, qconn=None):
if not qconn:
qconn = _get_qconnection(QHOST, QPORT)
qconn.use(tube_name)
print qconn.kick(count)
def qstat(qconn=None, delay=None):
def compare_tubes(last, cur):
_cur = cur.copy()
for _k, _v in last.items():
try:
change = int(_cur[_k]) - int(_v)
if change > 0:
change = "+%s" % change
elif change == 0:
change = ""
_cur[_k] = "%-5s %s" % (_cur[_k], change)
except ValueError:
pass
# Not an number
return _cur
def compare_numbers(last, cur):
_cur = cur
change = int(_cur) - int(last)
if change > -1:
change = "+%s" % change
_cur = "%s %s" % (_cur, change)
return _cur
if not qconn:
qconn = _get_qconnection(QHOST, QPORT)
tubes_stats_last = {}
while True:
LINE="%-24s %-13s %-13s %-13s"
print LINE % ('tube', 'watching', 'buried', 'ready')
tubes = qconn.tubes()
tubes.sort()
for tube in tubes:
if tube:
name = str(tube)
tube_stats_cur = qconn.stats_tube(tube)
if tubes_stats_last.get(name):
tube_stats = compare_tubes(tubes_stats_last[name], tube_stats_cur)
else:
tube_stats = tube_stats_cur
#tube_stats = qconn.stats_tube(tube)
print LINE % (name,
tube_stats.get('current-watching'),
tube_stats.get('current-jobs-buried'),
tube_stats.get('current-jobs-ready'))
tubes_stats_last[name] = tube_stats_cur
if delay:
time.sleep(delay)
else:
return None
def qwrapperbatch(tube_in, tube_out, worker_cmd, batch_size=10):
import subprocess
import os
_env = os.environ
qconn_in = _get_qconnection(QHOST, QPORT)
qconn_out = _get_qconnection(QHOST, QPORT)
qconn_in.watch(tube_in)
qconn_out.use(tube_out)
while True:
batch_jobs = []
cmd = []
cmd.extend(worker_cmd)
# Filling the pool
while len(batch_jobs) < batch_size:
LOG.debug(u"Filling up the job pool: %s jobs already. %s needed" % (len(batch_jobs), batch_size))
try:
job = qconn_in.reserve(timeout=QTIMEOUT)
if not job:
LOG.info(u"Timeout reached")
batch_jobs.append(job)
except DeadlineSoon:
LOG.debug(u"Oops. Got DeadlineSoon. Do not waiting for queue to fill up..")
#LOG.debug(u"Oops. Got DeadlineSoon. Touching reserved jobs..")
map(lambda j: j.touch(), batch_jobs)
break
#continue
cmd.extend(map(lambda j: j.body, batch_jobs))
LOG.debug(u"Calling command %s...%s" % (u" ".join(cmd)[:30], u" ".join(cmd)[-15:]))
#LOG.debug(u"Calling command %s" % (u" ".join(cmd)))
retcode = subprocess.call(cmd)
if not retcode == 0:
def _bury(job):
job.bury()
LOG.info(u"Job %s buried" % job.jid)
map(_bury, batch_jobs)
else:
def _put(job):
jid = qconn_out.put(job.body)
print job.body
job.delete()
LOG.info(u"New job put in to %s: %s" % (tube_out, jid))
map(_put, batch_jobs)
def qcleanup(qname):
qconn = _get_qconnection(QHOST, QPORT)
print "Cleaninig up", qconn.use(qname)
while True:
job = qconn.peek_buried()
if not job:
break
print "Deleting:", job.jid, job.body
job.delete()
# FIXME: this is copypaste from local draft. not working. should be fixed
def qwrapperwithstats():
stattistics_queue_name = sys.argv[1]
obj_id = sys.argv[2]
command = u" ".join(sys.argv[3:])
QUEUESTATS_CONN = get_qconn_or_die(HOST,PORT)
QUEUESTATS_CONN.use(stattistics_queue_name)
log_id = "%s.%s" % (socket.gethostname(), uuid4().hex)
log_redirect = " > %s/%s.log 2>&1" % (LOG_DIR, log_id)
cmd = u"".join([command, log_redirect])
LOG.debug(u"Calling command %s" % cmd)
time_started = int(time.time())
retcode = subprocess.call(cmd, shell=True)
time_duration = int(time.time()) - time_started
LOG.info(u"Exit code is %s" % retcode)
task_stats = {'obj_id': obj_id,
'duration': time_duration,
'started': time_started,
'worker': command,
'ecode': retcode,
'log_id': log_id }
QUEUESTATS_CONN.put(json.dumps(task_stats))
LOG.info(u"Task statistics: %s" % json.dumps(task_stats))
sys.exit(retcode)
def qwrapper(tube_in, tube_out, worker_cmd):
import subprocess
qconn_in = _get_qconnection(QHOST, QPORT)
qconn_out = _get_qconnection(QHOST, QPORT)
qconn_in.watch(tube_in)
qconn_out.use(tube_out)
LOG.info(u"QHOST: %s, QPORT %s, TTR %s, PRIORITY %s, Watching queues: %s" % (QHOST, QPORT, QTTR, QPRIORITY, qconn_in.watching()) )
while True:
job = qconn_in.reserve(timeout=QTIMEOUT)
if not job:
LOG.info(u"Timeout reached. Bye-bye")
sys.exit()
params = job.body
# If we have {} inside the command, replace this with job payload. like xargs(1)
if u" ".join(worker_cmd).find('{}') > -1:
cmd = map(lambda arg: arg.replace('{}', job.body), worker_cmd)
else:
cmd = list(worker_cmd)
cmd.append(job.body)
LOG.info(u"Got job {0}".format(job.stats()))
LOG.info(u"Calling command '%s'" % cmd)
retcode = subprocess.call(cmd)
if not retcode == 0:
LOG.error(u"Worker command '%s' was exited with retcode %s" % (cmd, retcode) )
job.bury()
LOG.info(u"Job %s buried" % job.jid)
else:
if tube_out != "null":
jid = qconn_out.put(str(params), ttr=QTTR, priority=QPRIORITY)
LOG.info(u"New job put in to %s: %s" % (tube_out, jid))
job.delete()
def main():
try:
COMMAND = os.path.basename(sys.argv[0])
args = sys.argv[1:]
if COMMAND == 'queueit':
if len(sys.argv) == 1:
print "Usage:"
print "%s q-get" % COMMAND
print "%s q-put" % COMMAND
print "%s q-kick" % COMMAND
print "%s q-stat" % COMMAND
print "%s q-wrapper" % COMMAND
print "%s q-wrapper-batch" % COMMAND
print "%s q-wrapper-with-stats" % COMMAND
sys.exit(1)
else:
COMMAND = os.path.basename(sys.argv[1])
args = sys.argv[2:]
if COMMAND == 'q-get':
if not len(args) == 1:
print "Usage: %s <queue>" % (COMMAND)
sys.exit(1)
qget(args[0])
elif COMMAND == 'q-put':
if len(args) == 1:
qput(args[0], [sys.stdin.read(),])
elif len(args) > 1:
qput(args[0], args[1:])
else:
print "Usage: %s <queue> [<message>, <message>, ...]\n Message body could be sent trough STDIN" % (COMMAND )
sys.exit(1)
elif COMMAND == 'q-kick':
if len(args) < 1 or len(args) > 2:
print "Usage: %s <queue> [<count>]" % COMMAND
sys.exit(1)
count = 1
if len(args) == 2:
try:
count = int(args[1])
except ValueError:
print "Wrong count value '%s'. Using default %s" % (args[1], count)
qkick(args[0], count)
elif COMMAND == 'q-stat':
if len(args) == 1:
qstat(delay=int(args[0]))
else:
qstat()
elif COMMAND == 'q-wrapper':
if len(args) >= 3:
qwrapper(args[0], args[1], worker_cmd=args[2:])
else:
print "Usage: %s <queue-in> <queue-out> [<cmd>]\n <cmd> could be sent trough STDIN" % (COMMAND)
print sys.exit(1)
elif COMMAND == 'q-wrapper-with-stats':
if len(args) == 4:
qwrapperwithstats(stats_queue_name, job_id, command)
else:
print "Usage: %s <statistics-queue> <job_id> <cmd>" % (COMMAND)
print sys.exit(1)
elif COMMAND == 'q-wrapper-batch':
if len(args) >= 4:
qwrapperbatch(args[0], args[1], worker_cmd=args[3:], batch_size=int(args[2]))
else:
print "Usage: %s <queue-in> <queue-out> <batch-size> <cmd>" % (COMMAND)
print sys.exit(1)
elif COMMAND == 'q-cleanup':
if len(args) == 1:
qcleanup(args[0])
else:
print "Usage: %s <queue>" % (COMMAND)
print sys.exit(1)
else:
print "Unknown command '%s'" % COMMAND
except KeyboardInterrupt:
print "Keyboard Interrupt. Bye-bye"
if __name__ == "__main__":
main()