Get Sidekiq to Execute a Job Immediately

Get sidekiq to execute a job immediately

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.

Sidekiq - Enqueuing a job to be performed 0.seconds from now

There is no difference. It looks like this is already considered in the Sidekiq library.

https://github.com/mperham/sidekiq/blob/main/lib/sidekiq/worker.rb#L261

# Optimization to enqueue something now that is scheduled to go out now or in the past
@opts["at"] = ts if ts > now

Perform sidekiq job synchronously


SidekiqWorker.new.perform

Voila!

Rails 6 grab results from Sidekiq Job - is it possible?

I don't believe what you want is possible.

https://github.com/mperham/sidekiq/issues/3532

The return value will be GC'd like any other unused data in a Ruby process. Jobs do not have a "result" in Sidekiq and Sidekiq does nothing with the value.

You would need some sort of model instead that keeps track of your background tasks. This is off the cuff but should give you an idea.

EG

# @attr result [Array]
# @attr status [String] Values of 'Pending', 'Error', 'Complete', etc..
class BackgroundTask < ActiveRecord
attr_accessor :product_codes
after_create :enqueue

def enqueue
::Imports::SyncProductsWorker.perform_async(product_codes, self.id)
end
end

def perform(list, id)
response = ::Imports::SynchronizeProducts.new(list).call
if (response.has_errors?)
BackgroundTask.find(id).update(status: 'Error', result: response)
else
BackgroundTask.find(id).update(status: 'Complete', result: response)
end
end

Then just use the BackgroundTask model for your frontend display.



Related Topics



Leave a reply



Submit