Class: Tincan::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



28
29
30
# File 'lib/tincan/cli.rb', line 28

def initialize
  @code = nil
end

Instance Attribute Details

#codeObject

Used for CLI testing



21
22
23
# File 'lib/tincan/cli.rb', line 21

def code
  @code
end

#configObject

Returns the value of attribute config.



25
26
27
# File 'lib/tincan/cli.rb', line 25

def config
  @config
end

#environmentObject

Returns the value of attribute environment.



23
24
25
# File 'lib/tincan/cli.rb', line 23

def environment
  @environment
end

#loggerObject

Returns the value of attribute logger.



24
25
26
# File 'lib/tincan/cli.rb', line 24

def logger
  @logger
end

#receiverObject

Returns the value of attribute receiver.



22
23
24
# File 'lib/tincan/cli.rb', line 22

def receiver
  @receiver
end

#threadObject

Returns the value of attribute thread.



26
27
28
# File 'lib/tincan/cli.rb', line 26

def thread
  @thread
end

Instance Method Details

#parse(args = ARGV) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/tincan/cli.rb', line 32

def parse(args = ARGV)
  @code = nil

  setup_config(args)
  initialize_logger
  check_required_keys!
  validate!
  daemonize
  write_pid
end

#runObject



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tincan/cli.rb', line 43

def run
  boot_system
  print_banner

  self_read, self_write = IO.pipe

  %w(INT TERM USR1 USR2 TTIN).each do |sig|
    begin
      trap sig do
        self_write.puts(sig)
      end
    rescue ArgumentError
      puts "Signal #{sig} not supported."
    end
  end

  logger.info "Running in #{RUBY_DESCRIPTION}."

  unless config[:daemon]
    logger.info 'Now listening for notifications. Hit Ctrl-C to stop.'
  end

  ## TODO: FIX THIS

  @receiver = Tincan::Receiver.new do |r|
    r.logger = @logger
    r.redis_host = config[:redis_host]
    r.client_name = config[:client_name]
    r.namespace = config[:namespace]
    r.listen_to = Hash[config[:listen_to].map do |object, runners|
      [object.to_sym, runners.map do |runner|
        klass, method_name = runner.split('.')
        klass = klass.constantize
        ->(data) { klass.send(method_name.to_sym, data) }
      end]
    end]
    r.on_exception = lambda do |exception, context|
      @logger.error exception
      @logger.error exception.backtrace
      # this was changed so that there isn't
      # a hard dependency on bugsnag (formally was using airbrake)
      # should probably abstract this out into an error handler.
      if Object.const_defined?('Bugsnag')
        Bugsnag.notify(exception, { severity: :error, parameters: context })
      end
    end
  end

  begin
    @thread = Thread.new { @receiver.listen }

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