README.md in phobos-1.9.0.pre.beta1 vs README.md in phobos-1.9.0.pre.beta2
- old
+ new
@@ -167,11 +167,11 @@
class MyHandler
include Phobos::Handler
def around_consume(payload, metadata)
Phobos.logger.info "consuming..."
- output = yield
+ output = yield payload, metadata
Phobos.logger.info "done, output: #{output}"
end
def consume(payload, metadata)
# consume or skip message
@@ -185,42 +185,31 @@
class MyHandler
include Phobos::Handler
def self.around_consume(payload, metadata)
Phobos.logger.info "consuming..."
- output = yield
+ output = yield payload, metadata
Phobos.logger.info "done, output: #{output}"
end
def consume(payload, metadata)
# consume or skip message
end
end
```
-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.
+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.
-```ruby
-class MyHandler
- include Phobos::Handler
-
- def before_consume(payload, metadata)
- # optionally preprocess payload
- payload
- end
-end
-```
-
Take a look at the examples folder for some ideas.
The hander life cycle can be illustrated as:
`.start` -> `#consume` -> `.stop`
or optionally,
- `.start` -> `#before_consume` -> `#around_consume` [ `#consume` ] -> `.stop`
+ `.start` -> `#around_consume` [ `#consume` ] -> `.stop`
#### Batch Consumption
In addition to the regular handler, Phobos provides a `BatchHandler`. The
basic ideas are identical, except that instead of being passed a single message
@@ -240,18 +229,16 @@
```ruby
class MyBatchHandler
include Phobos::BatchHandler
- def before_consume_batch(payloads, metadata)
+ def around_consume_batch(payloads, metadata)
payloads.each do |p|
p.payload[:timestamp] = Time.zone.now
end
- end
- def around_consume_batch(payloads, metadata)
- yield
+ yield payloads, metadata
end
def consume_batch(payloads, metadata)
payloads.each do |p|
logger.info("Got payload #{p.payload}, #{p.partition}, #{p.offset}, #{p.key}, #{p.payload[:timestamp]}")
@@ -623,10 +610,9 @@
let(:payload) { 'foo' }
let(:metadata) { Hash(foo: 'bar') }
it 'consumes my message' do
expect_any_instance_of(described_class).to receive(:around_consume).with(payload, metadata).once.and_call_original
- expect_any_instance_of(described_class).to receive(:before_consume).with(payload, metadata).once.and_call_original
expect_any_instance_of(described_class).to receive(:consume).with(payload, metadata).once.and_call_original
process_message(handler: described_class, payload: payload, metadata: metadata)
end
end