简体   繁体   中英

Queue Sidekiq job on Rails app start

I need to add a job to the Sidekiq queue when my Rails app starts, to update some data, but I don't know where is the best place to do it.

Right now, I've wrote this on my application.rb :

class Application < Rails::Application
    config.after_initialize do
        MyWorker.perform_async
    end
end

But the problem is that when I run the sidekiq command it will also load the Rails stack, so I'll end up with 2 jobs on the queue.

Is there any other way of doing that? This is my first big Rails app and my first time with Sidekiq, so I don't know if I'm not understanding things correctly. That might not be the right way of doing that.

Thanks!

A better solution would be to create an initializer config/initializers/sidekiq.rb

Sidekiq.configure_client do |config|
    Rails.application.config.after_initialize do
        # You code goes here
    end
end

We were having issues w/ Redis connections and multiple jobs being launched.

I ended up using this and it seems to be working well:

if defined?(Sidekiq)
  Sidekiq.configure_server do |config|
    config.on(:startup) do
      already_scheduled = Sidekiq::ScheduledSet.new.any? {|job| job.klass == "MyWorker" }
      MyWorker.perform_async unless already_scheduled
    end
  end
end

可能工头适合您的目的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM