Class: Sidekiq::CLI
- Inherits:
-
Object
show all
- Includes:
- Component, Singleton
- Defined in:
- lib/sidekiq/cli.rb
Constant Summary
collapse
- SIGNAL_HANDLERS =
{
"INT" => ->(cli) { raise Interrupt },
"TERM" => ->(cli) { raise Interrupt },
"TSTP" => ->(cli) {
cli.logger.info "Received TSTP, no longer accepting new work"
cli.launcher.quiet
},
"TTIN" => ->(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
}
}
- UNHANDLED_SIGNAL_HANDLER =
->(cli) { cli.logger.info "No signal handler registered, ignoring" }
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Component
#fire_event, #handle_exception, #hostname, #identity, #logger, #process_nonce, #redis, #safe_thread, #tid, #watchdog
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
22
23
24
|
# File 'lib/sidekiq/cli.rb', line 22
def config
@config
end
|
#environment ⇒ Object
Returns the value of attribute environment.
21
22
23
|
# File 'lib/sidekiq/cli.rb', line 21
def environment
@environment
end
|
#launcher ⇒ Object
Returns the value of attribute launcher.
20
21
22
|
# File 'lib/sidekiq/cli.rb', line 20
def launcher
@launcher
end
|
Class Method Details
.b ⇒ Object
149
150
151
|
# File 'lib/sidekiq/cli.rb', line 149
def self.b
"\e[30m"
end
|
.banner ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/sidekiq/cli.rb', line 157
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
|
.r ⇒ Object
145
146
147
|
# File 'lib/sidekiq/cli.rb', line 145
def self.r
"\e[31m"
end
|
.reset ⇒ Object
153
154
155
|
# File 'lib/sidekiq/cli.rb', line 153
def self.reset
"\e[0m"
end
|
.w ⇒ Object
141
142
143
|
# File 'lib/sidekiq/cli.rb', line 141
def self.w
"\e[37m"
end
|
Instance Method Details
#handle_signal(sig) ⇒ Object
198
199
200
201
|
# File 'lib/sidekiq/cli.rb', line 198
def handle_signal(sig)
logger.debug "Got #{sig} signal"
SIGNAL_HANDLERS[sig].call(self)
end
|
#jruby? ⇒ Boolean
34
35
36
|
# File 'lib/sidekiq/cli.rb', line 34
def jruby?
defined?(::JRUBY_VERSION)
end
|
#launch(self_read) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/sidekiq/cli.rb', line 113
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!"
exit(0)
end
end
|
#parse(args = ARGV.dup) ⇒ Object
24
25
26
27
28
29
30
31
32
|
# File 'lib/sidekiq/cli.rb', line 24
def parse(args = ARGV.dup)
@config = Sidekiq
@config[:error_handlers].clear
@config[:error_handlers] << @config.method(:default_error_handler)
setup_options(args)
initialize_logger
validate!
end
|
#run(boot_app: 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.
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
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
|
# File 'lib/sidekiq/cli.rb', line 41
def run(boot_app: true)
boot_application if boot_app
if environment == "development" && $stdout.tty? && @config.log_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 TTIN TSTP]
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
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)
info = @config.redis_info
ver = info["redis_version"]
raise "You are connecting to Redis v#{ver}, Sidekiq requires Redis v4.0.0 or greater" if ver < "4"
maxmemory_policy = info["maxmemory_policy"]
if maxmemory_policy != "noeviction"
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/mperham/sidekiq/wiki/Using-Redis#memory
EOM
end
cursize = @config.redis_pool.size
needed = @config[:concurrency] + 2
raise "Your pool of #{cursize} Redis connections is too small, please increase the size to at least #{needed}" if cursize < needed
@config[:identity] = identity
@config.server_middleware
fire_event(:startup, reverse: false, reraise: true)
logger.debug { "Client Middleware: #{@config.client_middleware.map(&:klass).join(", ")}" }
logger.debug { "Server Middleware: #{@config.server_middleware.map(&:klass).join(", ")}" }
launch(self_read)
end
|