1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/middleware/i18n.rb
Mike Perham 83aea0690e Explicitly pass Redis associated with this job
When pushing a job, the middleware should be able to access the Redis instance associated with that job.  Previously, Sidekiq was limited to one global Redis instance.  Now that we want to support sharding, we have to explicitly pass the instance in OR hack up APIs with thread local variables.  Explicit is better.
2014-03-25 21:38:17 -07:00

35 lines
856 B
Ruby

module Sidekiq::Middleware::I18n
# Get the current locale and store it in the message
# to be sent to Sidekiq.
class Client
def call(worker_class, msg, queue, redis_pool)
msg['locale'] ||= I18n.locale
yield
end
end
# Pull the msg locale out and set the current thread to use it.
class Server
def call(worker, msg, queue)
I18n.locale = msg['locale'] || I18n.default_locale
yield
ensure
I18n.locale = I18n.default_locale
end
end
end
Sidekiq.configure_client do |config|
config.client_middleware do |chain|
chain.add Sidekiq::Middleware::I18n::Client
end
end
Sidekiq.configure_server do |config|
config.client_middleware do |chain|
chain.add Sidekiq::Middleware::I18n::Client
end
config.server_middleware do |chain|
chain.add Sidekiq::Middleware::I18n::Server
end
end