phobos 1.9.0.pre.beta1 → 1.9.0.pre.beta2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ac51b2a5d3cdaa995dc2f8dfca3d8597c225c0b311724655712bb1ba7060991
4
- data.tar.gz: d58bf202dced7bf24c483b0cd2067e4e13e2a43e8f1cfa756377ccf1cf4861dc
3
+ metadata.gz: f2b2f9eeb44e40d9cb01add121e7b388c66e99f5482e5f2ec78bb7bc3263bd32
4
+ data.tar.gz: 796484971553a2b19edc18f0e4192ffaece1de0c27894bfa0344b0af29fc3f23
5
5
  SHA512:
6
- metadata.gz: 4b41e84f671b6f59d94bced995a491818468b1932fe22309e55bff1236d5f165c624d65647bc24ce054f5ab41e8ed20a53ffdb04ab21d755a3fa46cc52e9ba24
7
- data.tar.gz: 69651f14500c3c0126b55429f0c5fd8bc2118845a6eac07147cd4c7cd87aa12d7f101a79f250cd428747c322ad469121af8a10fe94af3bc1f5fa67c70d0666a6
6
+ metadata.gz: 0f2f422bff9b56354c1378c283090277a1af164b2b8a3f9a038c85a25157131e1e2cd005d37e72ba21d921620521210020859368b42851dc110043395052ab11
7
+ data.tar.gz: dbd23e7210111e63921854bf279c0f64aa9a4ba426f24af897924a1c6a76cb24a19be4c35db1105013c0d0de3c6a6e2bb73272df6cce9b24e4dce9c9a445becf
data/CHANGELOG.md CHANGED
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
6
6
  ``
7
7
  ## UNRELEASED
8
8
 
9
+ ## [1.9.0-beta2] - 2020-01-09
10
+
11
+ - Allow `around_consume` to yield payload and metadata, and deprecate
12
+ `before_consume` and `around_consume` that does not yield anything.
13
+
9
14
  ## [1.9.0-beta1] - 2019-12-18
10
15
  - Update `publish` and `async_publish` to use keyword arguments
11
16
  instead of positional ones. Deprecate positional arguments
data/README.md CHANGED
@@ -169,7 +169,7 @@ class MyHandler
169
169
 
170
170
  def around_consume(payload, metadata)
171
171
  Phobos.logger.info "consuming..."
172
- output = yield
172
+ output = yield payload, metadata
173
173
  Phobos.logger.info "done, output: #{output}"
174
174
  end
175
175
 
@@ -187,7 +187,7 @@ class MyHandler
187
187
 
188
188
  def self.around_consume(payload, metadata)
189
189
  Phobos.logger.info "consuming..."
190
- output = yield
190
+ output = yield payload, metadata
191
191
  Phobos.logger.info "done, output: #{output}"
192
192
  end
193
193
 
@@ -197,18 +197,7 @@ class MyHandler
197
197
  end
198
198
  ```
199
199
 
200
- Finally, it is also possible to preprocess the message payload before consuming it using the `before_consume` hook which is invoked before `#around_consume` and `#consume`. The result of this operation will be assigned to payload, so it is important to return the modified payload. This can be very useful, for example if you want a single point of decoding Avro messages and want the payload as a hash instead of a binary.
201
-
202
- ```ruby
203
- class MyHandler
204
- include Phobos::Handler
205
-
206
- def before_consume(payload, metadata)
207
- # optionally preprocess payload
208
- payload
209
- end
210
- end
211
- ```
200
+ Note: Previous versions used a `before_consume` method to pre-process the payload. This is still supported, but deprecated. Going forward, `around_consume` should yield the payload and metadata back to the calling code, allowing it to act as a pre-processor, e.g. by decoding Avro messages into Ruby hashes.
212
201
 
213
202
  Take a look at the examples folder for some ideas.
214
203
 
@@ -218,7 +207,7 @@ The hander life cycle can be illustrated as:
218
207
 
219
208
  or optionally,
220
209
 
221
- `.start` -> `#before_consume` -> `#around_consume` [ `#consume` ] -> `.stop`
210
+ `.start` -> `#around_consume` [ `#consume` ] -> `.stop`
222
211
 
223
212
  #### Batch Consumption
224
213
 
@@ -242,14 +231,12 @@ instance method `headers` with the headers for that message.
242
231
  class MyBatchHandler
243
232
  include Phobos::BatchHandler
244
233
 
245
- def before_consume_batch(payloads, metadata)
234
+ def around_consume_batch(payloads, metadata)
246
235
  payloads.each do |p|
247
236
  p.payload[:timestamp] = Time.zone.now
248
237
  end
249
- end
250
238
 
251
- def around_consume_batch(payloads, metadata)
252
- yield
239
+ yield payloads, metadata
253
240
  end
254
241
 
255
242
  def consume_batch(payloads, metadata)
@@ -625,7 +612,6 @@ describe MyConsumer do
625
612
 
626
613
  it 'consumes my message' do
627
614
  expect_any_instance_of(described_class).to receive(:around_consume).with(payload, metadata).once.and_call_original
628
- expect_any_instance_of(described_class).to receive(:before_consume).with(payload, metadata).once.and_call_original
629
615
  expect_any_instance_of(described_class).to receive(:consume).with(payload, metadata).once.and_call_original
630
616
 
631
617
  process_message(handler: described_class, payload: payload, metadata: metadata)
@@ -47,12 +47,30 @@ module Phobos
47
47
  )
