Class: Cuniculus::CLI

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/cuniculus/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/cuniculus/cli.rb', line 15

def options
  @options
end

Instance Method Details

#handle_signal(sig) ⇒ Object



64
65
66
67
68
69
# File 'lib/cuniculus/cli.rb', line 64

def handle_signal(sig)
  case sig
  when "INT", "TERM"
    raise Interrupt
  end
end

#launch(pipe_reader) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cuniculus/cli.rb', line 43

def launch(pipe_reader)
  config = Cuniculus.config
  supervisor = Cuniculus::Supervisor.new(config)

  begin
    Cuniculus.logger.info("Starting process")
    supervisor.start

    while (readable_io = IO.select([pipe_reader]))
      signal = readable_io.first[0].gets.strip
      handle_signal(signal)
    end
  rescue Interrupt
    Cuniculus.logger.info("Interrupt received; shutting down")
    supervisor.stop
    Cuniculus.logger.info("Shutdown complete")
  end

  exit(0)
end

#parse(args = ARGV) ⇒ Object

Raises:

  • (ArgumentError)


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

def parse(args = ARGV)
  @options = parse_options(args)

  return unless options[:require]

  raise ArgumentError, "Invalid '--require' argument: #{options[:require]}. File does not exist" unless File.exist?(options[:require])
  raise ArgumentError, "Invalid '--require' argument: #{options[:require]}. Cannot be a directory" if File.directory?(options[:require])
  require File.join(Dir.pwd, options[:require])
end

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cuniculus/cli.rb', line 27

def run
  pipe_reader, pipe_writer = IO.pipe
  sigs = %w[INT TERM]

  sigs.each do |sig|
    trap sig do
      pipe_writer.puts(sig)
    end
  rescue ArgumentError
    puts "Signal #{sig} not supported"
  end


  launch(pipe_reader)
end