module Sidekiq::DeferredJobs
Constants
- Job
Data structure to hold job information.
Public Class Methods
Defer enqueuing Sidekiq
workers within the block until the end of the block. Any workers that normally would have been enqueued with a `perform_async` call will instead be queued up and run in an ensure clause at the end of the block. @param filter [Array<Module>, Array<Hash>] An array of either classes, modules, or hashes.
If this is provided, only workers that match either a class or module or which have sidekiq_options that match a hash will be deferred. All other worker will be enqueued as normal.
@return [void]
# File lib/sidekiq/deferred_jobs.rb, line 15 def defer(filter, &block) jobs, filters = Thread.current[:sidekiq_deferred_jobs_jobs] unless jobs filters = [] jobs = Jobs.new Thread.current[:sidekiq_deferred_jobs_jobs] = [jobs, filters] end filters.push(Filter.new(filter)) begin yield ensure filters.pop if filters.empty? Thread.current[:sidekiq_deferred_jobs_jobs] = nil jobs.enqueue! end end end
Return true if the specified class with optional options should be deferred. @param klass [Class] A Sidekiq
worker class @param opts [Hash, Nil] Optionsl options set at runtime for the worker. @return Boolean
# File lib/sidekiq/deferred_jobs.rb, line 51 def defer?(klass, opts = nil) _jobs, filters = Thread.current[:sidekiq_deferred_jobs_jobs] return false if filters.nil? filters.any? { |filter| filter.match?(klass, opts) } end
Schedule a worker to be run at the end of the outermost defer block. @param klass [Class] Sidekiq
worker class @param args [Array] Sidekiq
job arguments @param opts [Hash, Nil] Optional sidekiq options specified for the job @return [void]
# File lib/sidekiq/deferred_jobs.rb, line 62 def defer_worker(klass, args, opts = nil) jobs, _filters = Thread.current[:sidekiq_deferred_jobs_jobs] if jobs jobs.defer(klass, args, opts) else klass.perform_async(*args) end end
Disable deferred workers within the block. All workers will be enqueued normally within the block. @return [void]
# File lib/sidekiq/deferred_jobs.rb, line 37 def undeferred(&block) save_val = Thread.current[:sidekiq_deferred_jobs_jobs] begin Thread.current[:sidekiq_deferred_jobs_jobs] = nil yield ensure Thread.current[:sidekiq_deferred_jobs_jobs] = save_val end end