Skip to content

Commit add75c9

Browse files
0expAS-AlStar
andauthored
[Receiving] Customizable Job configuration which is based on the EventHandler DSL/option (#27)
* customizable job configuration * DSL * queue name * cop * specs - start to spec * fix specs * bump rails-html-sanitizer --------- Co-authored-by: AS-AlStar <[email protected]>
1 parent de2b8aa commit add75c9

File tree

5 files changed

+44
-10
lines changed

5 files changed

+44
-10
lines changed

Diff for: CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [1.0.1] - 2024-12-06
5+
### Added
6+
- **Receiving**
7+
- Support for custom receiving job config which is placed in receiving event handler;
8+
49
## [1.0.0] - 2024-10-23
510
### Changed
611
- Remove `sneakers` gem in favour of [kicks](https://github.com/ruby-amqp/kicks)

Diff for: Gemfile.lock

+6-6
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ GEM
124124
rails (>= 4.2)
125125
language_server-protocol (3.17.0.3)
126126
logger (1.6.1)
127-
loofah (2.22.0)
127+
loofah (2.23.1)
128128
crass (~> 1.0.2)
129129
nokogiri (>= 1.12.0)
130130
mail (2.8.1)
@@ -135,7 +135,7 @@ GEM
135135
marcel (1.0.4)
136136
method_source (1.1.0)
137137
mini_mime (1.1.5)
138-
mini_portile2 (2.8.7)
138+
mini_portile2 (2.8.8)
139139
minitest (5.25.1)
140140
mutex_m (0.2.0)
141141
net-imap (0.4.17)
@@ -148,7 +148,7 @@ GEM
148148
net-smtp (0.5.0)
149149
net-protocol
150150
nio4r (2.7.3)
151-
nokogiri (1.16.7)
151+
nokogiri (1.16.8)
152152
mini_portile2 (~> 2.8.2)
153153
racc (~> 1.4)
154154
parallel (1.26.3)
@@ -187,9 +187,9 @@ GEM
187187
activesupport (>= 5.0.0)
188188
minitest
189189
nokogiri (>= 1.6)
190-
rails-html-sanitizer (1.6.0)
190+
rails-html-sanitizer (1.6.1)
191191
loofah (~> 2.21)
192-
nokogiri (~> 1.14)
192+
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)
193193
railties (7.1.4.1)
194194
actionpack (= 7.1.4.1)
195195
activesupport (= 7.1.4.1)
@@ -306,4 +306,4 @@ DEPENDENCIES
306306
simplecov-lcov
307307

308308
BUNDLED WITH
309-
2.3.20
309+
2.5.22

Diff for: lib/rabbit/event_handler.rb

+9
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@ class Rabbit::EventHandler
1212

1313
class_attribute :queue
1414
class_attribute :ignore_queue_conversion, default: false
15+
class_attribute :additional_job_configs, default: {}
1516

1617
class << self
1718
private
1819

1920
def queue_as(queue = nil, &block)
2021
self.queue = queue || block
2122
end
23+
24+
def job_config(**config_opts)
25+
additional_job_configs.merge!(config_opts)
26+
end
27+
28+
def job_configs(**config_opts)
29+
self.additional_job_configs = config_opts
30+
end
2231
end
2332

2433
def initialize(message)

Diff for: lib/rabbit/receiving/receive.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def log!
2828

2929
def process_message
3030
job_class
31-
.set(queue: queue)
31+
.set(queue: queue_name, **job_configs)
3232
.perform_later(message, message_info)
3333
end
3434

@@ -53,7 +53,15 @@ def message_info
5353
end
5454

5555
def queue
56-
Rabbit::Receiving::Queue.new(message, arguments).name
56+
@queue ||= Rabbit::Receiving::Queue.new(message, arguments)
57+
end
58+
59+
def job_configs
60+
queue.handler.additional_job_configs
61+
end
62+
63+
def queue_name
64+
queue.name
5765
end
5866

5967
def job_class

Diff for: spec/units/rabbit/receiving_spec.rb

+14-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
let(:arguments) { { type: event, app_id: "some_group.some_app", message_id: "uuid" } }
1010
let(:event) { "some_successful_event" }
1111
let(:job_class) { Rabbit::Receiving::Job }
12+
let(:job_configs) { {} }
1213
let(:conversion) { false }
1314
let(:handler) { Rabbit::Handler::SomeGroup::SomeSuccessfulEvent }
1415
let(:before_hook) { double("before hook") }
1516
let(:after_hook) { double("after hook") }
1617
let(:message_info) { arguments.merge(delivery_info.slice(:exchange, :routing_key)) }
1718

1819
def expect_job_queue_to_be_set
19-
expect(job_class).to receive(:set).with(queue: queue)
20+
expect(job_class).to receive(:set).with(queue: queue, **job_configs)
2021
end
2122

2223
def expect_some_handler_to_be_called
@@ -52,7 +53,7 @@ def expect_hooks_to_be_called
5253
Rabbit.config.before_receiving_hooks = [before_hook]
5354
Rabbit.config.after_receiving_hooks = [after_hook]
5455

55-
allow(job_class).to receive(:set).with(queue: queue).and_call_original
56+
allow(job_class).to receive(:set).with(queue: queue, **job_configs).and_call_original
5657

5758
allow(before_hook).to receive(:call).with(message, message_info)
5859
allow(after_hook).to receive(:call).with(message, message_info)
@@ -134,6 +135,17 @@ def call; end
134135
run_receive
135136
end
136137

138+
context "custom job configuration" do
139+
let(:job_configs) { Hash[some: :kek, pek: 123] }
140+
141+
before { handler.additional_job_configs = job_configs }
142+
after { handler.additional_job_configs = {} }
143+
144+
it "invokes job with custom config" do
145+
run_receive
146+
end
147+
end
148+
137149
context "job performs unsuccessfully" do
138150
let(:event) { "some_unsuccessful_event" }
139151
let(:queue) { "custom_prepared" }

0 commit comments

Comments
 (0)