[英]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. 当我的Rails应用程序启动时,我需要向Sidekiq队列添加一个作业,以更新一些数据,但我不知道哪里是最好的地方。
Right now, I've wrote this on my application.rb
: 现在,我在我的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. 但问题是当我运行sidekiq
命令时它还会加载Rails堆栈,所以我最终会在队列中找到2个作业。
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. 这是我的第一个大Rails应用程序,也是我第一次使用Sidekiq,所以我不知道我是不是正确理解了。 That might not be the right way of doing that. 这可能不是这样做的正确方法。
Thanks! 谢谢!
A better solution would be to create an initializer config/initializers/sidekiq.rb
更好的解决方案是创建初始化程序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. 我们遇到了与Redis连接和多个工作正在启动的问题。
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
可能工头适合您的目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.