ears 0.6.0 → 0.7.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
  SHA256:
3
- metadata.gz: b1bc62c3b6367b99bdd80d44f670f79bd38709587df8e0bbcb72201772b99a74
4
- data.tar.gz: b2fd12a20e5b0879fc1a69efe3729e6a422f8cc20f57007098a4da21d0198b4c
3
+ metadata.gz: d26c2fc5a7c55ba2a002cb8137232e06168e84d79a37b3d4f25f404304ce7cbe
4
+ data.tar.gz: 002ba87fc6d46b3ec4f0b9ed12626b1f3b13d2a41b44685416a329a65d1d20b8
5
5
  SHA512:
6
- metadata.gz: 21299077971e38f928ef72d047726ad99218fc6d4b3c0d73cdd9a179f0cf87d8d5fab6f129a7d4c5dd20d97190d3d1d73daf81f229f3bd79cb21d60a73c9af95
7
- data.tar.gz: 8e92b3f7bba6f494fd563f210dd43f03a95e4df0bf1f722dd1a6a3052fee9c82038679d8e7568a0de5e1e6581cdb343caa6092cc151f63890c2105a0da3b8134
6
+ metadata.gz: 05cae7fcff052959abf699b26d02f5024f2ef00793d0424f98f899efc9ceb898b8188c08b63671239663a9bdd950b359a9abe5697318347b4c4f58b3ee9561d7
7
+ data.tar.gz: 84a75eafa821fba5550bc1a2cc020b49b460473fee2cf0de8bba7b5520e74dca7734505e44996e40515c89b33fe1e7a73dfa6849d48eb67feee31820eab5746a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.0 (2021-12-06)
4
+
5
+ - add options to create retry and error queues
6
+
3
7
  ## 0.6.0 (2021-11-18)
4
8
 
5
9
  - add `connection_name` to configuration and make it a mandatory setting
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ears (0.6.0)
4
+ ears (0.7.0)
5
5
  bunny
6
6
  multi_json
7
7
 
data/README.md CHANGED
@@ -156,6 +156,48 @@ Ears.setup do
156
156
  end
