Class: Faktory::CLI

Inherits:
Object
  • Object
show all
Includes:
Util, Singleton
Defined in:
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.



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

def initialize
  @code = nil
end

Instance Attribute Details

#codeObject

Used for CLI testing



20
21
22
# File 'lib/faktory/cli.rb', line 20

def code
  @code
end

#environmentObject

Returns the value of attribute environment.



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

def environment
  @environment
end

#launcherObject

Returns the value of attribute launcher.



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

def launcher
  @launcher
end

Class Method Details



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/faktory/cli.rb', line 100

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

}
end

Instance Method Details

#handle_signal(sig) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/faktory/cli.rb', line 117

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:



36
37
38
# File 'lib/faktory/cli.rb', line 36

def jruby?
  defined?(::JRUBY_VERSION)
end

#parse(args = ARGV) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/faktory/cli.rb', line 28

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.



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

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