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

[Receiving] Customizable Job configuration which is based on the EventHandler DSL/option #27

Merged
merged 7 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog
All notable changes to this project will be documented in this file.

## [1.0.1] - 2024-12-06
### Added
- **Receiving**
- Support for custom receiving job config which is placed in receiving event handler;

## [1.0.0] - 2024-10-23
### Changed
- Remove `sneakers` gem in favour of [kicks](https://github.com/ruby-amqp/kicks)
Expand Down
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ GEM
rails (>= 4.2)
language_server-protocol (3.17.0.3)
logger (1.6.1)
loofah (2.22.0)
loofah (2.23.1)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mail (2.8.1)
Expand All @@ -135,7 +135,7 @@ GEM
marcel (1.0.4)
method_source (1.1.0)
mini_mime (1.1.5)
mini_portile2 (2.8.7)
mini_portile2 (2.8.8)
minitest (5.25.1)
mutex_m (0.2.0)
net-imap (0.4.17)
Expand All @@ -148,7 +148,7 @@ GEM
net-smtp (0.5.0)
net-protocol
nio4r (2.7.3)
nokogiri (1.16.7)
nokogiri (1.16.8)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
parallel (1.26.3)
Expand Down Expand Up @@ -187,9 +187,9 @@ GEM
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
rails-html-sanitizer (1.6.1)
loofah (~> 2.21)
nokogiri (~> 1.14)
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
railties (7.1.4.1)
actionpack (= 7.1.4.1)
activesupport (= 7.1.4.1)
Expand Down Expand Up @@ -306,4 +306,4 @@ DEPENDENCIES
simplecov-lcov

BUNDLED WITH
2.3.20
2.5.22
9 changes: 9 additions & 0 deletions lib/rabbit/event_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ class Rabbit::EventHandler

class_attribute :queue
class_attribute :ignore_queue_conversion, default: false
class_attribute :additional_job_configs, default: {}

class << self
private

def queue_as(queue = nil, &block)
self.queue = queue || block
end

def job_config(**config_opts)
additional_job_configs.merge!(config_opts)
end

def job_configs(**config_opts)
self.additional_job_configs = config_opts
end
end

def initialize(message)
Expand Down
12 changes: 10 additions & 2 deletions lib/rabbit/receiving/receive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def log!

def process_message
job_class
.set(queue: queue)
.set(queue: queue_name, **job_configs)
.perform_later(message, message_info)
end

Expand All @@ -53,7 +53,15 @@ def message_info
end

def queue
Rabbit::Receiving::Queue.new(message, arguments).name
@queue ||= Rabbit::Receiving::Queue.new(message, arguments)
end

def job_configs
queue.handler.additional_job_configs
end

def queue_name
queue.name
end

def job_class
Expand Down
16 changes: 14 additions & 2 deletions spec/units/rabbit/receiving_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
let(:arguments) { { type: event, app_id: "some_group.some_app", message_id: "uuid" } }
let(:event) { "some_successful_event" }
let(:job_class) { Rabbit::Receiving::Job }
let(:job_configs) { {} }
let(:conversion) { false }
let(:handler) { Rabbit::Handler::SomeGroup::SomeSuccessfulEvent }
let(:before_hook) { double("before hook") }
let(:after_hook) { double("after hook") }
let(:message_info) { arguments.merge(delivery_info.slice(:exchange, :routing_key)) }

def expect_job_queue_to_be_set
expect(job_class).to receive(:set).with(queue: queue)
expect(job_class).to receive(:set).with(queue: queue, **job_configs)
end

def expect_some_handler_to_be_called
Expand Down Expand Up @@ -52,7 +53,7 @@ def expect_hooks_to_be_called
Rabbit.config.before_receiving_hooks = [before_hook]
Rabbit.config.after_receiving_hooks = [after_hook]

allow(job_class).to receive(:set).with(queue: queue).and_call_original
allow(job_class).to receive(:set).with(queue: queue, **job_configs).and_call_original

allow(before_hook).to receive(:call).with(message, message_info)
allow(after_hook).to receive(:call).with(message, message_info)
Expand Down Expand Up @@ -134,6 +135,17 @@ def call; end
run_receive
end

context "custom job configuration" do
let(:job_configs) { Hash[some: :kek, pek: 123] }

before { handler.additional_job_configs = job_configs }
after { handler.additional_job_configs = {} }

it "invokes job with custom config" do
run_receive
end
end

context "job performs unsuccessfully" do
let(:event) { "some_unsuccessful_event" }
let(:queue) { "custom_prepared" }
Expand Down
Loading