48
48
  end
49
49
 
50
+ def preprocess(batch, handler)
51
+ if handler.respond_to?(:before_consume_batch)
52
+ Phobos.deprecate('before_consume_batch is deprecated and will be removed in 2.0. \
53
+ Use around_consume_batch and yield payloads and metadata objects.')
54
+ handler.before_consume_batch(batch, @metadata)
55
+ else
56
+ batch
57
+ end
58
+ end
59
+
50
60
  def process_batch(batch)
51
61
  instrument('listener.process_batch_inline', @metadata) do |_metadata|
52
62
  handler = @listener.handler_class.new
53
63
 
54
- preprocessed_batch = handler.before_consume_batch(batch, @metadata)
55
- consume_block = proc { handler.consume_batch(preprocessed_batch, @metadata) }
64
+ preprocessed_batch = preprocess(batch, handler)
65
+ consume_block = proc { |around_batch, around_metadata|
66
+ if around_batch
67
+ handler.consume_batch(around_batch, around_metadata)
68
+ else
69
+ Phobos.deprecate('Calling around_consume_batch without yielding payloads \
70
+ and metadata is deprecated and will be removed in 2.0.')
71
+ handler.consume_batch(preprocessed_batch, @metadata)
72
+ end
73
+ }
56
74
 
57
75
  handler.around_consume_batch(preprocessed_batch, @metadata, &consume_block)
58
76
  end
@@ -35,33 +35,50 @@ module Phobos
35
35
 
36
36
  private
37
37
 
38
+ def preprocess(payload, handler)
39
+ if handler.respond_to?(:before_consume)
40
+ Phobos.deprecate('before_consume is deprecated and will be removed in 2.0. \
41
+ Use around_consume and yield payload and metadata objects.')
42
+ begin
43
+ handler.before_consume(payload, @metadata)
44
+ rescue ArgumentError
45
+ handler.before_consume(payload)
46
+ end
47
+ else
48
+ payload
49
+ end
50
+ end
51
+
52
+ def consume_block(payload, handler)
53
+ proc { |around_payload, around_metadata|
54
+ if around_payload
55
+ handler.consume(around_payload, around_metadata)
56
+ else
57
+ Phobos.deprecate('Calling around_consume without yielding payload and metadata \
58
+ is deprecated and will be removed in 2.0.')
59
+ handler.consume(payload, @metadata)
60
+ end
61
+ }
62
+ end
63
+
38
64
  def process_message(payload)
39
65
  instrument('listener.process_message', @metadata) do
40
66
  handler = @listener.handler_class.new
41
67
 
42
- preprocessed_payload = before_consume(handler, payload)
43
- consume_block = proc { handler.consume(preprocessed_payload, @metadata) }
68
+ preprocessed_payload = preprocess(payload, handler)
69
+ block = consume_block(preprocessed_payload, handler)
44
70
 
45
71
  if @listener.handler_class.respond_to?(:around_consume)
46
72
  # around_consume class method implementation
47
73
  Phobos.deprecate('around_consume has been moved to instance method, please update '\
48
74
  'your consumer. This will not be backwards compatible in the future.')
49
- @listener.handler_class.around_consume(preprocessed_payload, @metadata, &consume_block)
75
+ @listener.handler_class.around_consume(preprocessed_payload, @metadata, &block)
50
76
  else
51
77
  # around_consume instance method implementation
52
- handler.around_consume(preprocessed_payload, @metadata, &consume_block)
78
+ handler.around_consume(preprocessed_payload, @metadata, &block)
53
79
  end
54
80
  end
55
81
  end
56
-
57
- def before_consume(handler, payload)
58
- handler.before_consume(payload, @metadata)
59
- rescue ArgumentError
60
- Phobos.deprecate('before_consume now expects metadata as second argument, please update '\
61
- 'your consumer. This will not be backwards compatible in the future.')
62
-
63
- handler.before_consume(payload)
64
- end
65
82
  end
66
83
  end
67
84
  end
@@ -6,16 +6,12 @@ module Phobos
6
6
  base.extend(ClassMethods)
7
7
  end
8
8
 
9
- def before_consume_batch(payloads, _metadata)
10
- payloads
11
- end
12
-
13
9
  def consume_batch(_payloads, _metadata)
14
10
  raise NotImplementedError
15
11
  end
16
12
 
17
- def around_consume_batch(_payloads, _metadata)
18
- yield
13
+ def around_consume_batch(payloads, metadata)
14
+ yield payloads, metadata
19
15
  end
20
16
 
21
17
  module ClassMethods
@@ -6,16 +6,12 @@ module Phobos
6
6
  base.extend(ClassMethods)
7
7
  end
8
8
 
9
- def before_consume(payload, _metadata)
10
- payload
11
- end
12
-
13
9
  def consume(_payload, _metadata)
14
10
  raise NotImplementedError
15
11
  end
16
12
 
17
- def around_consume(_payload, _metadata)
18
- yield
13
+ def around_consume(payload, metadata)
14
+ yield payload, metadata
19
15
  end
20
16
 
21
17
  module ClassMethods
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Phobos
4
- VERSION = '1.9.0-beta1'
4
+ VERSION = '1.9.0-beta2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phobos
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0.pre.beta1
4
+ version: 1.9.0.pre.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Túlio Ornelas
@@ -15,7 +15,7 @@ authors:
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
- date: 2019-12-18 00:00:00.000000000 Z
18
+ date: 2020-01-09 00:00:00.000000000 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: bundler