class Canto::CLI

Constants

SIGNAL_HANDLERS
UNHANDLED_SIGNAL_HANDLER

Attributes

logger[RW]
environment[RW]

Public Instance Methods

boot_application() click to toggle source
# File lib/canto/cli.rb, line 117
def boot_application
  ENV['RACK_ENV'] = ENV['RAILS_ENV'] = environment

  require options[:require]
end
handle_signal(sig) click to toggle source
# File lib/canto/cli.rb, line 82
def handle_signal(sig)
  CLI.logger.debug "Got #{sig} signal"
  SIGNAL_HANDLERS[sig].call(self)
end
initialize_logger() click to toggle source
# File lib/canto/cli.rb, line 113
def initialize_logger
  CLI.logger = Logger.new($stdout, level: Logger::INFO)
end
launch(self_read) click to toggle source
# File lib/canto/cli.rb, line 40
def launch(self_read)
  begin
    while (readable_io = IO.select([self_read]))
      signal = readable_io.first[0].gets.strip
      handle_signal(signal)
    end
  rescue Interrupt
    CLI.logger.info 'Shutting down'
    CLI.logger.info 'Bye!'

    # Explicitly exit so busy Processor threads won't block process shutdown.
    #
    # NB: slow at_exit handlers will prevent a timely exit if they take
    # a while to run. If Sidekiq is getting here but the process isn't exiting,
    # use the TTIN signal to determine where things are stuck.
    exit(0)
  end
end
option_parser(opts) click to toggle source
# File lib/canto/cli.rb, line 135
def option_parser(opts)
  parser = OptionParser.new { |o|
    o.on '-e', '--environment ENV', 'Application environment' do |arg|
      opts[:environment] = arg
    end

    o.on '-r', '--require [PATH]', 'Location of ruby file to require' do |arg|
      opts[:require] = arg
    end

    o.on '-V', '--version', 'Print version and exit' do |arg|
      puts "Canto #{Canto::VERSION}"
      die(0)
    end
  }

  parser.banner = 'canto [options]'
  parser.on_tail '-h', '--help', 'Show help' do
    CLI.logger.info parser
    die 1
  end

  parser
end
options() click to toggle source
# File lib/canto/cli.rb, line 87
def options
  @options ||= Canto::DEFAULTS
end
parse(args = ARGV) click to toggle source
# File lib/canto/cli.rb, line 18
def parse(args = ARGV)
  initialize_logger
  setup_options(args)
  validate!
end
parse_options(argv) click to toggle source
# File lib/canto/cli.rb, line 100
def parse_options(argv)
  opts = {}
  @parser = option_parser(opts)
  @parser.parse!(argv)
  opts
end
run(boot_app: true) click to toggle source
# File lib/canto/cli.rb, line 24
def run(boot_app: true)
  boot_application if boot_app

  self_read, self_write = IO.pipe
  sigs = %w[INT TERM TTIN TSTP]
  sigs.each do |sig|
    trap sig do
      self_write.puts(sig)
    end
  rescue ArgumentError
    puts "Signal #{sig} not supported"
  end

  launch(self_read)
end
set_environment(cli_env) click to toggle source
# File lib/canto/cli.rb, line 109
def set_environment(cli_env)
  @environment = cli_env || ENV['APP_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
end
setup_options(args) click to toggle source
# File lib/canto/cli.rb, line 91
def setup_options(args)
  # parse CLI options
  opts = parse_options(args)

  set_environment opts[:environment]

  options.merge!(opts)
end
validate!() click to toggle source
# File lib/canto/cli.rb, line 123
def validate!
  if !File.exist?(options[:require]) ||
      (File.directory?(options[:require]) && !File.exist?("#{options[:require]}/config/application.rb"))
    CLI.logger.info '=================================================================='
    CLI.logger.info '  Please point Canto to Ruby file  '
    CLI.logger.info '  to load your classes with -r [FILE].'
    CLI.logger.info '=================================================================='
    CLI.logger.info @parser
    die(1)
  end
end