"不要在Sidekiq :: Worker类上调用.delay,调用.perform_async"

时间:2015-11-30 16:59:45

标签: sidekiq

我经常与Sidekiq工作者一起使用的一种模式是定义perform_all方法:

class ModelMutateWorker
  include Sidekiq::Worker

  def perform(id)
    model = Model.find(id)
    # Mutate model in some way
  end

  def self.perform_all
    batch = Sidekiq::Batch.new
    batch.description = "#{self.name}.#{__method__}"
    batch.jobs do
      Sidekiq::Client.push_bulk('class' => self, 'args' => Model.ids.map { |id| [id] })
    end
    return batch
  end

end

在这个有点人为的例子中,Model.idsSidekiq::Client.push_bulk电话都有些昂贵。从Web请求或命令行执行此操作时,最好也延迟这些操作。

ModelMutateWorker.delay(queue: "my_other_queue").perform_all

然而,来自.delay的Sidekiq 4.0 removed .delay_for.delay_untilSidekiq::Worker。我不记得曾经看过一个弃用警告。是否有一种干嘛的方法可以恢复这种行为?

1 个答案:

答案 0 :(得分:0)

perform_all移到模型中或创建一个新的BulkOperation类来保存它。