Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Wait for Parallel Sidekiq Workers to All Complete Before Calling Another Method?

I have several workers that are being run using Sidekiq and scheduled using Sidetiq. I'm looking for advice on the best way to wait for all workers to complete before executing a callback, similar to Sidekiq-Pro's batching functionality. Any advice on possible options would be greatly appreciated!

like image 879
AvocadoRivalry Avatar asked Apr 29 '15 20:04

AvocadoRivalry


People also ask

How Sidekiq works or how would you implement Sidekiq?

Sidekiq is an open-source framework that provides efficient background processing for Ruby applications. It uses Redis as an in-memory data structure to store all of its job and operational data. It's important to be aware that Sidekiq by default doesn't do scheduling, it only executes jobs.

How many jobs can Sidekiq handle?

Today Sidekiq uses a default concurrency of 25. These means Sidekiq will spawn 25 worker threads and execute up to 25 jobs concurrently in a process.

How do I stop Sidekiq workers?

You can add the ability to stop a worker to your own code by having the worker code check regularly to see if it should stop, similar to job cancellation.

Is Sidekiq multithreaded?

At the same time, Sidekiq uses multithreading so it is much more memory-efficient than Rescue.


1 Answers

You can write a method:

def wait_for_sidekiq
  sleep(1) until Sidekiq::Workers.new.size == 0 && Sidekiq::Queue.new.size == 0
end

I would also suggest that you make sure that the jobs got queued in the first place:

def wait_for_queuing
  sleep(1) until Sidekiq::Queue.new.size > 0 || Sidekiq::Workers.new.size > 0
end

This is required, because sometimes the first method might get executed a few miliseconds before queuing the jobs, so that it won't wait at all.

like image 103
Alexander Popov Avatar answered Sep 19 '22 23:09

Alexander Popov