如何使用 Sidekiq 运行连续的后台作业? [英] How can I run a continuous background job with Sidekiq?
问题描述
我一直在成功地使用 Sidekiq 来运行由用户操作在 Rails 3.2 应用程序中启动的后台作业.我的特定应用涉及通过第三方 API 从外部源发送和接收数据.
I have been using Sidekiq successfully to run background jobs that are initiated by user actions in a Rails 3.2 application. My particular application involves sending and receiving data from and external source via a third party API.
我需要通过不断检查每个用户是否有可供下载的数据来使数据与此外部源保持同步.
I need to keep data synchronized with this external source by continuously checking for each user if there is data available to be downloaded.
到目前为止,我所有的后台作业都是用户启动的,正如我上面提到的.我的问题是,如果我想在 while
循环中不断检查每个用户的外部数据是否存在,我想:
All of my background jobs thus far are user-initiated as I mentioned above. My question is, if I want to continuously check for the existence of external data for each user, in a while
loop I imagine:
# pseudocode
while true
for user in User.active
data = user.get_data
next if data.blank?
UserDataWorker.perform_async(user.id)
end
end
那么我把上面的代码放在哪里?我对如何产生连续任务感到困惑.我不想将代码放在 Sidekiq 工作器中,因为它只是一个永远不会完成的工作.还是我应该这样做?
then where do I put the code above? I get confused on how to spawn a continuous task. I don't want to put the code in a Sidekiq worker, because it will just be a job that never completes. Or should I do that?
我不想把它放在一个 rake 任务中,因为那我如何监控那个 rake 任务呢?我读到的关于 Sidekiq 的所有内容似乎都指向一个 Worker 正在运行一个有限且能够完成或出错的任务.感谢您提供的任何帮助.
I don't want to put it in a rake task, because then how do I monitor that rake task? Everything I've read about Sidekiq seems to point to a Worker running a task that is finite and is able to be completed or errored out. I appreciate any help you can offer.
推荐答案
Ruby 多线程正是为此而生.在您的 initializers 文件夹中生成一个线程:
Ruby multithreading is just for that. Spawn a thread in your initializers folder:
Thread.new do
loop do
for user in User.active
data = user.get_data
next if data.blank?
UserDataWorker.perform_async(user.id)
end
sleep 1
end
end
如果你的支票不需要太多资源,应该没问题.
If your checks don't need much resources, should be allright.
这篇关于如何使用 Sidekiq 运行连续的后台作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!