Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sidekiq Middleware registered in the wrong order with Batches when used with Sidekiq Pro #262

Open
eapache-opslevel opened this issue Oct 23, 2024 · 0 comments

Comments

@eapache-opslevel
Copy link

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
end

and 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 }
end

You 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant