当一组工人完成时如何执行 Sidekiq 回调 [英] How to perform a Sidekiq callback when a group of workers are complete
本文介绍了当一组工人完成时如何执行 Sidekiq 回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个 Sidekiq 任务可以将产品处理到我的数据库中.每个产品都按商店分组,所以我的代码的一个过于简化的例子是这样的......
Lets say I have a Sidekiq task that processes products to my database. Each product is grouped by store, so an overly simplified example of my code would be something like this...
stores.each do |store|
store.products.each do |product|
ProductWorker.perform_async(product.id)
end
end
当一家商店的所有产品都已运行时.我想用当前时间更新商店 last_updated
列.但仅当该商店的最后一个任务运行时.我怎样才能做到这一点?
When all the products from one store have run. I'd like to update the stores last_updated
column with the current time. But only when the last task for that store has run. How can I achieve this?
推荐答案
这正是 Sidekiq Pro 的批处理功能旨在解决的问题:
This is exactly what Sidekiq Pro's Batches feature is designed to solve:
https://github.com/mperham/sidekiq/wiki/Batches
您将编写此代码:
class ProductWorker
include Sidekiq::Worker
def on_complete(status, params)
Store.find(params['sid']).update_attribute(:last_updated, Time.now)
end
def perform(product_id)
# do something
end
end
stores.each do |store|
b = Sidekiq::Batch.new
b.on(:complete, ProductWorker, 'sid' => store.id)
b.jobs do
store.products.find_each do |product|
ProductWorker.perform_async(product.id)
end
end
end
简单.
这篇关于当一组工人完成时如何执行 Sidekiq 回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文