Class: ProcessBalancer::CLI

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/process_balancer/cli.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#hostname, #identity, #logger, #redis, #start_thread, #watchdog

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



17
18
19
# File 'lib/process_balancer/cli.rb', line 17

def environment
  @environment
end

#managerObject (readonly)

Returns the value of attribute manager.



17
18
19
# File 'lib/process_balancer/cli.rb', line 17

def manager
  @manager
end

Class Method Details

.instanceObject



19
20
21
# File 'lib/process_balancer/cli.rb', line 19

def self.instance
  @instance ||= new
end

Instance Method Details

#parse(args = ARGV) ⇒ Object



23
24
25
26
27
# File 'lib/process_balancer/cli.rb', line 23

def parse(args = ARGV)
  setup_options(args)
  initialize_logger
  validate!
end

#runObject



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
70
71
72
73
# File 'lib/process_balancer/cli.rb', line 29

def run
  boot_system
  logger.info "Booted Rails #{::Rails.version} application in #{environment} environment" if rails_app?
  Thread.current.name = 'main'

  self_read, self_write = IO.pipe
  signals               = %w[INT TERM TTIN TSTP USR1 USR2]

  signals.each do |sig|
    trap sig do
      self_write.write("#{sig}\n")
    end
  rescue ArgumentError
    logger.info "Signal #{sig} not supported"
  end

  logger.info "Running in #{RUBY_DESCRIPTION}"

  if options[:job_sets].empty?
    logger.error 'No jobs configured! Configure your jobs in the configuration file.'
  else
    logger.info 'Configured jobs'
    options[:job_sets].each do |config|
      logger.info " - #{config[:id]}"
    end
  end

  @manager = ProcessBalancer::Manager.new(options)

  begin
    @manager.run

    while (readable_io = IO.select([self_read]))
      signal = readable_io.first[0].gets.strip
      handle_signal(signal)
    end
  rescue Interrupt
    logger.info 'Shutting down'
    @manager.stop
    logger.info 'Bye!'

    # Explicitly exit so busy Processor threads wont block process shutdown.
    exit(0)
  end
end