How to perform a Sidekiq callback when a group of workers are complete

旧巷老猫 提交于 2019-12-04 06:44:34

This is exactly what Sidekiq Pro's Batches feature is designed to solve:

https://github.com/mperham/sidekiq/wiki/Batches

http://sidekiq.org/pro/

You would write this code:

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

Easy.

You can try sidekiq-batch gem to solve your problem. It's like paid Batches feature of Sidekiq Pro and has same api.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!