Class: Faktory::CLI

Inherits:
Object
  • Object
show all
Includes:
Util, Singleton
Defined in:
lib/faktory/cli.rb,
lib/faktory/cli.rb

Constant Summary

Constants included from Util

Util::EXPIRY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#fire_event, #hostname, #identity, #logger, #process_nonce, #safe_thread, #server, #watchdog

Methods included from ExceptionHandler

#handle_exception

Constructor Details

#initializeCLI

Returns a new instance of CLI.



29
30
31
# File 'lib/faktory/cli.rb', line 29

def initialize
  @code = nil
end

Instance Attribute Details

#codeObject

Used for CLI testing



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

def code
  @code
end

#environmentObject

Returns the value of attribute environment.



27
28
29
# File 'lib/faktory/cli.rb', line 27

def environment
  @environment
end

#launcherObject

Returns the value of attribute launcher.



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

def launcher
  @launcher
end

Class Method Details



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/faktory/cli.rb', line 105

def self.banner
%q{
                ,,,,
        ,,,,    |  |
        |  |    |  |
        |  |    |  |
        |  |,,~~'  '~,          ___       _
   ,,~~''   ,~~,      '~,      / __)     | |      _
 ,,~~''   ,~~,  |  |        |    _| |__ _____| |  _ _| |_ ___   ____ _   _
 |  ,~~,  |  |  |  |        |   (_   __|____ | |_/ |_   _) _ \ / ___) | | |
 |  |  |  |  |  |  |        |     | |  / ___ |  _ (  | || |_| | |   | |_| |
 |  |__|  |__|  |__|        |     |_|  \_____|_| \_)  \__)___/|_|    \__  |
 |__________________________|                                       (____/

}
end

Instance Method Details

#handle_signal(sig) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/faktory/cli.rb', line 122

def handle_signal(sig)
  Faktory.logger.debug "Got #{sig} signal"
  case sig
  when 'INT'
    raise Interrupt
  when 'TERM'
    # Heroku sends TERM and then waits 30 seconds for process to exit.
    raise Interrupt
  when 'TSTP'
    Faktory.logger.info "Received TSTP, no longer accepting new work"
    launcher.quiet
  when 'TTIN'
    Thread.list.each do |thread|
      Faktory.logger.warn "Thread TID-#{thread.object_id.to_s(36)} #{thread['faktory_label']}"
      if thread.backtrace
        Faktory.logger.warn thread.backtrace.join("\n")
      else
        Faktory.logger.warn "<no backtrace available>"
      end
    end
  end
end

#jruby?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/faktory/cli.rb', line 41

def jruby?
  defined?(::JRUBY_VERSION)
end

#parse(args = ARGV) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/faktory/cli.rb', line 33

def parse(args=ARGV)
  @code = nil

  setup_options(args)
  initialize_logger
  validate!
end

#runObject

Code within this method is not tested because it alters global process state irreversibly. PRs which improve the test coverage of Faktory::CLI are welcomed.



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/faktory/cli.rb', line 48

def run
  Faktory::Client.worker!

  boot_system
  print_banner

  self_read, self_write = IO.pipe
  sigs = %w(INT TERM TTIN TSTP)

  sigs.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}"
  logger.info Faktory::LICENSE

  # cache process identity
  Faktory.options[:identity] = identity

  # Touch middleware so it isn't lazy loaded by multiple threads, #3043
  Faktory.worker_middleware

  # Before this point, the process is initializing with just the main thread.
  # Starting here the process will now have multiple threads running.
  fire_event(:startup)

  logger.debug { "Client Middleware: #{Faktory.client_middleware.map(&:klass).join(', ')}" }
  logger.debug { "Worker Middleware: #{Faktory.worker_middleware.map(&:klass).join(', ')}" }

  logger.info 'Starting processing, hit Ctrl-C to stop' if $stdout.tty?

  require 'faktory/launcher'
  @launcher = Faktory::Launcher.new(options)

  begin
    launcher.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'
    launcher.stop
    # Explicitly exit so busy Processor threads can't block
    # process shutdown.
    logger.info "Bye!"
    exit(0)
  end
end