1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Fix keyword args issue on Ruby 3.0

This commit is contained in:
Mike Perham 2021-11-06 18:12:09 -07:00
parent 0723abd071
commit 26f5f3373b
5 changed files with 38 additions and 21 deletions

View file

@ -5,6 +5,11 @@
HEAD
---------
- Fix keyword arguments error with CurrentAttributes on Ruby 3.0 [#5048]
6.3.0
---------
- **BREAK**: The Web UI has been refactored to remove jQuery. Any UI extensions
which use jQuery will break.
- **FEATURE**: Sidekiq.logger has been enhanced so any `Rails.logger`

View file

@ -15,33 +15,37 @@ module Sidekiq
#
module CurrentAttributes
class Save
def initialize(with:)
@klass = with
def initialize(cattr)
@klass = cattr
end
def call(_, job, _, _)
job["ctx"] = @klass.attributes
job["cattr"] = @klass.attributes
yield
end
end
class Load
def initialize(with:)
@klass = with
def initialize(cattr)
@klass = cattr
end
def call(_, job, _, &block)
@klass.set(job["ctx"], &block)
if job.has_key?("cattr")
@klass.set(job["cattr"], &block)
else
yield
end
end
end
def self.persist(klass)
Sidekiq.configure_client do |config|
config.client_middleware.add Save, with: klass
config.client_middleware.add Save, klass
end
Sidekiq.configure_server do |config|
config.client_middleware.add Save, with: klass
config.server_middleware.add Load, with: klass
config.client_middleware.add Save, klass
config.server_middleware.add Load, klass
end
end
end

View file

@ -1,5 +1,5 @@
# frozen_string_literal: true
module Sidekiq
VERSION = "6.3.0"
VERSION = "6.3.1"
end

View file

@ -33,4 +33,14 @@ class TimedWorker
end
Sidekiq::Extensions.enable_delay!
module Myapp
class Current < ActiveSupport::CurrentAttributes
attribute :tenant_id
end
end
require "sidekiq/middleware/current_attributes"
Sidekiq::CurrentAttributes.persist(Myapp::Current) # Your AS::CurrentAttributes singleton
Post.delay.testing

View file

@ -9,19 +9,19 @@ end
class TestCurrentAttributes < Minitest::Test
def test_save
cm = Sidekiq::CurrentAttributes::Save.new(with: Myapp::Current)
cm = Sidekiq::CurrentAttributes::Save.new(Myapp::Current)
job = {}
with_context(:user_id, 123) do
cm.call(nil, job, nil, nil) do
assert_equal 123, job["ctx"][:user_id]
assert_equal 123, job["cattr"][:user_id]
end
end
end
def test_load
cm = Sidekiq::CurrentAttributes::Load.new(with: Myapp::Current)
cm = Sidekiq::CurrentAttributes::Load.new(Myapp::Current)
job = { "ctx" => { "user_id" => 123 } }
job = { "cattr" => { "user_id" => 123 } }
assert_nil Myapp::Current.user_id
cm.call(nil, job, nil) do
assert_equal 123, Myapp::Current.user_id
@ -32,18 +32,16 @@ class TestCurrentAttributes < Minitest::Test
def test_persist
begin
Sidekiq::CurrentAttributes.persist(Myapp::Current)
Myapp::Current.user_id = 16
job_hash = {}
Sidekiq.client_middleware.invoke(nil, job_hash, nil, nil) do
assert_equal 16, job_hash["ctx"][:user_id]
with_context(:user_id, 16) do
Sidekiq.client_middleware.invoke(nil, job_hash, nil, nil) do
assert_equal 16, job_hash["cattr"][:user_id]
end
end
Myapp::Current.reset_all
assert_nil Myapp::Current.user_id
Sidekiq.server_middleware.invoke(nil, job_hash, nil) do
assert_equal 16, job_hash["ctx"][:user_id]
assert_equal 16, job_hash["cattr"][:user_id]
assert_equal 16, Myapp::Current.user_id
end
assert_nil Myapp::Current.user_id