forked from brightroll/rq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrq.rb
309 lines (252 loc) · 8.09 KB
/
rq.rb
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
require 'code/rule_processor'
require 'code/queueclient'
require 'code/errors'
def check_usage(arg_list)
if not arg_list.length > 0 or arg_list.include?('-h') or arg_list.include?('--help')
puts "Valid commands are:"
puts " " + Commands.new.public_methods.grep(/^cmd_/).each{|c| c.to_s.gsub!(/^cmd_/, '') }.sort.join("\n ")
exit 1
end
end
def process_args(arg_list)
input = { }
input[:cmd] = arg_list.shift
input[:xtra] = []
# TODO: do this in a functional manner to cleanse
i = 0
while i < arg_list.length
if arg_list[i].index('--')
# Ok, we have a param
# ... does it have an '='
if arg_list[i].index('=')
parts = arg_list[i].split('=', 2)
input[parts[0][2..-1]] = parts[1]
i += 1
next
elsif (i+1 < arg_list.length) && (arg_list[i+1].index('--') == nil)
input[arg_list[i][2..-1]] = arg_list[i+1]
i += 2
next
end
input[arg_list[i][2..-1]] = true
i += 1
else
input[:xtra] << arg_list[i]
i += 1
end
end
input
end
class Commands
def get_queue_client(q_name)
RQ::QueueClient.new(q_name)
end
# Create a message
# - 'dest' queue
# - 'src' id
# - param[1234]
def cmd_sendmesg(args)
q_name = args['dest']
raise RQ::RqMissingArgument if not q_name
if q_name.start_with?('http:')
raise RQ::RqCannotRelay if !args.has_key?('relay-ok') # throw :halt, [404, 'Sorry - cannot relay message']
q_name = 'relay'
end
qc = get_queue_client(q_name)
# Construct message
mesg = {}
keys = %w(dest src count max_count param1 param2 param3 param4 due force_remote)
keys.each do |key|
next unless args.has_key?(key)
mesg[key] = args[key]
end
result = qc.create_message(mesg)
print "#{result[0]} #{result[1]}\n"
result[0] == "ok" ? 0 : 1
end
def cmd_prepmesg(args)
q_name = args['dest']
raise RQ::RqMissingArgument if not q_name
if q_name.start_with?('http:')
raise RQ::RqCannotRelay if !args.has_key?('relay-ok') # throw :halt, [404, 'Sorry - cannot relay message']
q_name = 'relay'
end
qc = get_queue_client(q_name)
# Construct message
mesg = {}
keys = %w(dest src count max_count param1 param2 param3 param4 due force_remote)
keys.each do |key|
next unless args.has_key?(key)
mesg[key] = args[key]
end
result = qc.prep_message(mesg)
print "#{result[0]} #{result[1]}\n"
result[0] == "ok" ? 0 : 1
end
def check_attachment(msg)
# simple early check, ok, now check for pathname
return [false, "No such file #{msg['pathname']} to attach to message"] unless File.exist?(msg['pathname'])
return [false, "Attachment currently cannot be a directory #{msg['pathname']}"] if File.directory?(msg['pathname'])
return [false, "Attachment currently cannot be read: #{msg['pathname']}"] unless File.readable?(msg['pathname'])
return [false, "Attachment currently not of supported type: #{msg['pathname']}"] unless File.file?(msg['pathname'])
return [true, '']
end
def cmd_attachmesg(args)
full_mesg_id = args['msg_id']
raise RQ::RqMissingArgument if not full_mesg_id
q_name = full_mesg_id[/\/q\/([^\/]+)/, 1]
msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1]
qc = get_queue_client(q_name)
# Construct message for queue mgr
msg = {'msg_id' => msg_id}
keys = %w(pathname name local_fs_only)
keys.each do |key|
next unless args.has_key?(key)
msg[key] = args[key]
end
msg['pathname'] = File.expand_path(msg['pathname'])
results = check_attachment(msg)
raise RQ::RqError(results[0]) if not results[0] # throw :halt, [404, "404 - #{results[0]}"]
result = qc.attach_message(msg)
print "#{result[0]} #{result[1]} for Message: #{full_mesg_id} attachment\n"
result[0] == "ok" ? 0 : 1
end
def cmd_commitmesg(args)
full_mesg_id = args['msg_id']
raise RQ::RqMissingArgument if not full_mesg_id
q_name = full_mesg_id[/\/q\/([^\/]+)/, 1]
msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1]
qc = get_queue_client(q_name)
# Construct message for queue mgr
mesg = {'msg_id' => msg_id }
result = qc.commit_message(mesg)
print "#{result[0]} #{result[1]}\n"
result[0] == "ok" ? 0 : 1
#p "#{result} for Message: #{mesg['msg-id']} committed"
end
def cmd_statusmesg(args)
full_mesg_id = args['msg_id']
raise RQ::RqMissingArgument if not full_mesg_id
q_name = full_mesg_id[/\/q\/([^\/]+)/, 1]
msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1]
qc = get_queue_client(q_name)
# Construct message for queue mgr
mesg = {'msg_id' => msg_id }
result = qc.get_message_status(mesg)
if result[0] == 'ok'
print "#{result[0]} #{result[1]['status']}\n"
else
print "#{result[0]} #{result[1]}\n"
end
result[0] == "ok" ? 0 : 1
end
def cmd_state(args)
full_mesg_id = args['msg_id']
raise RQ::RqMissingArgument if not full_mesg_id
q_name = full_mesg_id[/\/q\/([^\/]+)/, 1]
msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1]
qc = get_queue_client(q_name)
# Construct message for queue mgr
mesg = {'msg_id' => msg_id }
result = qc.get_message_state(mesg)
if result[0] == 'ok'
print "#{result[0]} #{result[1]}\n"
else
print "#{result[0]} #{result[1]}\n"
end
result[0] == "ok" ? 0 : 1
end
def cmd_statuscountmesg(args)
full_mesg_id = args['msg_id']
raise RQ::RqMissingArgument if not full_mesg_id
q_name = full_mesg_id[/\/q\/([^\/]+)/, 1]
msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1]
qc = get_queue_client(q_name)
# Construct message for queue mgr
mesg = {'msg_id' => msg_id }
result = qc.get_message(mesg)
if result[0] == 'ok'
print "#{result[0]} #{result[1].fetch('count', '0')}\n"
else
print "#{result[0]} #{result[1]}\n"
end
result[0] == "ok" ? 0 : 1
end
def cmd_single_que(args)
q_name = args['dest']
raise RQ::RqMissingArgument if not q_name
qc = get_queue_client(q_name)
# Construct message
mesg = {}
keys = %w(dest src count max_count param1 param2 param3 param4 due force_remote)
keys.each do |key|
next unless args.has_key?(key)
mesg[key] = args[key]
end
result = qc.single_que(mesg)
print "#{result[0]} #{result[1]}\n"
result[0] == "ok" ? 0 : 1
end
def cmd_attachstatusmesg(args)
full_mesg_id = args['msg_id']
raise RQ::RqMissingArgument if not full_mesg_id
q_name = full_mesg_id[/\/q\/([^\/]+)/, 1]
msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1]
qc = get_queue_client(q_name)
# Construct message for queue mgr
mesg = {'msg_id' => msg_id }
result = qc.get_message(mesg)
if result[0] == 'ok'
ents = []
if result[1].has_key?('_attachments')
result[1]['_attachments'].each do |k,v|
ents << [k, v['md5'], v['size'], v['path']]
end
end
print "#{result[0]} #{ents.length}\n"
ents.each do |ent|
print "#{ent[0]} #{ent[1]} #{ent[2]} #{ent[3]}\n"
end
else
print "#{result[0]} #{result[1]}\n"
end
result[0] == "ok" ? 0 : 1
end
def cmd_clone(args)
full_mesg_id = args['msg_id']
raise RQ::RqMissingArgument if not full_mesg_id
q_name = full_mesg_id[/\/q\/([^\/]+)/, 1]
msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1]
qc = get_queue_client(q_name)
mesg = {'msg_id' => msg_id }
result = qc.clone_message(mesg)
print "#{result[0]} #{result[1]}\n"
result[0] == "ok" ? 0 : 1
end
def cmd_verify_rules(args)
raise RQ::RqMissingArgument if not args['path']
rp = RQ::RuleProcessor.process_pathname(args['path'], args['verbose'])
if rp == nil
puts "fail bad router rules file"
return 1
else
if args['verbose']
rp.rules.each {|o| p o}
end
puts "ok"
return 0
end
end
end
# BEGIN THINGS THAT RUN
args = check_usage(ARGV) || process_args(ARGV)
retval = 0
cmd = "cmd_#{args[:cmd]}"
cmds = Commands.new
if cmds.respond_to? cmd
retval = cmds.send cmd, args
else
warn "Command not found: #{args[:cmd]} (#{cmd})"
retval = 1
end
exit retval