问题
I currently use delayed job to process jobs asynchronously. Instead of creating workers, I use the .delay
method a lot.
I want to move to Sidekiq, but I have too many types of jobs, and can't make sure all of them are thread safe. So I want to run Delayed Job and Sidekiq in parallel, and migrating one type of job at a time.
Since both Delayed Job and Sidekiq offers the .delay
method, how can I make the distinction between the two? Are there any other potential issues?
回答1:
For Sidekiq 2.17.1 and later, somewhere in the Rails initializers, call the following:
Sidekiq.hook_rails!
Sidekiq.remove_delay!
and you will have only prefixed sidekiq_delay
methods and so on.
(official document)
For older versions of Sidekiq:
Put the following in config/initializers/sidekiq.rb
module Sidekiq::Extensions::Klass
alias :sidekiq_delay :delay
remove_method :delay
alias :sidekiq_delay_for :delay_for
remove_method :delay_for
alias :sidekiq_delay_until :delay_until
remove_method :delay_until
end
module Sidekiq::Extensions::ActiveRecord
alias :sidekiq_delay :delay
remove_method :delay
alias :sidekiq_delay_for :delay_for
remove_method :delay_for
alias :sidekiq_delay_until :delay_until
remove_method :delay_until
end
module Sidekiq::Extensions::ActionMailer
alias :sidekiq_delay :delay
remove_method :delay
alias :sidekiq_delay_for :delay_for
remove_method :delay_for
alias :sidekiq_delay_until :delay_until
remove_method :delay_until
end
And then you can use sidekiq_delay
to queue in Sidekiq, and call delay
to queue in Delayed Job.
回答2:
For anyone searching for this. I did find Sidekiq now has a setting for this out of the box. All you
need to do is add Sidekiq.remove_delay!
to config/initializers/sidekiq.rb
This is described here: https://github.com/mperham/sidekiq/wiki/Delayed-Extensions
回答3:
It seems that Sidekiq has mix-in that adds .delay
methods to all classes. Not 100% sure how this will behave but it could result in problems if delay
you are refering to is on the instance level.
My advice would be to add sidekiq
library per job until you have moved all you jobs to it. Meaning to avoid including both libraries at the same time, again if possible.
来源:https://stackoverflow.com/questions/13306215/run-delayed-jobs-and-sidekiq-at-the-same-time