mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Adjust CurrentAttributes to use String, fixes #5536
This commit is contained in:
parent
eb85755a84
commit
d0e0e545a3
5 changed files with 22 additions and 17 deletions
|
|
@ -2,6 +2,12 @@
|
||||||
|
|
||||||
[Sidekiq Changes](https://github.com/mperham/sidekiq/blob/main/Changes.md) | [Sidekiq Pro Changes](https://github.com/mperham/sidekiq/blob/main/Pro-Changes.md) | [Sidekiq Enterprise Changes](https://github.com/mperham/sidekiq/blob/main/Ent-Changes.md)
|
[Sidekiq Changes](https://github.com/mperham/sidekiq/blob/main/Changes.md) | [Sidekiq Pro Changes](https://github.com/mperham/sidekiq/blob/main/Pro-Changes.md) | [Sidekiq Enterprise Changes](https://github.com/mperham/sidekiq/blob/main/Ent-Changes.md)
|
||||||
|
|
||||||
|
HEAD
|
||||||
|
----------
|
||||||
|
|
||||||
|
- Adjust CurrentAttributes to work with the String class name so we aren't referencing
|
||||||
|
the Class within a Rails initializer [#5536]
|
||||||
|
|
||||||
6.5.7
|
6.5.7
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,18 +11,18 @@ module Sidekiq
|
||||||
#
|
#
|
||||||
# # in your initializer
|
# # in your initializer
|
||||||
# require "sidekiq/middleware/current_attributes"
|
# require "sidekiq/middleware/current_attributes"
|
||||||
# Sidekiq::CurrentAttributes.persist(Myapp::Current)
|
# Sidekiq::CurrentAttributes.persist("Myapp::Current")
|
||||||
#
|
#
|
||||||
module CurrentAttributes
|
module CurrentAttributes
|
||||||
class Save
|
class Save
|
||||||
include Sidekiq::ClientMiddleware
|
include Sidekiq::ClientMiddleware
|
||||||
|
|
||||||
def initialize(cattr)
|
def initialize(cattr)
|
||||||
@klass = cattr
|
@strklass = cattr
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(_, job, _, _)
|
def call(_, job, _, _)
|
||||||
attrs = @klass.attributes
|
attrs = @strklass.constantize.attributes
|
||||||
if attrs.any?
|
if attrs.any?
|
||||||
if job.has_key?("cattr")
|
if job.has_key?("cattr")
|
||||||
job["cattr"].merge!(attrs)
|
job["cattr"].merge!(attrs)
|
||||||
|
|
@ -38,12 +38,12 @@ module Sidekiq
|
||||||
include Sidekiq::ServerMiddleware
|
include Sidekiq::ServerMiddleware
|
||||||
|
|
||||||
def initialize(cattr)
|
def initialize(cattr)
|
||||||
@klass = cattr
|
@strklass = cattr
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(_, job, _, &block)
|
def call(_, job, _, &block)
|
||||||
if job.has_key?("cattr")
|
if job.has_key?("cattr")
|
||||||
@klass.set(job["cattr"], &block)
|
@strklass.constantize.set(job["cattr"], &block)
|
||||||
else
|
else
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
|
|
@ -51,8 +51,8 @@ module Sidekiq
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.persist(klass, config = Sidekiq.default_configuration)
|
def self.persist(klass, config = Sidekiq.default_configuration)
|
||||||
config.client_middleware.add Save, klass
|
config.client_middleware.add Save, klass.to_s
|
||||||
config.server_middleware.add Load, klass
|
config.server_middleware.add Load, klass.to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
5
myapp/app/lib/myapp/current.rb
Normal file
5
myapp/app/lib/myapp/current.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
module Myapp
|
||||||
|
class Current < ActiveSupport::CurrentAttributes
|
||||||
|
attribute :tenant_id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -32,14 +32,8 @@ class TimedWorker
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module Myapp
|
|
||||||
class Current < ActiveSupport::CurrentAttributes
|
|
||||||
attribute :tenant_id
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
require "sidekiq/middleware/current_attributes"
|
require "sidekiq/middleware/current_attributes"
|
||||||
Sidekiq::CurrentAttributes.persist(Myapp::Current) # Your AS::CurrentAttributes singleton
|
Sidekiq::CurrentAttributes.persist("Myapp::Current") # Your AS::CurrentAttributes singleton
|
||||||
|
|
||||||
# Sidekiq.transactional_push!
|
# Sidekiq.transactional_push!
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ describe "Current attributes" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "saves" do
|
it "saves" do
|
||||||
cm = Sidekiq::CurrentAttributes::Save.new(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
|
||||||
|
|
@ -26,7 +26,7 @@ describe "Current attributes" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "loads" do
|
it "loads" do
|
||||||
cm = Sidekiq::CurrentAttributes::Load.new(Myapp::Current)
|
cm = Sidekiq::CurrentAttributes::Load.new("Myapp::Current")
|
||||||
|
|
||||||
job = {"cattr" => {"user_id" => 123}}
|
job = {"cattr" => {"user_id" => 123}}
|
||||||
assert_nil Myapp::Current.user_id
|
assert_nil Myapp::Current.user_id
|
||||||
|
|
@ -37,7 +37,7 @@ describe "Current attributes" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "persists" do
|
it "persists" do
|
||||||
Sidekiq::CurrentAttributes.persist(Myapp::Current, @config)
|
Sidekiq::CurrentAttributes.persist("Myapp::Current", @config)
|
||||||
job_hash = {}
|
job_hash = {}
|
||||||
with_context(:user_id, 16) do
|
with_context(:user_id, 16) do
|
||||||
@config.client_middleware.invoke(nil, job_hash, nil, nil) do
|
@config.client_middleware.invoke(nil, job_hash, nil, nil) do
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue