Class: Geary::CLI

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

Constant Summary collapse

Shutdown =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdout = STDOUT, stderr = STDERR, kernel = Kernel, pipe = IO.pipe) ⇒ CLI

Returns a new instance of CLI.



16
17
18
19
20
21
22
23
24
# File 'lib/geary/cli.rb', line 16

def initialize(argv, stdout = STDOUT, stderr = STDERR, kernel = Kernel,
               pipe = IO.pipe)
  @argv = argv
  @stdout = stdout
  @stdout = stderr
  @kernel = kernel
  @internal_signal_queue, @external_signal_queue = pipe
  @configuration = OptionParser.new.parse(@argv)
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



14
15
16
# File 'lib/geary/cli.rb', line 14

def configuration
  @configuration
end

#external_signal_queueObject (readonly)

Returns the value of attribute external_signal_queue.



14
15
16
# File 'lib/geary/cli.rb', line 14

def external_signal_queue
  @external_signal_queue
end

#internal_signal_queueObject (readonly)

Returns the value of attribute internal_signal_queue.



14
15
16
# File 'lib/geary/cli.rb', line 14

def internal_signal_queue
  @internal_signal_queue
end

Instance Method Details

#execute!Object



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
# File 'lib/geary/cli.rb', line 26

def execute!
  Celluloid.logger.level = configuration.log_level

  %w(INT TERM).each do |signal|
    trap signal do
      external_signal_queue.puts(signal)
    end
  end

  munge_environment_given(configuration)
  load_rails

  manager = Manager.new(configuration: configuration)
  manager.start

  begin
    loop do
      IO.select([internal_signal_queue])
      signal = internal_signal_queue.gets.strip

      handle(signal)
    end
  rescue Shutdown
    manager.async.stop

    manager.wait(:done)

    @kernel.exit(0)
  end
end

#handle(signal) ⇒ Object



57
58
59
60
61
# File 'lib/geary/cli.rb', line 57

def handle(signal)
  if %w(INT TERM).include?(signal)
    raise Shutdown
  end
end