Class: GoodJob::ActiveJobExtensions::Concurrency::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/good_job/active_job_extensions/concurrency.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Rule

Returns a new instance of Rule.



21
22
23
24
25
26
27
28
29
# File 'lib/good_job/active_job_extensions/concurrency.rb', line 21

def initialize(config)
  @label = config[:label]
  @key = config.key?(:key) ? config[:key] : GoodJob::NONE
  @total_limit = config[:total_limit]
  @enqueue_limit = config[:enqueue_limit]
  @perform_limit = config[:perform_limit]
  @enqueue_throttle = config[:enqueue_throttle]
  @perform_throttle = config[:perform_throttle]
end

Instance Attribute Details

#enqueue_limitObject (readonly)

Returns the value of attribute enqueue_limit.



19
20
21
# File 'lib/good_job/active_job_extensions/concurrency.rb', line 19

def enqueue_limit
  @enqueue_limit
end

#enqueue_throttleObject (readonly)

Returns the value of attribute enqueue_throttle.



19
20
21
# File 'lib/good_job/active_job_extensions/concurrency.rb', line 19

def enqueue_throttle
  @enqueue_throttle
end

#labelObject (readonly)

Returns the value of attribute label.



19
20
21
# File 'lib/good_job/active_job_extensions/concurrency.rb', line 19

def label
  @label
end

#perform_limitObject (readonly)

Returns the value of attribute perform_limit.



19
20
21
# File 'lib/good_job/active_job_extensions/concurrency.rb', line 19

def perform_limit
  @perform_limit
end

#perform_throttleObject (readonly)

Returns the value of attribute perform_throttle.



19
20
21
# File 'lib/good_job/active_job_extensions/concurrency.rb', line 19

def perform_throttle
  @perform_throttle
end

#total_limitObject (readonly)

Returns the value of attribute total_limit.



19
20
21
# File 'lib/good_job/active_job_extensions/concurrency.rb', line 19

def total_limit
  @total_limit
end

Instance Method Details

#evaluate(job, stage) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/good_job/active_job_extensions/concurrency.rb', line 35

def evaluate(job, stage)
  resolved_key = resolve_key(job)
  resolved_label = resolve_label(job)
  return nil if resolved_key.blank? && resolved_label.blank?

  if stage == :enqueue
    enqueue_limit = resolve_limit(job, @enqueue_limit) || resolve_limit(job, @total_limit)
    enqueue_throttle = resolve_throttle(job, @enqueue_throttle)
    return nil unless enqueue_limit || enqueue_throttle

    check_enqueue(enqueue_limit, enqueue_throttle, job, resolved_key, resolved_label, enqueue_limit_flag: @enqueue_limit.present?)
  elsif stage == :perform
    perform_limit = resolve_limit(job, @perform_limit) || resolve_limit(job, @total_limit)
    perform_throttle = resolve_throttle(job, @perform_throttle)
    return nil unless perform_limit || perform_throttle

    check_perform(perform_limit, perform_throttle, job, resolved_key, resolved_label)
  end
end

#keyObject



31
32
33
# File 'lib/good_job/active_job_extensions/concurrency.rb', line 31

def key
  @key.equal?(GoodJob::NONE) ? nil : @key
end