module Sidekiq::Throttled
Concurrency and threshold throttling for Sidekiq
.
Just add somewhere in your bootstrap:
require "sidekiq/throttled"
Once you’ve done that you can include {Sidekiq::Throttled::Job} to your job classes and configure throttling:
class MyJob include Sidekiq::Job include Sidekiq::Throttled::Job sidekiq_options :queue => :my_queue sidekiq_throttle({ # Allow maximum 10 concurrent jobs of this class at a time. :concurrency => { :limit => 10 }, # Allow maximum 1K jobs being processed within one hour window. :threshold => { :limit => 1_000, :period => 1.hour } }) def perform # ... end end
Constants
- MUTEX
- VERSION
-
Gem version
- Worker
-
Adds helpers to your worker classes
@example Usage
class MyJob include Sidekiq::Job include Sidekiq::Throttled::Job sidkiq_options :queue => :my_queue sidekiq_throttle :threshold => { :limit => 123, :period => 1.hour }, :requeue => { :to => :other_queue, :with => :schedule } def perform # ... end end
@see ClassMethods
Attributes
@api internal
@return [Config, nil]
@api internal
@return [Cooldown, nil]
Public Class Methods
Source
# File lib/sidekiq/throttled.rb, line 68 def configure MUTEX.synchronize do config = @config.dup yield config @config = config.freeze @cooldown = Cooldown[@config] end end
@example
Sidekiq::Throttled.configure do |config| config.cooldown_period = nil # Disable queues cooldown manager end
@yieldparam config [Config]
Source
# File lib/sidekiq/throttled.rb, line 100 def requeue_throttled(work) message = JSON.parse(work.job) job_class = Object.const_get(message.fetch("wrapped") { message.fetch("class") { return false } }) Registry.get job_class do |strategy| strategy.requeue_throttled(work) end end
Return throttled job to be executed later, delegating the details of how to do that to the Strategy
for that job.
@return [void]
Source
# File lib/sidekiq/throttled.rb, line 83 def throttled?(message) message = Message.new(message) return false unless message.job_class && message.job_id Registry.get(message.job_class) do |strategy| return strategy.throttled?(message.job_id, *message.job_args) end false rescue StandardError false end
Tells whenever job is throttled or not.
@param [String] message Job’s JSON payload @return [Boolean]