Batch background jobs with Sidekiq

Tram Ho

When we want to create multiple background jobs that run in parallel and want to know when all of those jobs are complete, what do we do?

Sidekiq Pro allows me to run a set of background jobs in parallel and then receive a callback right after all jobs have been run. However, Sidekiq Pro is Sidekiq’s paid feature. Thankfully, sidekiq-batch gem has been built based on the Sidekiq Pro API and I can use it for free, and the usage is similar to Sidekiq Pro.

Installation

Basic Usage

When the set of all jobs is completed, MyCallBack is executed.

  • complete – when all jobs in the batch have been run at least once, successful or not.
  • success – when all jobs in the batch have been run successfully.
  • death – when an error occurs in the batch

In the above callbacks, params status will have the following options:

Demo Example

Result:

As the result above, we see that after the jobs ran in complete parallel, the on_success and on_complete callbacks performed as expected.

References:

https://github.com/breamware/sidekiq-batch

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

https://gorails.com/episodes/batching-background-jobs-with-sidekiq?autoplay=1

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo