class RabbitFeed::Client

Constants

DEFAULTS

Attributes

command[R]
options[R]

Public Class Methods

new(arguments = ARGV) click to toggle source
# File lib/rabbit_feed/client.rb, line 26
def initialize(arguments = ARGV)
  @command = arguments[0]
  @options = parse_options arguments
  return if shutdown?
  validate!
  set_logging
  set_configuration
  load_dependancies unless console?
end

Public Instance Methods

run() click to toggle source
# File lib/rabbit_feed/client.rb, line 36
def run
  send(command)
end

Private Instance Methods

config_file_exists() click to toggle source
# File lib/rabbit_feed/client.rb, line 50
def config_file_exists
  errors.add(:options, "configuration file not found: '#{options[:config_file]}', specify this using the --config option") unless File.exist?(options[:config_file])
end
console() click to toggle source
# File lib/rabbit_feed/client.rb, line 72
def console
  require_relative 'console_consumer'
  ConsoleConsumer.init
  consume
end
console?() click to toggle source
# File lib/rabbit_feed/client.rb, line 132
def console?
  command == 'console'
end
consume() click to toggle source
# File lib/rabbit_feed/client.rb, line 67
def consume
  daemonize if daemonize?
  RabbitFeed::Consumer.run
end
daemonize() click to toggle source
# File lib/rabbit_feed/client.rb, line 114
def daemonize
  Process.daemon(true, true)
  pid_path = File.split options[:pidfile]
  PidFile.new(piddir: pid_path[0], pidfile: pid_path[1])
end
daemonize?() click to toggle source
# File lib/rabbit_feed/client.rb, line 128
def daemonize?
  options[:daemon]
end
environment() click to toggle source
# File lib/rabbit_feed/client.rb, line 120
def environment
  [options[:environment], ENV['RACK_ENV'], ENV['RAILS_ENV']].detect(&:present?)
end
environment_specified() click to toggle source
# File lib/rabbit_feed/client.rb, line 63
def environment_specified
  errors.add(:options, '--environment not specified') unless environment.present?
end
load_dependancies() click to toggle source
# File lib/rabbit_feed/client.rb, line 104
def load_dependancies
  if require_rails?
    require 'rails'
    require File.expand_path("#{options[:require_path]}/config/environment.rb")
    ::Rails.application.eager_load!
  else
    require options[:require_path]
  end
end
log_file_path_exists() click to toggle source
# File lib/rabbit_feed/client.rb, line 46
def log_file_path_exists
  errors.add(:options, "log file path not found: '#{options[:logfile]}', specify this using the --logfile option") unless Dir.exist?(File.dirname(options[:logfile]))
end
parse_options(argv) click to toggle source
# File lib/rabbit_feed/client.rb, line 144
def parse_options(argv)
  opts = {}

  parser = OptionParser.new do |o|
    o.on '-a', '--application VAL', 'Name of the application' do |arg|
      opts[:application] = arg
    end

    o.on '-m', '--payload VAL', 'Payload of event to produce' do |arg|
      opts[:payload] = arg
    end

    o.on '-n', '--name VAL', 'Name of event to produce' do |arg|
      opts[:name] = arg
    end

    o.on '-d', '--daemon', 'Daemonize process' do |arg|
      opts[:daemon] = arg
    end

    o.on '-e', '--environment ENV', 'Application environment' do |arg|
      opts[:environment] = arg
    end

    o.on '-r', '--require [PATH|DIR]', 'Location of Rails application with workers or file to require' do |arg|
      opts[:require_path] = arg
    end

    o.on '-v', '--verbose', 'Print more verbose output' do |arg|
      opts[:verbose] = arg
    end

    o.on '-C', '--config PATH', 'Path to YAML config file' do |arg|
      opts[:config_file] = arg
    end

    o.on '-L', '--logfile PATH', 'Path to writable logfile' do |arg|
      opts[:logfile] = arg
    end

    o.on '-P', '--pidfile PATH', 'Path to pidfile' do |arg|
      opts[:pidfile] = arg
    end

    o.on '-V', '--version', 'Print version and exit' do |_arg|
      puts "RabbitFeed #{RabbitFeed::VERSION}"
      exit 0
    end
  end

  parser.banner = 'rabbit_feed action [options]'
  parser.on_tail '-h', '--help', 'Show help' do
    puts parser
    exit 1
  end
  parser.parse! argv
  DEFAULTS.merge opts
end
pid() click to toggle source
# File lib/rabbit_feed/client.rb, line 82
def pid
  File.read(options[:pidfile]).to_i
end
pidfile_path_exists() click to toggle source
# File lib/rabbit_feed/client.rb, line 59
def pidfile_path_exists
  errors.add(:options, "pid file path not found: '#{options[:pidfile]}', specify this using the --pidfile option") unless Dir.exist?(File.dirname(options[:pidfile]))
end
produce() click to toggle source
# File lib/rabbit_feed/client.rb, line 86
def produce
  RabbitFeed::Producer.publish_event options[:name], options[:payload]
end
require_path_valid() click to toggle source
# File lib/rabbit_feed/client.rb, line 54
def require_path_valid
  return unless require_rails? && !File.exist?("#{options[:require_path]}/config/application.rb")
  errors.add(:options, 'point rabbit_feed to a Rails 3/4 application or a Ruby file to load your worker classes with --require')
end
require_rails?() click to toggle source
# File lib/rabbit_feed/client.rb, line 140
def require_rails?
  File.directory?(options[:require_path])
end
set_configuration() click to toggle source
# File lib/rabbit_feed/client.rb, line 96
def set_configuration
  RabbitFeed.environment             = environment
  RabbitFeed.application             = options[:application]
  RabbitFeed.configuration_file_path = options[:config_file]
  ENV['RACK_ENV']  ||= RabbitFeed.environment
  ENV['RAILS_ENV'] ||= RabbitFeed.environment
end
set_logging() click to toggle source
# File lib/rabbit_feed/client.rb, line 90
def set_logging
  RabbitFeed.log           = Logger.new(options[:logfile], 10, 100.megabytes)
  RabbitFeed.log.level     = verbose? ? Logger::DEBUG : Logger::INFO
  RabbitFeed.log.formatter = RabbitFeed::JsonLogFormatter
end
shutdown() click to toggle source
# File lib/rabbit_feed/client.rb, line 78
def shutdown
  `kill -TERM #{pid}`
end
shutdown?() click to toggle source
# File lib/rabbit_feed/client.rb, line 124
def shutdown?
  command == 'shutdown'
end
validate!() click to toggle source
# File lib/rabbit_feed/client.rb, line 42
def validate!
  raise Error, errors.messages if invalid?
end
verbose?() click to toggle source
# File lib/rabbit_feed/client.rb, line 136
def verbose?
  options[:verbose]
end