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:
parent
0723abd071
commit
26f5f3373b
5 changed files with 38 additions and 21 deletions
|
@ -5,6 +5,11 @@
|
||||||
HEAD
|
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
|
- **BREAK**: The Web UI has been refactored to remove jQuery. Any UI extensions
|
||||||
which use jQuery will break.
|
which use jQuery will break.
|
||||||
- **FEATURE**: Sidekiq.logger has been enhanced so any `Rails.logger`
|
- **FEATURE**: Sidekiq.logger has been enhanced so any `Rails.logger`
|
||||||
|
|
|
@ -15,33 +15,37 @@ module Sidekiq
|
||||||
#
|
#
|
||||||
module CurrentAttributes
|
module CurrentAttributes
|
||||||
class Save
|
class Save
|
||||||
def initialize(with:)
|
def initialize(cattr)
|
||||||
@klass = with
|
@klass = cattr
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(_, job, _, _)
|
def call(_, job, _, _)
|
||||||
job["ctx"] = @klass.attributes
|
job["cattr"] = @klass.attributes
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Load
|
class Load
|
||||||
def initialize(with:)
|
def initialize(cattr)
|
||||||
@klass = with
|
@klass = cattr
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(_, job, _, &block)
|
def call(_, job, _, &block)
|
||||||
@klass.set(job["ctx"], &block)
|
if job.has_key?("cattr")
|
||||||
|
@klass.set(job["cattr"], &block)
|
||||||
|
else
|
||||||
|
yield
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.persist(klass)
|
def self.persist(klass)
|
||||||
Sidekiq.configure_client do |config|
|
Sidekiq.configure_client do |config|
|
||||||
config.client_middleware.add Save, with: klass
|
config.client_middleware.add Save, klass
|
||||||
end
|
end
|
||||||
Sidekiq.configure_server do |config|
|
Sidekiq.configure_server do |config|
|
||||||
config.client_middleware.add Save, with: klass
|
config.client_middleware.add Save, klass
|
||||||
config.server_middleware.add Load, with: klass
|
config.server_middleware.add Load, klass
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Sidekiq
|
module Sidekiq
|
||||||
VERSION = "6.3.0"
|
VERSION = "6.3.1"
|
||||||
end
|
end
|
||||||
|
|
|
@ -33,4 +33,14 @@ class TimedWorker
|
||||||
end
|
end
|
||||||
|
|
||||||
Sidekiq::Extensions.enable_delay!
|
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
|
Post.delay.testing
|
||||||
|
|
|
@ -9,19 +9,19 @@ end
|
||||||
|
|
||||||
class TestCurrentAttributes < Minitest::Test
|
class TestCurrentAttributes < Minitest::Test
|
||||||
def test_save
|
def test_save
|
||||||
cm = Sidekiq::CurrentAttributes::Save.new(with: Myapp::Current)
|
cm = Sidekiq::CurrentAttributes::Save.new(Myapp::Current)
|
||||||
job = {}
|
job = {}
|
||||||
with_context(:user_id, 123) do
|
with_context(:user_id, 123) do
|
||||||
cm.call(nil, job, nil, nil) 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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_load
|
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
|
assert_nil Myapp::Current.user_id
|
||||||
cm.call(nil, job, nil) do
|
cm.call(nil, job, nil) do
|
||||||
assert_equal 123, Myapp::Current.user_id
|
assert_equal 123, Myapp::Current.user_id
|
||||||
|
@ -32,18 +32,16 @@ class TestCurrentAttributes < Minitest::Test
|
||||||
def test_persist
|
def test_persist
|
||||||
begin
|
begin
|
||||||
Sidekiq::CurrentAttributes.persist(Myapp::Current)
|
Sidekiq::CurrentAttributes.persist(Myapp::Current)
|
||||||
Myapp::Current.user_id = 16
|
|
||||||
|
|
||||||
job_hash = {}
|
job_hash = {}
|
||||||
|
with_context(:user_id, 16) do
|
||||||
Sidekiq.client_middleware.invoke(nil, job_hash, nil, nil) do
|
Sidekiq.client_middleware.invoke(nil, job_hash, nil, nil) do
|
||||||
assert_equal 16, job_hash["ctx"][:user_id]
|
assert_equal 16, job_hash["cattr"][:user_id]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Myapp::Current.reset_all
|
|
||||||
|
|
||||||
assert_nil Myapp::Current.user_id
|
assert_nil Myapp::Current.user_id
|
||||||
Sidekiq.server_middleware.invoke(nil, job_hash, nil) do
|
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
|
assert_equal 16, Myapp::Current.user_id
|
||||||
end
|
end
|
||||||
assert_nil Myapp::Current.user_id
|
assert_nil Myapp::Current.user_id
|
||||||
|
|
Loading…
Reference in a new issue