iron_mq 1.8.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +16 -18
- data/lib/iron_mq/client.rb +9 -1
- data/lib/iron_mq/queues.rb +24 -8
- data/lib/iron_mq/version.rb +1 -1
- data/test/test_iron_mq.rb +98 -54
- metadata +1 -1
data/README.md
CHANGED
@@ -4,28 +4,36 @@ IronMQ Ruby Client
|
|
4
4
|
Getting Started
|
5
5
|
==============
|
6
6
|
|
7
|
-
Install the gem:
|
7
|
+
1. Install the gem:
|
8
8
|
|
9
9
|
gem install iron_mq
|
10
10
|
|
11
|
-
|
11
|
+
2. Setup your Iron.io credentials: http://dev.iron.io/articles/configuration/
|
12
12
|
|
13
|
-
|
13
|
+
3. Create an IronMQ client object:
|
14
14
|
|
15
|
-
|
15
|
+
@ironmq = IronMQ::Client.new()
|
16
16
|
|
17
17
|
|
18
18
|
The Basics
|
19
19
|
=========
|
20
20
|
|
21
|
+
**Get a Queue object**
|
22
|
+
|
23
|
+
You can have as many queues as you want, each with their own unique set of messages.
|
24
|
+
|
25
|
+
@queue = @ironmq.queue("my_queue")
|
26
|
+
|
27
|
+
Now you can use it:
|
28
|
+
|
21
29
|
**Push** a message on the queue:
|
22
30
|
|
23
|
-
msg = @
|
31
|
+
msg = @queue.post("hello world!")
|
24
32
|
p msg
|
25
33
|
|
26
34
|
**Pop** a message off the queue:
|
27
35
|
|
28
|
-
msg = @
|
36
|
+
msg = @queue.get()
|
29
37
|
p msg
|
30
38
|
|
31
39
|
When you pop/get a message from the queue, it will NOT be deleted. It will eventually go back onto the queue after
|
@@ -33,24 +41,14 @@ a timeout if you don't delete it (default timeout is 10 minutes).
|
|
33
41
|
|
34
42
|
**Delete** a message from the queue:
|
35
43
|
|
36
|
-
res = msg.delete # or @
|
44
|
+
res = msg.delete # or @queue.delete(msg.id)
|
37
45
|
p res
|
38
46
|
|
39
47
|
Delete a message from the queue when you're done with it.
|
40
48
|
|
41
|
-
Queue Selection
|
42
|
-
===============
|
43
|
-
|
44
|
-
One of the following:
|
45
|
-
|
46
|
-
1. Pass `:queue_name=>'my_queue'` into IronMQ::Client.new
|
47
|
-
1. `@client.queue_name = 'my_queue'`
|
48
|
-
1. Pass `:queue_name=>'my_queue'` into any post(), get(), or delete()
|
49
|
-
|
50
49
|
Queue Information
|
51
50
|
=================
|
52
51
|
|
53
|
-
queue = @client.
|
52
|
+
queue = @client.queue("my_queue")
|
54
53
|
puts "size: #{queue.size}"
|
55
54
|
|
56
|
-
|
data/lib/iron_mq/client.rb
CHANGED
@@ -7,11 +7,14 @@ module IronMQ
|
|
7
7
|
class Client < IronCore::Client
|
8
8
|
AWS_US_EAST_HOST = 'mq-aws-us-east-1.iron.io'
|
9
9
|
|
10
|
-
attr_accessor :queue_name
|
10
|
+
attr_accessor :queue_name, :logger
|
11
11
|
|
12
12
|
def initialize(options={})
|
13
13
|
super('mq', options, [:queue_name])
|
14
14
|
|
15
|
+
@logger = Logger.new(STDOUT)
|
16
|
+
@logger.level = Logger::INFO
|
17
|
+
|
15
18
|
load_from_hash('defaults', {:scheme => 'https', :host => IronMQ::Client::AWS_US_EAST_HOST, :port => 443,
|
16
19
|
:api_version => 1,
|
17
20
|
:user_agent => 'iron_mq_ruby-' + IronMQ::VERSION + ' (iron_core_ruby-' + IronCore.version + ')',
|
@@ -23,6 +26,11 @@ module IronMQ
|
|
23
26
|
end
|
24
27
|
end
|
25
28
|
|
29
|
+
def queue(name)
|
30
|
+
return Queue.new(self, {"name"=>name})
|
31
|
+
end
|
32
|
+
|
33
|
+
|
26
34
|
def messages
|
27
35
|
return Messages.new(self)
|
28
36
|
end
|
data/lib/iron_mq/queues.rb
CHANGED
@@ -10,7 +10,7 @@ module IronMQ
|
|
10
10
|
def path(options={})
|
11
11
|
path = "projects/#{@client.project_id}/queues"
|
12
12
|
if options[:name]
|
13
|
-
path << "/#{options[:name]}"
|
13
|
+
path << "/#{URI.escape(options[:name])}"
|
14
14
|
end
|
15
15
|
path
|
16
16
|
end
|
@@ -53,8 +53,8 @@ module IronMQ
|
|
53
53
|
|
54
54
|
class Queue
|
55
55
|
|
56
|
-
def initialize(
|
57
|
-
@
|
56
|
+
def initialize(client, res)
|
57
|
+
@client = client
|
58
58
|
@data = res
|
59
59
|
end
|
60
60
|
|
@@ -74,10 +74,18 @@ module IronMQ
|
|
74
74
|
raw["name"]
|
75
75
|
end
|
76
76
|
|
77
|
+
# Used if lazy loading
|
78
|
+
def load_queue
|
79
|
+
q = @client.queues.get(:name=>name)
|
80
|
+
@client.logger.debug "GOT Q: " + q.inspect
|
81
|
+
@data = q.raw
|
82
|
+
q
|
83
|
+
end
|
84
|
+
|
77
85
|
def size
|
78
86
|
return raw["size"] if raw["size"]
|
79
87
|
return @size if @size
|
80
|
-
q =
|
88
|
+
q = load_queue()
|
81
89
|
@size = q.size
|
82
90
|
@size
|
83
91
|
end
|
@@ -85,14 +93,22 @@ module IronMQ
|
|
85
93
|
def total_messages
|
86
94
|
return raw["total_messages"] if raw["total_messages"]
|
87
95
|
return @total_messages if @total_messages
|
88
|
-
q =
|
96
|
+
q = load_queue()
|
89
97
|
@total_messages = q.total_messages
|
90
98
|
@total_messages
|
91
99
|
end
|
92
100
|
|
93
|
-
|
94
|
-
|
95
|
-
|
101
|
+
def post(body, options={})
|
102
|
+
@client.messages.post(body, options.merge(:queue_name=>name))
|
103
|
+
end
|
104
|
+
|
105
|
+
def get(options={})
|
106
|
+
@client.messages.get(options.merge(:queue_name=>name))
|
107
|
+
end
|
108
|
+
|
109
|
+
def delete(id, options={})
|
110
|
+
@client.messages.delete(id, options.merge(:queue_name=>name))
|
111
|
+
end
|
96
112
|
end
|
97
113
|
|
98
114
|
end
|
data/lib/iron_mq/version.rb
CHANGED
data/test/test_iron_mq.rb
CHANGED
@@ -71,6 +71,44 @@ class IronMQTests < TestBase
|
|
71
71
|
res = @client.messages.get()
|
72
72
|
p res
|
73
73
|
assert res.nil?
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
# new style of referencing queue
|
78
|
+
queue = @client.queue("test_basics")
|
79
|
+
v = "hello big world"
|
80
|
+
res = queue.post(v)
|
81
|
+
p res
|
82
|
+
assert res.msg
|
83
|
+
|
84
|
+
res = queue.get()
|
85
|
+
p res
|
86
|
+
assert res["id"]
|
87
|
+
assert res.id
|
88
|
+
assert res.body == v
|
89
|
+
|
90
|
+
res = queue.delete(res.id)
|
91
|
+
p res
|
92
|
+
puts "shouldn't be any more"
|
93
|
+
res = queue.get()
|
94
|
+
p res
|
95
|
+
assert res.nil?
|
96
|
+
|
97
|
+
# test delete by item
|
98
|
+
res = queue.post(v)
|
99
|
+
p res
|
100
|
+
assert res.msg
|
101
|
+
|
102
|
+
res = queue.get()
|
103
|
+
p res
|
104
|
+
assert res.body
|
105
|
+
res = res.delete
|
106
|
+
p res
|
107
|
+
puts "shouldn't be any more"
|
108
|
+
res = queue.get()
|
109
|
+
p res
|
110
|
+
assert res.nil?
|
111
|
+
|
74
112
|
end
|
75
113
|
|
76
114
|
# TODO: pass :timeout in post/get messages and test those
|
@@ -147,6 +185,11 @@ class IronMQTests < TestBase
|
|
147
185
|
assert res.size == 0
|
148
186
|
|
149
187
|
|
188
|
+
queue = @client.queue("test_basics")
|
189
|
+
assert queue.name
|
190
|
+
assert queue.size
|
191
|
+
|
192
|
+
|
150
193
|
end
|
151
194
|
|
152
195
|
def test_delay
|
@@ -194,60 +237,61 @@ class IronMQTests < TestBase
|
|
194
237
|
end
|
195
238
|
end
|
196
239
|
|
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
|
-
|
240
|
+
# PUT THIS BACK IN WHEN READY
|
241
|
+
#def test_release
|
242
|
+
# puts 'test_release'
|
243
|
+
# @client.queue_name = "test_release"
|
244
|
+
# clear_queue
|
245
|
+
# msgTxt = "testMessage-"+Time.now.to_s
|
246
|
+
# puts msgTxt
|
247
|
+
# msg_id = @client.messages.post(msgTxt, {:timeout => 3600}).id
|
248
|
+
# puts "msg_id: #{msg_id}"
|
249
|
+
# msg = @client.messages.get
|
250
|
+
# p msg
|
251
|
+
# assert msg.id == msg_id
|
252
|
+
# # Ok, so should have received same message, now let's release it quicker than the original timeout
|
253
|
+
#
|
254
|
+
# # but first, ensure the next get is nil
|
255
|
+
# msg = @client.messages.get
|
256
|
+
# p msg
|
257
|
+
# assert msg.nil?
|
258
|
+
#
|
259
|
+
# # now release it instantly
|
260
|
+
# @client.messages.release(msg_id)
|
261
|
+
# msg = @client.messages.get
|
262
|
+
# p msg
|
263
|
+
# assert msg
|
264
|
+
# assert msg.id == msg_id
|
265
|
+
#
|
266
|
+
# # ok, so should be reserved again
|
267
|
+
# msgr = @client.messages.get
|
268
|
+
# p msgr
|
269
|
+
# assert msgr.nil?
|
270
|
+
#
|
271
|
+
# # let's release it in 10 seconds
|
272
|
+
# @client.messages.release(msg_id, :delay=>10)
|
273
|
+
# msg = @client.messages.get
|
274
|
+
# p msg
|
275
|
+
# assert msg.nil?
|
276
|
+
#
|
277
|
+
# sleep 11
|
278
|
+
#
|
279
|
+
# msg = @client.messages.get
|
280
|
+
# p msg
|
281
|
+
# assert msg
|
282
|
+
#
|
283
|
+
# msg.release(:delay => 5)
|
284
|
+
# msg = @client.messages.get
|
285
|
+
# p msg
|
286
|
+
# assert msg.nil?
|
287
|
+
#
|
288
|
+
# sleep 6
|
289
|
+
#
|
290
|
+
# msg = @client.messages.get
|
291
|
+
# p msg
|
292
|
+
# assert msg
|
293
|
+
#
|
294
|
+
#end
|
251
295
|
|
252
296
|
end
|
253
297
|
|