Class: Upperkut::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/upperkut/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV) ⇒ CLI

Returns a new instance of CLI.



8
9
10
11
12
13
# File 'lib/upperkut/cli.rb', line 8

def initialize(args = ARGV)
  @options = {}
  @logger = Upperkut::Logging.logger

  parse_options(args)
end

Instance Method Details

#startObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/upperkut/cli.rb', line 15

def start
  if target_required = @options[:require]
    if File.directory?(target_required)
      require 'rails'
      if ::Rails::VERSION::MAJOR == 4
        require File.expand_path("#{@options[:require]}/config/application.rb")
        ::Rails::Application.initializer 'upperkut.eager_load' do
          ::Rails.application.config.eager_load = true
        end

        require File.expand_path("#{@options[:require]}/config/environment.rb")
      else
        require File.expand_path("#{@options[:require]}/config/environment.rb")
      end
    else
      require target_required
    end
  end

  if log_level = @options[:log_level]
    @logger.level = log_level
  end

  @options[:logger] = @logger

  manager = Manager.new(@options)

  @logger.info(@options)

  r, w = IO.pipe
  signals = %w[INT TERM]

  signals.each do |signal|
    trap signal do
      w.puts(signal)
    end
  end

  begin
    manager.run
    while readable_io = IO.select([r])
      signal = readable_io.first[0].gets.strip
      handle_signal(signal)
    end
  rescue Interrupt
    @logger.info(
      'Stopping managers, wait for 5 seconds and them kill processors'
    )

    manager.stop
    sleep(Integer(ENV['UPPERKUT_TIMEOUT'] || 8))
    manager.kill
    exit(0)
  end
end