Class: Sidekiq::CLI

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

Constant Summary collapse

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 },
  "TSTP" => ->(cli) {
    Sidekiq.logger.info "Received TSTP, no longer accepting new work"
    cli.launcher.quiet
  },
  "TTIN" => ->(cli) {
    Thread.list.each do |thread|
      Sidekiq.logger.warn "Thread TID-#{(thread.object_id ^ ::Process.pid).to_s(36)} #{thread.name}"
      if thread.backtrace
        Sidekiq.logger.warn thread.backtrace.join("\n")
      else
        Sidekiq.logger.warn "<no backtrace available>"
      end
    end
  },
}
UNHANDLED_SIGNAL_HANDLER =
->(cli) { Sidekiq.logger.info "No signal handler registered, ignoring" }

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, #tid, #watchdog

Methods included from ExceptionHandler

#handle_exception

Instance Attribute Details

#environmentObject

Returns the value of attribute environment


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

def environment
  @environment
end

#launcherObject

Returns the value of attribute launcher


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

def launcher
  @launcher
end

Class Method Details

.bObject


119
120
121
# File 'lib/sidekiq/cli.rb', line 119

def self.b
  "\e[30m"
end

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/sidekiq/cli.rb', line 127

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

.rObject


115
116
117
# File 'lib/sidekiq/cli.rb', line 115

def self.r
  "\e[31m"
end

.resetObject


123
124
125
# File 'lib/sidekiq/cli.rb', line 123

def self.reset
  "\e[0m"
end

.wObject


111
112
113
# File 'lib/sidekiq/cli.rb', line 111

def self.w
  "\e[37m"
end

Instance Method Details

#handle_signal(sig) ⇒ Object


168
169
170
171
# File 'lib/sidekiq/cli.rb', line 168

def handle_signal(sig)
  Sidekiq.logger.debug "Got #{sig} signal"
  SIGNAL_HANDLERS[sig].call(self)
end

#jruby?Boolean

Returns:

  • (Boolean)

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

def jruby?
  defined?(::JRUBY_VERSION)
end

#launch(self_read) ⇒ Object


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

def launch(self_read)
  if environment == "development" && $stdout.tty?
    logger.info "Starting processing, hit Ctrl-C to stop"
  end

  @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
    logger.info "Bye!"

    # Explicitly exit so busy Processor threads won't block process shutdown.
    #
    # NB: slow at_exit handlers will prevent a timely exit if they take
    # a while to run. If Sidekiq is getting here but the process isn't exiting,
    # use the TTIN signal to determine where things are stuck.
    exit(0)
  end
end

#parse(args = ARGV) ⇒ Object


23
24
25
26
27
# File 'lib/sidekiq/cli.rb', line 23

def parse(args = ARGV)
  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 Sidekiq::CLI are welcomed.


36
37
38
39
40
41
42
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
# File 'lib/sidekiq/cli.rb', line 36

def run
  boot_system
  if environment == "development" && $stdout.tty? && Sidekiq.log_formatter.is_a?(Sidekiq::Logger::Formatters::Pretty)
    print_banner
  end

  self_read, self_write = IO.pipe
  sigs = %w[INT TERM TTIN TSTP]
  sigs.each do |sig|
    trap sig do
      self_write.puts(sig)
    end
  rescue ArgumentError
    puts "Signal #{sig} not supported"
  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 v4.0.0 or greater" if ver < "4"

  # Since the user can pass us a connection pool explicitly in the initializer, we
  # need to verify the size is large enough or else Sidekiq's performance is dramatically slowed.
  cursize = Sidekiq.redis_pool.size
  needed = Sidekiq.options[:concurrency] + 2
  raise "Your pool of #{cursize} Redis connections is too small, please increase the size to at least #{needed}" if cursize < needed

  # 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(", ")}" }

  launch(self_read)
end