Class: Sidekiq::CLI

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

Constant Summary collapse

PROCTITLES =
[
  proc { 'sidekiq'.freeze },
  proc { Sidekiq::VERSION },
  proc { |me, data| data['tag'] },
  proc { |me, data| "[#{Processor::WORKER_STATE.size} of #{data['concurrency']} busy]" },
  proc { |me, data| "stopping" if me.stopping? },
]
SIGNAL_HANDLERS =
{
  # Ctrl-C in terminal
  'INT' => ->(cli) { raise Interrupt },
  # TERM is the signal that Sidekiq must exit.
  # Heroku sends TERM and then waits 30 seconds for process to exit.
  'TERM' => ->(cli) { raise Interrupt },
  'USR1' => ->(cli) {
    Sidekiq.logger.info "Received USR1, no longer accepting new work"
    cli.launcher.quiet
  },
  'TSTP' => ->(cli) {
    Sidekiq.logger.info "Received TSTP, no longer accepting new work"
    cli.launcher.quiet
  },
  'USR2' => ->(cli) {
    if Sidekiq.options[:logfile]
      Sidekiq.logger.info "Received USR2, reopening log file"
      Sidekiq::Logging.reopen_logs
    end
  },
  'TTIN' => ->(cli) {
    Thread.list.each do |thread|
      Sidekiq.logger.warn "Thread TID-#{(thread.object_id ^ ::Process.pid).to_s(36)} #{thread['sidekiq_label']}"
      if thread.backtrace
        Sidekiq.logger.warn thread.backtrace.join("\n")
      else
        Sidekiq.logger.warn "<no backtrace available>"
      end
    end
  },
}

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, #redis, #safe_thread, #watchdog

Methods included from ExceptionHandler

#handle_exception

Constructor Details

#initializeCLI

Returns a new instance of CLI.



32
33
34
# File 'lib/sidekiq/cli.rb', line 32

def initialize
  @code = nil
end

Instance Attribute Details

#codeObject

Used for CLI testing



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

def code
  @code
end

#environmentObject

Returns the value of attribute environment.



30
31
32
# File 'lib/sidekiq/cli.rb', line 30

def environment
  @environment
end

#launcherObject

Returns the value of attribute launcher.



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

def launcher
  @launcher
end

Class Method Details



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/sidekiq/cli.rb', line 121

def self.banner
%q{
     m,
     `$b
.ss,  $$:         .,d$
`$$P,d$P'    .,md$P"'
 ,$$$$$bmmd$$$P^'
   .d$$$$$$$$$$P'
   $$^' `"^$$$'       ____  _     _      _    _
   $:     ,$$:       / ___|(_) __| | ___| | _(_) __ _
   `b     :$$        \___ \| |/ _` |/ _ \ |/ / |/ _` |
      $$:         ___) | | (_| |  __/   <| | (_| |
      $$         |____/|_|\__,_|\___|_|\_\_|\__, |
    .d$$                                       |_|
}
end

Instance Method Details

#handle_signal(sig) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/sidekiq/cli.rb', line 170

def handle_signal(sig)
  Sidekiq.logger.debug "Got #{sig} signal"
  handy = SIGNAL_HANDLERS[sig]
  if handy
    handy.call(self)
  else
    Sidekiq.logger.info { "No signal handler for #{sig}" }
  end
end

#jruby?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/sidekiq/cli.rb', line 46

def jruby?
  defined?(::JRUBY_VERSION)
end

#parse(args = ARGV) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/sidekiq/cli.rb', line 36

def parse(args=ARGV)
  @code = nil

  setup_options(args)
  initialize_logger
  validate!
  daemonize
  write_pid
end

#runObject

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



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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sidekiq/cli.rb', line 53

def run
  boot_system
  print_banner

  self_read, self_write = IO.pipe
  sigs = %w(INT TERM TTIN TSTP)
  # USR1 and USR2 don't work on the JVM
  if !jruby?
    sigs << 'USR1'
    sigs << 'USR2'
  end

  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 Sidekiq::LICENSE
  logger.info "Upgrade to Sidekiq Pro for more features and support: http://sidekiq.org" unless defined?(::Sidekiq::Pro)

  # touch the connection pool so it is created before we
  # fire startup and start multithreading.
  ver = Sidekiq.redis_info['redis_version']
  raise "You are using Redis v#{ver}, Sidekiq requires Redis v2.8.0 or greater" if ver < '2.8'

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

  # Touch middleware so it isn't lazy loaded by multiple threads, #3043
  Sidekiq.server_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, reverse: false, reraise: true)

  logger.debug { "Client Middleware: #{Sidekiq.client_middleware.map(&:klass).join(', ')}" }
  logger.debug { "Server Middleware: #{Sidekiq.server_middleware.map(&:klass).join(', ')}" }

  if !options[:daemon]
    logger.info 'Starting processing, hit Ctrl-C to stop'
  end

  require 'sidekiq/launcher'
  @launcher = Sidekiq::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