Skip to content

Commit b376f96

Browse files
hyb175mensfeld
andauthored
[Chore] Add Support for message_attributes to InlineExecutor (#835)
* chore: add support for message_attributes to InlineExecutor * chore: disable Metrics/BlockLength rule for RSpec describe and context blocks * chore: add more testing with batch for message_attributes support --------- Co-authored-by: Maciej Mensfeld <maciej@mensfeld.pl>
1 parent 9427e17 commit b376f96

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

.rubocop.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ AllCops:
33
- '**/Gemfile'
44
TargetRubyVersion: 3.4
55

6+
Metrics/BlockLength:
7+
AllowedMethods:
8+
- describe
9+
- context
10+
611
Metrics/PerceivedComplexity:
712
Enabled: false
813

lib/shoryuken/worker/inline_executor.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ class << self
55
def perform_async(worker_class, body, options = {})
66
body = JSON.dump(body) if body.is_a?(Hash)
77
queue_name = options.delete(:queue) || worker_class.get_shoryuken_options['queue']
8+
message_attributes = options.delete(:message_attributes) || {}
9+
message_attributes['shoryuken_class'] = {
10+
string_value: worker_class.to_s,
11+
data_type: 'String'
12+
}
813

914
sqs_msg = OpenStruct.new(
1015
body: body,
1116
attributes: nil,
1217
md5_of_body: nil,
1318
md5_of_message_attributes: nil,
14-
message_attributes: nil,
19+
message_attributes: message_attributes,
1520
message_id: nil,
1621
receipt_handle: nil,
1722
delete: nil,

spec/shoryuken/worker/inline_executor_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@
1111

1212
TestWorker.perform_async('test')
1313
end
14+
15+
it 'properly sets message_attributes' do
16+
custom_attributes = {
17+
'custom_key' => { string_value: 'custom_value', data_type: 'String' }
18+
}
19+
20+
expect_any_instance_of(TestWorker).to receive(:perform) do |_, sqs_msg, _|
21+
expect(sqs_msg.message_attributes).to include('shoryuken_class')
22+
expect(sqs_msg.message_attributes).to include('custom_key')
23+
expect(sqs_msg.message_attributes['custom_key'][:string_value]).to eq('custom_value')
24+
end
25+
26+
TestWorker.perform_async('test', message_attributes: custom_attributes)
27+
end
1428
end
1529

1630
describe '.perform_in' do
@@ -19,6 +33,20 @@
1933

2034
TestWorker.perform_in(60, 'test')
2135
end
36+
37+
it 'properly passes message_attributes to perform_async' do
38+
custom_attributes = {
39+
'custom_key' => { string_value: 'custom_value', data_type: 'String' }
40+
}
41+
42+
expect_any_instance_of(TestWorker).to receive(:perform) do |_, sqs_msg, _|
43+
expect(sqs_msg.message_attributes).to include('shoryuken_class')
44+
expect(sqs_msg.message_attributes).to include('custom_key')
45+
expect(sqs_msg.message_attributes['custom_key'][:string_value]).to eq('custom_value')
46+
end
47+
48+
TestWorker.perform_in(60, 'test', message_attributes: custom_attributes)
49+
end
2250
end
2351

2452
context 'batch' do
@@ -36,6 +64,20 @@
3664

3765
TestWorker.perform_async('test')
3866
end
67+
68+
it 'properly passes message_attributes with batch' do
69+
custom_attributes = {
70+
'custom_key' => { string_value: 'custom_value', data_type: 'String' }
71+
}
72+
73+
expect_any_instance_of(TestWorker).to receive(:perform) do |_, sqs_msgs, _|
74+
expect(sqs_msgs.first.message_attributes).to include('shoryuken_class')
75+
expect(sqs_msgs.first.message_attributes).to include('custom_key')
76+
expect(sqs_msgs.first.message_attributes['custom_key'][:string_value]).to eq('custom_value')
77+
end
78+
79+
TestWorker.perform_async('test', message_attributes: custom_attributes)
80+
end
3981
end
4082

4183
describe '.perform_in' do
@@ -44,6 +86,20 @@
4486

4587
TestWorker.perform_in(60, 'test')
4688
end
89+
90+
it 'properly passes message_attributes with batch' do
91+
custom_attributes = {
92+
'custom_key' => { string_value: 'custom_value', data_type: 'String' }
93+
}
94+
95+
expect_any_instance_of(TestWorker).to receive(:perform) do |_, sqs_msgs, _|
96+
expect(sqs_msgs.first.message_attributes).to include('shoryuken_class')
97+
expect(sqs_msgs.first.message_attributes).to include('custom_key')
98+
expect(sqs_msgs.first.message_attributes['custom_key'][:string_value]).to eq('custom_value')
99+
end
100+
101+
TestWorker.perform_in(60, 'test', message_attributes: custom_attributes)
102+
end
47103
end
48104
end
49105
end

0 commit comments

Comments
 (0)