From 26f5f3373b4708767a05561c3cd334db0ef5488d Mon Sep 17 00:00:00 2001 From: Mike Perham Date: Sat, 6 Nov 2021 18:12:09 -0700 Subject: [PATCH] Fix keyword args issue on Ruby 3.0 --- Changes.md | 5 +++++ lib/sidekiq/middleware/current_attributes.rb | 22 ++++++++++++-------- lib/sidekiq/version.rb | 2 +- myapp/config/initializers/sidekiq.rb | 10 +++++++++ test/test_current_attributes.rb | 20 ++++++++---------- 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/Changes.md b/Changes.md index ee23b0c6..c995241c 100644 --- a/Changes.md +++ b/Changes.md @@ -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` diff --git a/lib/sidekiq/middleware/current_attributes.rb b/lib/sidekiq/middleware/current_attributes.rb index 44833882..079538a0 100644 --- a/lib/sidekiq/middleware/current_attributes.rb +++ b/lib/sidekiq/middleware/current_attributes.rb @@ -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 diff --git a/lib/sidekiq/version.rb b/lib/sidekiq/version.rb index 40462327..caf8fa92 100644 --- a/lib/sidekiq/version.rb +++ b/lib/sidekiq/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Sidekiq - VERSION = "6.3.0" + VERSION = "6.3.1" end diff --git a/myapp/config/initializers/sidekiq.rb b/myapp/config/initializers/sidekiq.rb index 907f2b74..6787518b 100644 --- a/myapp/config/initializers/sidekiq.rb +++ b/myapp/config/initializers/sidekiq.rb @@ -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 diff --git a/test/test_current_attributes.rb b/test/test_current_attributes.rb index 24176b99..9c499726 100644 --- a/test/test_current_attributes.rb +++ b/test/test_current_attributes.rb @@ -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