-
Notifications
You must be signed in to change notification settings - Fork 54
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
Job running on the wrong tanent #27
Comments
Yes I have run into this problem myself. I haven't yet looked into solving the problem, but I have sidekiq-web running on a non-multitenant Rails app, and when I retry jobs from the sidekiq-web dashboard there, they get run on the wrong tenant within my multitenant Rails apps. This happens even though the job itself contains the |
For my Future Self, for anyone else from
|
@jdrago999 I need to make time to figure this out. :/ |
@jdrago999 bookmarking it thanx |
I checked this issue and I was not able to replicate it. Does someone know if this is still happening in newer versions of rails? |
Nevermind, it is still happening. |
I was able to solve this without this gem. This is the approach: This is for active jobs (app/jobs/application_job.rb): class ApplicationJob < ActiveJob::Base
before_enqueue do |job|
tenant = Apartment::Tenant.current
job.arguments.push({}) unless job.arguments.last.is_a?(Hash)
job.arguments.last.merge!(tenant: tenant)
end
before_perform do |job|
tenant = job.arguments.last[:tenant]
Apartment::Tenant.switch!(tenant)
end
end This is for action mailer module ActionMailer
class MailDeliveryJob < ActiveJob::Base
before_enqueue do |job|
tenant = Apartment::Tenant.current
args = job.arguments.last[:args]
args.push({}) unless args.last.is_a?(Hash)
args.last.merge!(tenant: tenant)
job.arguments.last[:args] = args
end
before_perform do |job|
args = job.arguments.last[:args]
tenant = args.last[:tenant]
Apartment::Tenant.switch!(tenant)
end
end
end With this you can continue using your jobs and mailers without passing extra params and you get the correct tenant in your jobs. I hope it is useful for someone. |
Thank you, I will try this and let you know. this will be a game-changer for me. |
@alejandrodevs Did you added anything else for normal jobs?? like processing background jobs (images, forms, and data process)? |
No, I didn't add anything else. Just your job classes must inherit from |
No at the moment, just checking the configurations before starting the test. This is a problem I been trying to fix for some time now. Gracias. |
|
My solution works using active job with sidekiq. I don't know how to do it directly with sidekiq workers. |
Using this solution you can't use perform_now or you will get a
|
You can make it works just by adding checking for the current tenant in the before_perform do |job|
args = job.arguments.last[:args]
tenant = args.last[:tenant] || Apartment::Tenant.current
Apartment::Tenant.switch!(tenant)
end |
@alejandrodevs This what I end up using.
|
So this is still not fixed? Is it fixable at all? |
I think I found the issue. You can try adding the middleware directly yourself in your sidekiq initializer. Eg: in
|
I'm using
I got a job that creates a batch of records on the sidekiq dashboard I get this information.
Job
BuildFooComponents
Extras
{"apartment"=>"other_tanent"}
Error Class
Error Message
if you can see the tanent
VALUES (nextval('demo.foo_id_seq')
is not the same from the extras "apartment"=>"other_tanent"the job
The text was updated successfully, but these errors were encountered: