Class: Sidekiq::CLI

Inherits:
Object
  • Object
show all
Includes:
Component
Defined in:
lib/sidekiq/cli.rb

Constant Summary collapse

HOLIDAY_COLORS =
{
  # got other color-specific holidays from around the world?
  # https://developer-book.com/post/definitive-guide-for-colored-text-in-terminal/#256-color-escape-codes
  "3-17" => "\e[1;32m", # St. Patrick's Day green
  "10-31" => "\e[38;5;208m" # Halloween orange
}
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) {
    cli.logger.info "Received TSTP, no longer accepting new work"
    cli.launcher.quiet
  },
  # deprecated, use INFO
  "TTIN" => ->(cli) {
    cli.logger.error { "DEPRECATED: Please use the INFO signal for backtraces, support for TTIN will be removed in Sidekiq 9.0." }
    Thread.list.each do |thread|
      cli.logger.warn "Thread TID-#{(thread.object_id ^ ::Process.pid).to_s(36)} #{thread.name}"
      if thread.backtrace
        cli.logger.warn thread.backtrace.join("\n")
      else
        cli.logger.warn "<no backtrace available>"
      end
    end
  },
  "INFO" => ->(cli) {
    Thread.list.each do |thread|
      cli.logger.warn "Thread TID-#{(thread.object_id ^ ::Process.pid).to_s(36)} #{thread.name}"
      if thread.backtrace
        cli.logger.warn thread.backtrace.join("\n")
      else
        cli.logger.warn "<no backtrace available>"
      end
    end
  }
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Component

#default_tag, #fire_event, #handle_exception, #hostname, #identity, #inspect, #logger, #mono_ms, #process_nonce, #real_ms, #redis, #safe_thread, #tid, #watchdog

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#environmentObject

Returns the value of attribute environment.



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

def environment
  @environment
end

#launcherObject

Returns the value of attribute launcher.



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

def launcher
  @launcher
end

Class Method Details

.bObject



163
164
165
# File 'lib/sidekiq/cli.rb', line 163

def self.b
  @@b ||= HOLIDAY_COLORS[day] || "\e[30m"
end


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/sidekiq/cli.rb', line 176

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}   $:    #{b}'#{w},$$:      #{r} / ___|(_) __| | ___| | _(_) __ _
  #{w}   `b     :$$       #{r} \\___ \\| |/ _` |/ _ \\ |/ / |/ _` |
  #{w}          $$:        #{r} ___) | | (_| |  __/   <| | (_| |
  #{w}          $$         #{r}|____/|_|\\__,_|\\___|_|\\_\\_|\\__, |
  #{w}        .d$$          #{r}                             |_|
  #{reset}}
end

.dayObject



152
153
154
155
156
157
# File 'lib/sidekiq/cli.rb', line 152

def self.day
  @@day ||= begin
    t = Date.today
    "#{t.month}-#{t.day}"
  end
end

.instanceObject



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

def self.instance
  @instance ||= new
end

.rObject



159
160
161
# File 'lib/sidekiq/cli.rb', line 159

def self.r
  @@r ||= HOLIDAY_COLORS[day] || "\e[1;31m"
end

.resetObject



171
172
173
174
# File 'lib/sidekiq/cli.rb', line 171

def self.reset
  @@b = @@r = @@day = nil
  "\e[0m"
end

.wObject



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

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

Instance Method Details

#handle_signal(sig) ⇒ Object



227
228
229
230
231
232
# File 'lib/sidekiq/cli.rb', line 227

def handle_signal(sig)
  logger.debug "Got #{sig} signal"
  hndlr = SIGNAL_HANDLERS[sig]
  hndlr ? hndlr.call(self) :
    logger.warn("No #{sig} signal handler registered, ignoring")
end

#jruby?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/sidekiq/cli.rb', line 35

def jruby?
  defined?(::JRUBY_VERSION)
end

#launch(self_read) ⇒ Object



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

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

  @launcher = Sidekiq::Launcher.new(@config)

  begin
    launcher.run

    while self_read.wait_readable
      signal = self_read.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.dup) ⇒ Object



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

def parse(args = ARGV.dup)
  @config ||= Sidekiq.default_configuration

  setup_options(args)
  initialize_logger
  validate!
end

#run(boot_app: true, warmup: true) ⇒ Object

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.



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

def run(boot_app: true, warmup: true)
  boot_application if boot_app

  if environment == "development" && $stdout.tty? && @config.logger.formatter.is_a?(Sidekiq::Logger::Formatters::Pretty)
    print_banner
  end
  logger.info "Booted Rails #{::Rails.version} application in #{environment} environment" if rails_app?

  self_read, self_write = IO.pipe
  sigs = %w[INT TERM INFO TTIN TSTP]
  # USR1 and USR2 don't work on the JVM
  sigs << "USR2" if Sidekiq.pro? && !jruby?
  sigs.each do |sig|
    old_handler = Signal.trap(sig) do
      if old_handler.respond_to?(:call)
        begin
          old_handler.call
        rescue Exception => exc
          # signal handlers can't use Logger so puts only
          puts ["Error in #{sig} handler", exc].inspect
        end
      end
      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: https://sidekiq.org" unless defined?(::Sidekiq::Pro)

  # touch the connection pool so it is created before we
  # fire startup and start multithreading.
  info = @config.redis_info
  ver = Gem::Version.new(info["redis_version"])
  raise "You are connected to Redis #{ver}, Sidekiq requires Redis 7.0.0 or greater" if ver < Gem::Version.new("7.0.0")

  maxmemory_policy = info["maxmemory_policy"]
  if maxmemory_policy != "noeviction" && maxmemory_policy != ""
    # Redis Enterprise Cloud returns "" for their policy 😳
    logger.warn <<~EOM


      WARNING: Your Redis instance will evict Sidekiq data under heavy load.
      The 'noeviction' maxmemory policy is recommended (current policy: '#{maxmemory_policy}').
      See: https://github.com/sidekiq/sidekiq/wiki/Using-Redis#memory

    EOM
  end

  # 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.
  @config.capsules.each_pair do |name, cap|
    raise ArgumentError, "Pool size too small for #{name}" if cap.redis_pool.size < cap.concurrency
  end

  # cache process identity
  @config[:identity] = identity

  # Touch middleware so it isn't lazy loaded by multiple threads, #3043
  @config.server_middleware

  ::Process.warmup if warmup && ::Process.respond_to?(:warmup) && ENV["RUBY_DISABLE_WARMUP"] != "1"

  # 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: #{@config.default_capsule.client_middleware.map(&:klass).join(", ")}" }
  logger.debug { "Server Middleware: #{@config.default_capsule.server_middleware.map(&:klass).join(", ")}" }

  launch(self_read)
end