queue_worker 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/queue_worker/version.rb +1 -1
- data/lib/queue_worker.rb +14 -7
- data/spec/lib/queue_worker_spec.rb +12 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4466bc827053b6069d1c0f4dddf73741cd3126f
|
4
|
+
data.tar.gz: 9312fcbebf8668f47d861f85cb00e2bd09ac4bad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a389c2029029ab522d75c0ab5df65e7dc555e0e7cad2a4eeb40b8ac49570b8ed5dd2bd401633c8c02de2cc94f8ce63bac1f62cfb7f6d19fcf5be1304deb38244
|
7
|
+
data.tar.gz: 6c2aa1abf124b00b9ecbe98d90c3f78bdf5c1cdfdcfa2eca28ace8c215a349abba637adba6f929301674f5602fe9770babbfcf40c3152e6fe500fcfe8a4498d9
|
data/lib/queue_worker/version.rb
CHANGED
data/lib/queue_worker.rb
CHANGED
@@ -30,7 +30,7 @@ class QueueWorker
|
|
30
30
|
@handler = block || proc { |body| Kernel.const_get(body[:class]).call(body[:args]) }
|
31
31
|
end
|
32
32
|
|
33
|
-
#
|
33
|
+
# Publish one or more messages to a queue
|
34
34
|
#
|
35
35
|
# @param [String] queue name
|
36
36
|
# @param [Array] messages a list of objects that are or can be converted to JSON
|
@@ -40,7 +40,7 @@ class QueueWorker
|
|
40
40
|
worker.close
|
41
41
|
end
|
42
42
|
|
43
|
-
#
|
43
|
+
# Peek at any number messages in the queue
|
44
44
|
#
|
45
45
|
# @param [String] queue_name
|
46
46
|
# @param [Integer] size specify the number of messages to return
|
@@ -56,6 +56,13 @@ class QueueWorker
|
|
56
56
|
messages
|
57
57
|
end
|
58
58
|
|
59
|
+
# Start a subscription worker with the given args
|
60
|
+
def self.subscribe(*args, &block)
|
61
|
+
worker = new(*args, &block)
|
62
|
+
worker.subscribe
|
63
|
+
worker.join
|
64
|
+
end
|
65
|
+
|
59
66
|
# = Publish a message to a queue
|
60
67
|
#
|
61
68
|
# @param [Hash] message - Data to serialize
|
@@ -67,7 +74,7 @@ class QueueWorker
|
|
67
74
|
|
68
75
|
alias push publish
|
69
76
|
|
70
|
-
#
|
77
|
+
# Subscribe (listen) to a queue
|
71
78
|
#
|
72
79
|
# @param [String] queue_name specify the queue name
|
73
80
|
# @param [Integer] size specify the number of messages the block may receive without sending +ack+
|
@@ -77,7 +84,7 @@ class QueueWorker
|
|
77
84
|
client.subscribe("/queue/#{queue_name || queue}", { :ack => 'client', 'activemq.prefetchSize' => size }, &callback)
|
78
85
|
end
|
79
86
|
|
80
|
-
#
|
87
|
+
# Subscribe to a queue for a limited time
|
81
88
|
#
|
82
89
|
# @param [Integer] duration to subscribe for before closing connection
|
83
90
|
# @param [Integer] size specify the number of messages the block may receive without sending +ack+
|
@@ -91,18 +98,18 @@ class QueueWorker
|
|
91
98
|
quit
|
92
99
|
end
|
93
100
|
|
94
|
-
#
|
101
|
+
# Unsubscribe from the current queue
|
95
102
|
def unsubscribe(queue_name = nil)
|
96
103
|
client.unsubscribe("/queue/#{queue_name || queue}")
|
97
104
|
end
|
98
105
|
|
99
|
-
#
|
106
|
+
# Unsubscribe from the current queue and close the connection
|
100
107
|
def quit(queue_name = nil)
|
101
108
|
unsubscribe(queue_name)
|
102
109
|
close
|
103
110
|
end
|
104
111
|
|
105
|
-
#
|
112
|
+
# Handles +subscribe+ callback
|
106
113
|
#
|
107
114
|
# Tries to delegate processing of message to a class based on the name of the queue. For example:
|
108
115
|
#
|
@@ -3,6 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe QueueWorker, slow: true do
|
4
4
|
let(:queue_name) { 'queue_foo' }
|
5
5
|
let(:log) { Logger.new(STDOUT).tap { |x| x.level = Logger::ERROR } }
|
6
|
+
let(:message) { Struct.new(:command, :body).new('MESSAGE', '{}') }
|
6
7
|
|
7
8
|
subject { described_class.new(queue_name, log) }
|
8
9
|
|
@@ -20,6 +21,17 @@ describe QueueWorker, slow: true do
|
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
24
|
+
describe '.subscribe' do
|
25
|
+
let(:handler) { proc {} }
|
26
|
+
|
27
|
+
it 'instantiates and subscribes itself to a given queue' do
|
28
|
+
expect(described_class).to receive(:new).with(queue_name, log, &handler).and_return(subject)
|
29
|
+
expect(subject).to receive(:subscribe)
|
30
|
+
expect(subject).to receive(:join)
|
31
|
+
described_class.subscribe(queue_name, log, &handler)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
23
35
|
describe '#publish' do
|
24
36
|
let(:message) { {} }
|
25
37
|
|
@@ -42,7 +54,6 @@ describe QueueWorker, slow: true do
|
|
42
54
|
describe '#subscribe' do
|
43
55
|
let(:size) { 1 }
|
44
56
|
let(:headers) { { :ack => 'client', 'activemq.prefetchSize' => size } }
|
45
|
-
let(:message) { Struct.new(:command, :body).new('MESSAGE', '{}') }
|
46
57
|
|
47
58
|
it 'pass the -message- to +call+' do
|
48
59
|
expect(subject.client).to receive(:subscribe).with("/queue/#{queue_name}", headers).and_yield(message)
|