queue_worker 2.0.0 → 2.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f626efd2e37e8b59b15ed2493d51a5ebc789bee3
4
- data.tar.gz: 19e29f12f964ca7c94f036ac1d3b35e3fe10a16d
3
+ metadata.gz: c4466bc827053b6069d1c0f4dddf73741cd3126f
4
+ data.tar.gz: 9312fcbebf8668f47d861f85cb00e2bd09ac4bad
5
5
  SHA512:
6
- metadata.gz: be5e1738bcc633ed4de448738b6cfb8b475ca88c002d58947253e31ef9f48759bcf21fc816403193e099ab9f9ac7dc35f9495002593a2ec44e26c2c447dc4f14
7
- data.tar.gz: d1033af1039fcf89cc7347a34b2fa0c88ab555f6090f764d9c9655ac798598fafa9587276fd792588e64f9547acbd55adfecfcb31cd15500f2992a5f750d42f5
6
+ metadata.gz: a389c2029029ab522d75c0ab5df65e7dc555e0e7cad2a4eeb40b8ac49570b8ed5dd2bd401633c8c02de2cc94f8ce63bac1f62cfb7f6d19fcf5be1304deb38244
7
+ data.tar.gz: 6c2aa1abf124b00b9ecbe98d90c3f78bdf5c1cdfdcfa2eca28ace8c215a349abba637adba6f929301674f5602fe9770babbfcf40c3152e6fe500fcfe8a4498d9
@@ -1,3 +1,3 @@
1
1
  class QueueWorker
2
- VERSION = '2.0.0'
2
+ VERSION = '2.1.0'
3
3
  end
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
- # = Publish one or more messages to a queue
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
- # = Peek at any number messages in the queue
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
- # = Subscribe (listen) to a queue
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
- # = Subscribe to a queue for a limited time
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
- # = Unsubscribe from the current queue
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
- # = Unsubscribe from the current queue and close the connection
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
- # = Handles +subscribe+ callback
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: queue_worker
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Buckley