class Emque::Consuming::Cli

Constants

APP_CONFIG_FILE
COMMANDS
IP_REGEX

Attributes

argv[RW]
command[R]
options[R]
parser[R]
runner[RW]

Public Class Methods

new(argv) click to toggle source
# File lib/emque/consuming/cli.rb, line 14
def initialize(argv)
  self.argv = argv

  extract_command
  intercept_help

  load_app
  setup_options
  parse_options

  execute
end

Private Instance Methods

execute() click to toggle source
# File lib/emque/consuming/cli.rb, line 32
def execute
  if command == :new
    Emque::Consuming::Generators::Application.new(options, argv.last).generate
  else
    self.runner = Emque::Consuming::Runner.new(options)
    runner.send(command)
  end
end
extract_command() click to toggle source
# File lib/emque/consuming/cli.rb, line 41
def extract_command
  if argv.size > 1 and argv[-2] == "new"
    @command = :new
  elsif argv.size > 0
    @command = argv[-1].to_sym
  end
end
intercept_help() click to toggle source
# File lib/emque/consuming/cli.rb, line 49
def intercept_help
  if command == :new and argv.last.to_sym == command
    argv << "--help"
  elsif ! COMMANDS.include?(command)
    argv << "--help"
  end
end
load_app() click to toggle source
# File lib/emque/consuming/cli.rb, line 57
def load_app
  current_dir = Dir.pwd

  if File.exist?(File.join(current_dir, APP_CONFIG_FILE))
    require_relative File.join(current_dir, APP_CONFIG_FILE)
  end
end
parse_options() click to toggle source
# File lib/emque/consuming/cli.rb, line 65
def parse_options
  parser.parse!(argv)
end
setup_options() click to toggle source
# File lib/emque/consuming/cli.rb, line 69
def setup_options
  @options = {
    :daemon => false
  }

  @parser = OptionParser.new { |o|
    o.on("-P", "--pidfile PATH", "Store pid in PATH") do |arg|
      options[:pidfile] = arg
    end

    o.on(
      "-S",
      "--socket PATH",
      "PATH to the application's unix socket"
    ) do |arg|
      options[:socket_path] = arg
    end

    o.on(
      "-b",
      "--bind IP:PORT",
      "IP & port for the http status application to listen on."
    ) do |arg|
      ip, port = arg.split(":")
      port = port.to_i
      options[:status_host] = ip if ip =~ IP_REGEX
      options[:status_port] = port if port > 0 && port <= 65535
    end

    o.on("-d", "--daemon", "Daemonize the application") do
      options[:daemon] = true
    end

    o.on(
      "-e",
      "--error-limit N",
      "Set the max errors before application suicide"
    ) do |arg|
      limit = arg.to_i
      options[:error_limit] = limit if limit > 0
    end

    o.on("-s", "--status", "Run the http status application") do
      options[:status] = :on
    end

    o.on(
      "-x",
      "--error-expiration SECONDS",
      "Expire errors after SECONDS"
    ) do |arg|
      exp = arg.to_i
      options[:error_expiration] = exp if exp > 0
    end

    o.on("--app-name NAME", "Run the application as NAME") do |arg|
      options[:app_name] = arg
    end

    o.on(
      "--env (ex. production)",
      "Set the application environment, overrides EMQUE_ENV"
    ) do |arg|
      options[:env] = arg
    end

    o.on(
      "--auto-shutdown (false|true)",
      "Enable or disable auto shutdown on reaching the error limit"
    ) do |arg|
      exp = arg.to_s
      if exp == "true"
        options[:auto_shutdown] = true
      else
        options[:auto_shutdown] = false
      end
    end

    o.on(
      "--retry-error-limit N",
      "Max number of times to retry a retryable error"
    ) do |arg|
      retry_limit = arg.to_i
      options[:retryable_error_limit] = retry_limit if retry_limit > 0
    end

    o.banner = "emque <options> (start|stop|new|console|help) <name (new only)>"
  }
end