forked from sidekiq/sidekiq
/
cli.rb
183 lines (149 loc) · 4.59 KB
/
cli.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
trap 'INT' do
# Handle Ctrl-C in JRuby like MRI
# http://jira.codehaus.org/browse/JRUBY-4637
Thread.main.raise Interrupt
end
trap 'TERM' do
# Heroku sends TERM and then waits 10 seconds for process to exit.
Thread.main.raise Interrupt
end
trap 'USR1' do
Sidekiq::Util.logger.info "Received USR1, no longer accepting new work"
mgr = Sidekiq::CLI.instance.manager
mgr.stop! if mgr
end
require 'yaml'
require 'singleton'
require 'optparse'
require 'sidekiq'
require 'sidekiq/util'
require 'sidekiq/manager'
module Sidekiq
class CLI
include Util
include Singleton
# Used for CLI testing
attr_accessor :code
def initialize
@code = nil
end
def parse(args=ARGV)
@code = nil
Sidekiq::Util.logger
cli = parse_options(args)
config = parse_config(cli)
options.merge!(config.merge(cli))
Sidekiq::Util.logger.level = Logger::DEBUG if options[:verbose]
Celluloid.logger = nil
validate!
write_pid
boot_system
end
def run
manager = Sidekiq::Manager.new(options)
poller = Sidekiq::Retry::Poller.new
begin
logger.info 'Starting processing, hit Ctrl-C to stop'
manager.start!
poller.poll!
sleep
rescue Interrupt
logger.info 'Shutting down'
poller.terminate
manager.stop!(:shutdown => true, :timeout => options[:timeout])
manager.wait(:shutdown)
end
end
private
def die(code)
exit(code)
end
def options
Sidekiq.options
end
def detected_environment
options[:environment] ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
end
def boot_system
ENV['RACK_ENV'] = ENV['RAILS_ENV'] = detected_environment
raise ArgumentError, "#{options[:require]} does not exist" unless File.exist?(options[:require])
if File.directory?(options[:require])
require File.expand_path("#{options[:require]}/config/environment.rb")
::Rails.application.eager_load!
else
require options[:require]
end
end
def validate!
options[:queues] << 'default' if options[:queues].empty?
options[:queues].shuffle!
if !File.exist?(options[:require]) ||
(File.directory?(options[:require]) && !File.exist?("#{options[:require]}/config/application.rb"))
logger.info "=================================================================="
logger.info " Please point sidekiq to a Rails 3 application or a Ruby file "
logger.info " to load your worker classes with -r [DIR|FILE]."
logger.info "=================================================================="
logger.info @parser
die(1)
end
end
def parse_options(argv)
opts = {}
@parser = OptionParser.new do |o|
o.on "-q", "--queue QUEUE,WEIGHT", "Queue to process, with optional weight" do |arg|
(q, weight) = arg.split(",")
parse_queues(opts, q, weight)
end
o.on "-v", "--verbose", "Print more verbose output" do
Sidekiq::Util.logger.level = Logger::DEBUG
end
o.on '-e', '--environment ENV', "Application environment" do |arg|
opts[:environment] = arg
end
o.on '-t', '--timeout NUM', "Shutdown timeout" do |arg|
opts[:timeout] = arg.to_i
end
o.on '-r', '--require [PATH|DIR]', "Location of Rails application with workers or file to require" do |arg|
opts[:require] = arg
end
o.on '-c', '--concurrency INT', "processor threads to use" do |arg|
opts[:concurrency] = arg.to_i
end
o.on '-P', '--pidfile PATH', "path to pidfile" do |arg|
opts[:pidfile] = arg
end
o.on '-C', '--config PATH', "path to YAML config file" do |arg|
opts[:config_file] = arg
end
end
@parser.banner = "sidekiq [options]"
@parser.on_tail "-h", "--help", "Show help" do
logger.info @parser
die 1
end
@parser.parse!(argv)
opts
end
def write_pid
if path = options[:pidfile]
File.open(path, 'w') do |f|
f.puts Process.pid
end
end
end
def parse_config(cli)
opts = {}
if cli[:config_file] && File.exist?(cli[:config_file])
opts = YAML.load_file cli[:config_file]
queues = opts.delete(:queues) || []
queues.each { |pair| parse_queues(opts, *pair) }
end
opts
end
def parse_queues(opts, q, weight)
(weight || 1).to_i.times do
(opts[:queues] ||= []) << q
end
end
end
end