-
Notifications
You must be signed in to change notification settings - Fork 108
Description
When using activerecord-multi-tenant with the paid Sidekiq-Pro gem, sidekiq batch callbacks do not have a tenant set by default, even if the original job and batch were enqueued inside a tenant block.
Given the following example job:
class TestJob
include Sidekiq::Job
def perform
puts "perform"
puts MultiTenant.current_tenant
end
def callback(status, options)
puts "callback"
puts MultiTenant.current_tenant
end
endand the following execution (assuming 1 is a valid tenant ID):
MultiTenant.with(1) do
b = Sidekiq::Batch.new
b.on(:success, "TestJob#callback")
b.jobs { TestJob.perform_async }
endYou will find that while the tenant is set correctly in perform, it is not set in callback. This is because of a middleware ordering issue between the multitenant code and the sidekiq-pro batch code.
We were able to work around this issue by manually removing and re-ordering the middleware in our own initializer:
# Fix middleware order so that Sidekiq Batch Callbacks are run with a tenant set
chain.remove(Sidekiq::Middleware::MultiTenant::Client)
chain.insert_before(Sidekiq::Batch::Client, Sidekiq::Middleware::MultiTenant::Client)(and the same thing for the server middleware, and the client middleware on the server).
Given that this is a sidekiq-builtin feature, it would be nice if this gem could check for the presence of the Batch middleware and insert its middleware in the correct spot by default.