1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
require "active_support/current_attributes"
module Sidekiq
##
# Automatically save and load any current attributes in the execution context
# so context attributes "flow" from Rails actions into any associated jobs.
# This can be useful for multi-tenancy, i18n locale, timezone, any implicit
# per-request attribute. See +ActiveSupport::CurrentAttributes+.
#
# @example
#
# # in your initializer
# require "sidekiq/middleware/current_attributes"
# Sidekiq::CurrentAttributes.persist(Myapp::Current)
#
module CurrentAttributes
class Save
def initialize(cattr)
@klass = cattr
end
def call(_, job, _, _)
attrs = @klass.attributes
if job.has_key?("cattr")
job["cattr"].merge!(attrs)
else
job["cattr"] = attrs
end
yield
end
end
class Load
def initialize(cattr)
@klass = cattr
end
def call(_, job, _, &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, klass
end
Sidekiq.configure_server do |config|
config.client_middleware.add Save, klass
config.server_middleware.add Load, klass
end
end
end
end
|