Class: Houston::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/houston/boot/timer.rb

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.



34
35
36
37
38
39
40
# File 'lib/houston/boot/timer.rb', line 34

def initialize
  @queued_timers = Concurrent::Array.new

  Houston.observer.on "daemon:scheduler:start", raise: true do
    schedule_queued_timers!
  end
end

Instance Method Details

#every(interval, &block) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
# File 'lib/houston/boot/timer.rb', line 42

def every(interval, &block)
  return schedule_later :every, interval, block unless $scheduler

  match = Attentive::Matcher.match!(TRIGGER_PHRASE, interval)
  raise ArgumentError, "Unrecognized interval: #{interval.inspect}" unless match
  method_name, argument = match["houston.trigger.every"]
  $scheduler.public_send method_name, argument, &block
end

#stop(interval, block) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/houston/boot/timer.rb', line 51

def stop(interval, block)
  return queued_timers.delete [:every, interval, block] unless $scheduler

  # Look up the job by its handler
  # Note: this doesn't check `interval`. Conceivably, two jobs could be
  # set up at different intervals that both invoke the same block.
  job = $scheduler.jobs.detect { |job| job.handler == block }
  job.unschedule
end