Class: ActiveJob::Status::Throttle

Inherits:
Object
  • Object
show all
Defined in:
lib/activejob-status/throttle.rb

Instance Method Summary collapse

Constructor Details

#initialize(interval) ⇒ Throttle

Returns a new instance of Throttle.



6
7
8
9
# File 'lib/activejob-status/throttle.rb', line 6

def initialize(interval)
  @interval = interval
  @started_at = Time.current
end

Instance Method Details

#wrap(force: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/activejob-status/throttle.rb', line 11

def wrap(force: false)
  return yield if force || @interval.nil? || @interval.zero?

  now = Time.current
  elasped = now - @started_at
  return if @interval > elasped

  yield
  @started_at = now
end