Module: DaimonSkycrawlers::Timer

Defined in:
lib/daimon_skycrawlers/timer.rb

Overview

Name space for timer

Class Method Summary collapse

Class Method Details

.setup_shutdown_timer(queue_name_prefix, interval: 10) ⇒ Timers::Group

Setup timer for shutdown

Parameters:

  • queue_name_prefix (String)

    previx of queue name

  • interval (String) (defaults to: 10)

    shutdown after this interval after the queue is empty

Returns:

  • (Timers::Group)

    timers



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/daimon_skycrawlers/timer.rb', line 17

def setup_shutdown_timer(queue_name_prefix, interval: 10)
  timers = Timers::Group.new
  timer = timers.after(interval) do
    if block_given?
      yield
    else
      Process.kill(:INT, 0)
    end
  end
  Thread.new(timers) do |t|
    loop { t.wait }
  end
  ActiveSupport::Notifications.subscribe("consume_message.songkick_queue") do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    queue_name = event.payload[:queue_name]
    DaimonSkycrawlers.configuration.logger.debug("Reset timer: consume message #{queue_name}")
    timer.reset
  end
  timers
end