Class: Sidekiq::Throttled::Strategy

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/throttled/strategy.rb,
lib/sidekiq/throttled/strategy/script.rb,
lib/sidekiq/throttled/strategy/threshold.rb,
lib/sidekiq/throttled/strategy/concurrency.rb

Overview

Meta-strategy that couples Concurrency and Threshold strategies.

Defined Under Namespace

Classes: Concurrency, Threshold

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, concurrency: nil, threshold: nil) ⇒ Strategy

Returns a new instance of Strategy.

Parameters:



23
24
25
26
27
28
29
30
31
32
# File 'lib/sidekiq/throttled/strategy.rb', line 23

def initialize(key, concurrency: nil, threshold: nil)
  base_key      = "throttled:#{key}"

  @concurrency  = concurrency && Concurrency.new(base_key, concurrency)
  @threshold    = threshold && Threshold.new(base_key, threshold)

  return if @concurrency || @threshold

  fail ArgumentError, "Neither :concurrency nor :threshold given"
end

Instance Attribute Details

#concurrencyObject (readonly)

Returns the value of attribute concurrency.



12
13
14
# File 'lib/sidekiq/throttled/strategy.rb', line 12

def concurrency
  @concurrency
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



16
17
18
# File 'lib/sidekiq/throttled/strategy.rb', line 16

def threshold
  @threshold
end

Instance Method Details

#finalize!(jid) ⇒ void

This method returns an undefined value.

Marks job as being processed.



48
49
50
# File 'lib/sidekiq/throttled/strategy.rb', line 48

def finalize!(jid)
  @concurrency && @concurrency.finalize!(jid)
end

#reset!void

This method returns an undefined value.

Resets count of jobs of all avaliable strategies



54
55
56
57
# File 'lib/sidekiq/throttled/strategy.rb', line 54

def reset!
  @concurrency && @concurrency.reset!
  @threshold && @threshold.reset!
end

#throttled?(jid) ⇒ Boolean

Returns whenever job is throttled or not.

Returns:

  • (Boolean)

    whenever job is throttled or not.



35
36
37
38
39
40
41
42
43
44
# File 'lib/sidekiq/throttled/strategy.rb', line 35

def throttled?(jid)
  return true if @concurrency && @concurrency.throttled?(jid)

  if @threshold && @threshold.throttled?
    finalize! jid
    return true
  end

  false
end