Get sidekiq to execute a job immediately

RubySidekiq

Ruby Problem Overview


At the moment, I have a sidekiq job like this:

class SyncUser
  include Sidekiq::Worker

  def perform(user_id)
    #do stuff
  end
end

I am placing a job on the queue like this:

SyncUser.perform_async user.id

This all works of course but there is a bit of a lag between calling perform_async and the job actually getting executed.

Is there anything else I can do to tell sidekiq to execute the job immediately?

Ruby Solutions


Solution 1 - Ruby

There are two questions here.

If you want to execute a job immediately, in the current context you can use:

SyncUser.new.perform(user.id)

If you want to decrease the delay between asynchronous work being scheduled and when it's executed in the sidekiq worker, you can decrease the poll_interval setting:

Sidekiq.configure_server do |config|
  config.poll_interval = 2
end

The poll_interval is the delay within worker backends of how frequently workers check for jobs on the queue. The average time between a job being scheduled and executed with a free worker will be poll_interval / 2.

Solution 2 - Ruby

For those who are using Sidekiq via the Active Job framework, you can do

SyncUser.perform_now(user.id)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondagda1View Question on Stackoverflow
Solution 1 - RubyWinfieldView Answer on Stackoverflow
Solution 2 - RubyDaniel MorrisView Answer on Stackoverflow