Skip to content

Commit

Permalink
Add keyword arguments handler in Sidekiq::JobRecord#display_args (#5134)
Browse files Browse the repository at this point in the history
* Add keyword arguments handler in Sidekiq::JobRecord#display_args

* Update Sidekiq::JobRecord#display_args kwargs's presence check so that we are not dependent on Rails.
  • Loading branch information
Thach Chau committed Jan 29, 2022
1 parent df20201 commit 10e7feb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/sidekiq/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,12 @@ def display_args
# Unwrap known wrappers so they show up in a human-friendly manner in the Web UI
@display_args ||= case klass
when /\ASidekiq::Extensions::Delayed/
safe_load(args[0], args) do |_, _, arg|
arg
safe_load(args[0], args) do |_, _, arg, kwarg|
if !kwarg || kwarg.empty?
arg
else
[arg, kwarg]
end
end
when "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper"
job_args = self["wrapped"] ? args[0]["arguments"] : []
Expand Down
12 changes: 8 additions & 4 deletions test/test_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ def self.long_class_method_with_optional_args(*arg, **kwargs)
assert_equal [], Sidekiq::Queue.all.map(&:name)
q = Sidekiq::Queue.new
assert_equal 0, q.size
MyModel.delay.long_class_method_with_optional_args(with: :keywords)
MyModel.delay.long_class_method_with_optional_args("argument_a", "argument_b", with: :keywords)
assert_equal ['default'], Sidekiq::Queue.all.map(&:name)
assert_equal 1, q.size
obj = YAML.load q.first['args'].first
assert_equal({ with: :keywords }, obj.last)
assert_equal([["argument_a", "argument_b"], { with: :keywords }], q.first.display_args)
end

it 'forwards the keyword arguments to perform' do
Expand Down Expand Up @@ -92,11 +93,12 @@ def greetings_with_optional_args(*arg, **kwargs)
assert_equal [], Sidekiq::Queue.all.map(&:name)
q = Sidekiq::Queue.new
assert_equal 0, q.size
UserMailer.delay.greetings_with_optional_args(with: :keywords)
UserMailer.delay.greetings_with_optional_args("argument_a", "argument_b", with: :keywords)
assert_equal ['default'], Sidekiq::Queue.all.map(&:name)
assert_equal 1, q.size
obj = YAML.load q.first['args'].first
assert_equal({ with: :keywords }, obj.last)
assert_equal([["argument_a", "argument_b"], { with: :keywords }], q.first.display_args)
end

it 'allows delayed scheduling of AM mails' do
Expand Down Expand Up @@ -132,10 +134,11 @@ def self.doit_with_optional_args(*arg, **kwargs)
it 'allows delay of any ole class method with optional arguments' do
q = Sidekiq::Queue.new
assert_equal 0, q.size
SomeClass.delay.doit_with_optional_args(with: :keywords)
SomeClass.delay.doit_with_optional_args("argument_a", "argument_b", with: :keywords)
assert_equal 1, q.size
obj = YAML.load q.first['args'].first
assert_equal({ with: :keywords }, obj.last)
assert_equal([["argument_a", "argument_b"], { with: :keywords }], q.first.display_args)
end

it 'forwards the keyword arguments to perform' do
Expand Down Expand Up @@ -170,10 +173,11 @@ def self.doit_with_optional_args(*arg, **kwargs)
it 'allows delay of any module class method with optional arguments' do
q = Sidekiq::Queue.new
assert_equal 0, q.size
SomeModule.delay.doit_with_optional_args(with: :keywords)
SomeModule.delay.doit_with_optional_args("argument_a", "argument_b", with: :keywords)
assert_equal 1, q.size
obj = YAML.load q.first['args'].first
assert_equal({ with: :keywords }, obj.last)
assert_equal([["argument_a", "argument_b"], { with: :keywords }], q.first.display_args)
end

it 'forwards the keyword arguments to perform' do
Expand Down

0 comments on commit 10e7feb

Please sign in to comment.