157
157
  ```
158
158
 
159
+ ### Implementing a retrying queue
160
+
161
+ Sometimes you want to automatically retry processing a message, in case it just failed due to temporary problems. In that case, you can set the `retry_queue` and `retry_delay` parameters when creating the queue.
162
+
163
+ ```ruby
164
+ my_queue =
165
+ queue('my_queue', durable: true, retry_queue: true, retry_delay: 5000)
166
+ ```
167
+
168
+ This will automatically create a queue named `my_queue.retry` and use the arguments `x-dead-letter-exchange` and `x-dead-letter-routing-key` to route rejected messages to it. When routed to the retry queue, messages will wait there for the number of milliseconds specified in `retry_delay`, after which they will be redelivered to the original queue.
169
+
170
+ This will happen indefinitely, so if you want to bail out of this cycle at some point, it is best to use the `error_queue` option to create an error queue and then use the `MaxRetries` middleware to route messages to this error queue after a certain amount of retries.
171
+
172
+ ### Implementing an error queue
173
+
174
+ You can set the `error_queue` parameter to automatically create an error queue.
175
+
176
+ ```ruby
177
+ my_queue =
178
+ queue(
179
+ 'my_queue',
180
+ durable: true,
181
+ retry_queue: true,
182
+ retry_delay: 5000,
183
+ error_queue: true,
184
+ )
185
+ ```
186
+
187
+ This will automatically create a queue named `my_queue.error`. It does not have any special properties, the helper's main purpose is to enforce naming conventions. In your consumer, you should then use the `MaxRetries` middleware to route messages to the error queue after a certain amount of retries.
188
+
189
+ ```ruby
190
+ class MyConsumer < Ears::Consumer
191
+ use Ears::Middlewares::MaxRetries, retries: 3, error_queue: 'my_queue.error'
192
+
193
+ def work(delivery_info, metadata, payload)
194
+ # ...
195
+ end
196
+ end
197
+ ```
198
+
199
+ This will automatically route messages to `my_queue.error` after they have been re-tried three times. This prevents you from infinitely retrying a faulty message.
200
+
159
201
  ## Documentation
160
202
 
161
203
  If you need more in-depth information, look at [our API documentation](https://www.rubydoc.info/gems/ears).
data/lib/ears/setup.rb CHANGED
@@ -5,6 +5,8 @@ require 'ears/consumer_wrapper'
5
5
  module Ears
6
6
  # Contains methods used in {Ears.setup} to set up your exchanges, queues and consumers.
7
7
  class Setup
8
+ QUEUE_PARAMS = %i[retry_queue retry_delay error_queue]
9
+
8
10
  # Creates a new exchange if it does not already exist.
9
11
  #
10
12
  # @param [String] name The name of the exchange.
@@ -19,9 +21,22 @@ module Ears
19
21
  #
20
22
  # @param [String] name The name of the queue.
21
23
  # @param [Hash] opts The options for the queue. These are passed on to +Bunny::Exchange.new+.
24
+ # @option args [Boolean] :retry_queue (false) Whether a retry queue should be created. The retry queue is configured as a dead-letter-exchange of the original queue automatically. The name of the queue will be the given name suffixed with ".retry".
25
+ # @option args [Integer] :retry_delay (5000) How long (in ms) a retried message is delayed before being routed back to the original queue.
26
+ # @option args [Boolean] :error_queue (false) Whether an error queue should be created. The name of the queue will be the given name suffixed with ".error".
22
27
  # @return [Bunny::Queue] The queue that was either newly created or was already there.
23
28
  def queue(name, opts = {})
24
- Bunny::Queue.new(Ears.channel, name, opts)
29
+ bunny_opts = opts.reject { |k, _| QUEUE_PARAMS.include?(k) }
30
+ retry_delay = opts.fetch(:retry_delay, 5000)
31
+
32
+ create_retry_queue(name, retry_delay, bunny_opts) if opts[:retry_queue]
33
+ create_error_queue(name, bunny_opts) if opts[:error_queue]
34
+
35
+ Bunny::Queue.new(
36
+ Ears.channel,
37
+ name,
38
+ bunny_opts.merge(retry_opts(name, opts)),
39
+ )
25
40
  end
26
41
 
27
42
  # Creates and starts one or many consumers bound to the given queue.
@@ -46,6 +61,19 @@ module Ears
46
61
 
47
62
  private
48
63
 
64
+ def retry_opts(name, opts)
65
+ if opts[:retry_queue]
66
+ {
67
+ arguments: {
68
+ 'x-dead-letter-exchange' => '',
69
+ 'x-dead-letter-routing-key' => "#{name}.retry",
70
+ },
71
+ }
72
+ else
73
+ {}
74
+ end
75
+ end
76
+
49
77
  def create_consumer(queue, consumer_class, args, number)
50
78
  ConsumerWrapper.new(
51
79
  consumer_class.new,
@@ -69,5 +97,27 @@ module Ears
69
97
  def create_consumer_queue(queue, args)
70
98
  Bunny::Queue.new(create_consumer_channel(args), queue.name, queue.options)
71
99
  end
100
+
101
+ def create_retry_queue(name, delay, opts)
102
+ Bunny::Queue.new(
103
+ Ears.channel,
104
+ "#{name}.retry",
105
+ opts.merge(retry_queue_opts(name, delay)),
106
+ )
107
+ end
108
+
109
+ def retry_queue_opts(name, delay)
110
+ {
111
+ arguments: {
112
+ 'x-message-ttl' => delay,
113
+ 'x-dead-letter-exchange' => '',
114
+ 'x-dead-letter-routing-key' => name,
115
+ },
116
+ }
117
+ end
118
+
119
+ def create_error_queue(name, opts)
120
+ Bunny::Queue.new(Ears.channel, "#{name}.error", opts)
121
+ end
72
122
  end
73
123
  end
data/lib/ears/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ears
2
- VERSION = '0.6.0'
2
+ VERSION = '0.7.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ears
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Mainz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-18 00:00:00.000000000 Z
11
+ date: 2021-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny