Sidekiq Batch – 使用 perform_in 安排任务并通过回调等待完成

huangapple go评论64阅读模式
英文:

Sidekiq Batch - Scheduling jobs with perform_in and waiting for completion with callbacks

问题

I am considering using Sidekiq::Batch and learning from the following reference: https://github.com/sidekiq/sidekiq/wiki/Batches

I would like to know if the following is possible and what would happen in that case:

In the example below, I schedule a job using perform_in inside a batch.
The last of MyJob should be executed 5 hours later.

batch = Sidekiq::Batch.new
batch.on(:success, MyCallback)
batch.jobs do
    5.times do |index|
        MyJob.perform_in((1 * index).hour, args)
    end
end

By setting batch.on(:success, MyCallback), I can do something after all jobs have completed.
Here, All jobs should be completed in 5 hours and a few minutes.

My questions are:

  • In this case, will the callback fire correctly after all jobs are completed in 5 hours and a few minutes (all job completed)?
  • In that case, where is the batch waiting and how can the batch know the all job has been completed?
  • Are there any negative effects if there are many batches waiting for a long time like this?

I appreciate any help as I am not familiar with Sidekiq, thank you in advance.

英文:

I am considering using Sidekiq::Batch and learning from the following reference: https://github.com/sidekiq/sidekiq/wiki/Batches

I would like to know if the following is possible and what would happen in that case:

In the example below, I schedule a job using perform_in inside a batch.
The last of MyJob should be executed 5 hours later.

batch = Sidekiq::Batch.new
batch.on(:success, MyCallback)
batch.jobs do
    5.times do |index|
        MyJob.perform_in((1 * index).hour, args)
    end
end

By setting batch.on(:success, MyCallback), I can do something after all jobs have completed.
Here, All jobs should be completed in 5 hours and a few minutes.

My questions are:

  • In this case, will the callback fire correctly after all jobs are completed in 5 hours and a few minutes (all job completed)?
  • In that case, where is the batch waiting and how can the batch know the all job has been completed?
  • Are there any negative effects if there are many batches waiting for a long time like this?

I appreciate any help as I am not familiar with Sidekiq, thank you in advance.

答案1

得分: 1

  1. 在这种情况下,所有工作在5小时零几分钟后都完成后,回调会正确触发吗(所有工作都完成了)?

是的。

  1. 在那种情况下,批处理在哪里等待,批处理如何知道所有工作已完成?

在Redis中有一个批次计数器,表示有5个工作。每个工作完成时,该计数器会递减。当计数器为0时,回调触发。

  1. 如果有许多批次长时间等待会有什么负面影响吗?

完全没有。批处理只是数据,不会在进行中执行,不会消耗任何资源,只有工作和回调实际执行。

英文:
  1. In this case, will the callback fire correctly after all jobs are completed in 5 hours and a few minutes (all job completed)?

Yes.

  1. In that case, where is the batch waiting and how can the batch know the all job has been completed?

There is batch counter inside Redis that says 5 jobs. As each job finishes, that counter is decremented. On 0, the callback fires.

  1. Are there any negative effects if there are many batches waiting for a long time like this?

Not at all. The batch is just data, it does not execute and consumes no resources while in progress, only the jobs and callbacks actually execute.

huangapple
  • 本文由 发表于 2023年5月10日 18:19:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定