Class: Msgr::CLI

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CLI

Returns a new instance of CLI.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/msgr/cli.rb', line 9

def initialize(options)
  @options = options

  if !File.exist?(options[:require]) ||
     (File.directory?(options[:require]) &&
     !File.exist?("#{options[:require]}/config/application.rb"))
    raise <<~ERR
      Rails application or required ruby file not found: #{options[:require]}
    ERR
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/msgr/cli.rb', line 7

def options
  @options
end

Class Method Details

.run(argv) ⇒ Object



58
59
60
# File 'lib/msgr/cli.rb', line 58

def run(argv)
  new(parse(argv)).run
end

Instance Method Details

#runObject



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

def run
  ENV['RACK_ENV'] = ENV['RAILS_ENV'] = options[:environment]

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

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

  r, w = IO.pipe

  Signal.trap('INT') { w.puts 'INT' }
  Signal.trap('TERM') { w.puts 'TERM' }

  Msgr.logger = Logger.new($stdout)
  Msgr.client.start

  # Wait until we receive a signal
  r.wait_readable
  case r.gets.strip
    when 'INT', 'TERM' # Safe shutdown
      Msgr.client.stop
    else # Error
      exit 1
  end
end