Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CCOL-2039: Preprocess message before batch consumption #206

Merged
merged 4 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/deimos/active_record_consume/batch_consumption.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,17 @@ def upsert_records(messages)
updater.mass_update(record_list)
end

# Process messages prior to saving to datbase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo, "database"

# @param _messages [Array<Deimos::Message>]
# @return [Void]
def pre_process(_messages)
nil
end

# @param messages [Array<Deimos::Message>]
# @return [BatchRecordList]
def build_records(messages)
pre_process(messages)
records = messages.map do |m|
attrs = if self.method(:record_attributes).parameters.size == 2
record_attributes(m.payload, m.key)
Expand Down
41 changes: 41 additions & 0 deletions spec/active_record_batch_consumer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -493,5 +493,46 @@ def record_attributes(payload, key)
end
end

describe 'pre processing' do
context 'with uncompacted messages' do
let(:consumer_class) do
Class.new(described_class) do
schema 'MySchema'
namespace 'com.my-namespace'
key_config plain: true
record_class Widget
compacted false

def pre_process(messages)
messages.each do |message|
message.payload[:some_int] = -message.payload[:some_int]
end
end

end
end

it 'should pre-process records' do
Widget.create!(id: 1, test_id: 'abc', some_int: 1)
Widget.create!(id: 2, test_id: 'def', some_int: 2)

publish_batch(
[
{ key: 1,
payload: { test_id: 'abc', some_int: 11 } },
{ key: 2,
payload: { test_id: 'def', some_int: 20 } }
]
)

widget_one, widget_two = Widget.all.to_a

expect(widget_one.some_int).to eq(-11)
expect(widget_two.some_int).to eq(-20)
end
end

end

end
end
Loading