class Rabbitek::CLI

Rabbitek server CLI

Public Instance Methods

run() click to toggle source
# File lib/rabbitek/cli.rb, line 17
def run # rubocop:disable Metrics/AbcSize
  opts
  require_application

  Yabeda.configure! unless Yabeda.already_configured?

  map_consumer_workers!

  start_log

  io_r, io_w = IO.pipe
  SignalHandlers.setup(io_w)

  begin
    consumers = boot_consumers

    while io_resp = IO.select([io_r]) # rubocop:disable Lint/AssignmentInCondition
      SignalHandlers.public_send(io_resp.first.first.gets.strip)
    end
  rescue Interrupt
    execute_shutdown(consumers)
  end
end

Private Instance Methods

boot_consumers() click to toggle source
# File lib/rabbitek/cli.rb, line 82
def boot_consumers
  (1..configuration[:threads]).each_with_object([]) do |i, arr|
    debug "Booting thread ##{i}"
    arr << Starter.new(Rabbitek.bunny_connection, configuration).start
  end
end
configuration() click to toggle source
# File lib/rabbitek/cli.rb, line 65
def configuration
  @configuration ||= YAML.load_file(opts[:config]).with_indifferent_access
end
execute_shutdown(consumers) click to toggle source
# File lib/rabbitek/cli.rb, line 89
def execute_shutdown(consumers)
  info 'Shutting down gracefully...'

  consumers.map(&:cancel)
  Rabbitek.close_bunny_connection
  info 'Graceful shutdown completed'

  exit(0)
end
map_consumer_workers!() click to toggle source
# File lib/rabbitek/cli.rb, line 78
def map_consumer_workers!
  configuration[:consumers].map!(&:constantize)
end
opts() click to toggle source
# File lib/rabbitek/cli.rb, line 49
def opts
  @opts ||= Slop.parse do |o|
    o.string '-c', '--config', 'config file path. Default: "config/rabbitek.yaml"', default: 'config/rabbitek.yml'
    o.string '-r', '--require', 'rails app location or file to require while booting. Default: "."',
             default: '.'
    o.on '--version', 'print the version' do
      puts VERSION
      exit
    end
    o.on '-h', '--help' do
      puts o
      exit
    end
  end
end
require_application() click to toggle source
# File lib/rabbitek/cli.rb, line 69
def require_application
  require File.expand_path(opts[:require]) unless File.directory?(opts[:require])

  # Rails application provided
  require 'rails'
  require 'rabbitek/rails'
  require File.expand_path("#{opts[:require]}/config/environment.rb")
end
start_log() click to toggle source
# File lib/rabbitek/cli.rb, line 43
def start_log # rubocop:disable Metrics/AbcSize
  info "Rabbit consumers '[#{configuration[:consumers].map(&:to_s).join(', ')}]' started with PID #{Process.pid}"
  info "Client hooks: [#{Rabbitek.config.client_hooks.map(&:class).join(', ')}]"
  info "Server hooks: [#{Rabbitek.config.server_hooks.map(&:class).join(', ')}]"
end