class FastlyNsq::Launcher

FastlyNsq::Launcher is a lighweight wrapper of a thread manager and heartbeat.

This class is used internally by FastlyNsq::CLI.

Attributes

logger[R]
pulse[RW]
timeout[R]

Public Class Methods

new(timeout: 5, pulse: 5, logger: FastlyNsq.logger, **options) click to toggle source
# File lib/fastly_nsq/launcher.rb, line 20
def initialize(timeout: 5, pulse: 5, logger: FastlyNsq.logger, **options)
  @done    = false
  @timeout = timeout
  @pulse   = pulse
  @logger  = logger

  FastlyNsq.manager = FastlyNsq::Manager.new(options)
  FastlyNsq.fire_event :startup
end

Public Instance Methods

beat() click to toggle source
# File lib/fastly_nsq/launcher.rb, line 30
def beat
  @heartbeat ||= safe_thread('heartbeat', &method(:start_heartbeat))
end
manager() click to toggle source
# File lib/fastly_nsq/launcher.rb, line 16
def manager
  FastlyNsq.manager
end
stop() click to toggle source
# File lib/fastly_nsq/launcher.rb, line 34
def stop
  @done = true
  manager.terminate(timeout)
  FastlyNsq.fire_event :shutdown
end
stop_listeners() click to toggle source
# File lib/fastly_nsq/launcher.rb, line 40
def stop_listeners
  @done = true
  manager.stop_listeners
end
stopping?() click to toggle source
# File lib/fastly_nsq/launcher.rb, line 45
def stopping?
  @done
end

Private Instance Methods

heartbeat() click to toggle source
# File lib/fastly_nsq/launcher.rb, line 51
def heartbeat
  logger.debug do
    [
      'HEARTBEAT:',
      'busy:', manager.pool.length,
      'processed:', manager.pool.completed_task_count,
      'max_threads:', manager.pool.max_length,
      'max_queue_size:', manager.pool.largest_length,
      'listeners:', manager.listeners.count
    ].join(' ')
  end

  # TODO: Check the health of the system overall and kill it if needed
  #       ::Process.kill('dieing because...', $$)
rescue => e
  logger.error "Heartbeat error: #{e.message}"
ensure
  FastlyNsq.fire_event :heartbeat
end
start_heartbeat() click to toggle source
# File lib/fastly_nsq/launcher.rb, line 71
def start_heartbeat
  until manager.stopped?
    heartbeat
    sleep pulse
  end
  logger.info('Heartbeat stopping...')
end