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
Rails Put Validation in a Module Mixin
How to Remove '---' on Top of a Yaml File
What Is the Best Practice When It Comes to Testing "Infinite Loops"
Nested Forms in Rails - Accessing Attribute in Has_Many Relation
The Compiler Failed to Generate an Executable File. (Runtimeerror)
Why Do I Get an Error Installing the JSON Gem in Ubuntu
How to Traverse Symlinked Directories in Ruby with a "**" Glob
Why Can't I Access a Local Variable Inside a Method in Ruby
How to Disable Db:Schema:Dump for Migrations
Elasticsearch & Tire: Using Mapping and To_Indexed_JSON
Detect Key Press (Non-Blocking) W/O Getc/Gets in Ruby
Selenium Scroll Element into (Center Of) View
Why Is Enumerable#Each_With_Object Deprecated
How to Downgrade from Ruby 1.9.2 to Ruby 1.8.7 to Run Rails 2.0.2