2012-01-16 19:14:47 -05:00
|
|
|
require 'optparse'
|
2012-01-16 19:18:36 -05:00
|
|
|
require 'sidekiq'
|
2012-01-22 19:01:46 -05:00
|
|
|
require 'sidekiq/server'
|
2012-01-24 01:08:38 -05:00
|
|
|
require 'connection_pool'
|
2012-01-16 19:14:47 -05:00
|
|
|
|
|
|
|
module Sidekiq
|
|
|
|
class CLI
|
2012-01-22 19:01:46 -05:00
|
|
|
include Util
|
|
|
|
|
2012-01-16 19:14:47 -05:00
|
|
|
def initialize
|
|
|
|
parse_options
|
2012-01-16 23:02:58 -05:00
|
|
|
validate!
|
2012-01-23 17:05:03 -05:00
|
|
|
boot_rails
|
2012-01-16 19:14:47 -05:00
|
|
|
end
|
|
|
|
|
2012-01-24 01:08:38 -05:00
|
|
|
FOREVER = 2_000_000_000
|
|
|
|
|
2012-01-16 19:14:47 -05:00
|
|
|
def run
|
2012-01-22 19:01:46 -05:00
|
|
|
write_pid if @options[:daemon]
|
2012-01-16 19:14:47 -05:00
|
|
|
|
2012-01-24 01:08:38 -05:00
|
|
|
::Sidekiq::Client.redis = ConnectionPool.new { Redis.connect(:url => @options[:server]) }
|
2012-01-16 19:14:47 -05:00
|
|
|
server = Sidekiq::Server.new(@options[:server], @options)
|
|
|
|
begin
|
|
|
|
log 'Starting processing, hit Ctrl-C to stop'
|
2012-01-22 19:01:46 -05:00
|
|
|
server.start!
|
2012-01-24 01:08:38 -05:00
|
|
|
sleep FOREVER
|
2012-01-16 19:14:47 -05:00
|
|
|
rescue Interrupt
|
|
|
|
log 'Shutting down...'
|
2012-01-22 19:01:46 -05:00
|
|
|
server.stop!
|
2012-01-23 17:05:03 -05:00
|
|
|
server.wait(:shutdown)
|
2012-01-16 19:14:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-16 19:18:36 -05:00
|
|
|
private
|
|
|
|
|
2012-01-23 17:05:03 -05:00
|
|
|
def boot_rails
|
2012-01-24 01:08:38 -05:00
|
|
|
ENV['RAILS_ENV'] = @options[:environment] || 'production'
|
2012-01-23 17:05:03 -05:00
|
|
|
require File.expand_path("#{@options[:rails]}/config/environment.rb")
|
2012-01-24 01:08:38 -05:00
|
|
|
Rails.application.eager_load!
|
2012-01-16 23:02:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate!
|
2012-01-22 19:01:46 -05:00
|
|
|
$DEBUG = @options[:verbose]
|
2012-01-16 23:02:58 -05:00
|
|
|
if @options[:queues].size == 0
|
|
|
|
log "========== Please configure at least one queue to process =========="
|
|
|
|
log @parser
|
2012-01-22 19:01:46 -05:00
|
|
|
exit(1)
|
2012-01-16 23:02:58 -05:00
|
|
|
end
|
2012-01-23 17:05:03 -05:00
|
|
|
|
|
|
|
if !File.exist?("#{@options[:rails]}/config/boot.rb")
|
|
|
|
log "========== Please point sidekiq to a Rails 3 application =========="
|
|
|
|
log @parser
|
|
|
|
exit(1)
|
|
|
|
end
|
2012-01-16 23:02:58 -05:00
|
|
|
end
|
|
|
|
|
2012-01-16 19:14:47 -05:00
|
|
|
def parse_options(argv=ARGV)
|
|
|
|
@options = {
|
2012-01-21 19:42:21 -05:00
|
|
|
:daemon => false,
|
2012-01-16 23:02:58 -05:00
|
|
|
:verbose => false,
|
2012-01-16 19:14:47 -05:00
|
|
|
:queues => [],
|
2012-01-16 23:02:58 -05:00
|
|
|
:worker_count => 25,
|
2012-01-22 19:01:46 -05:00
|
|
|
:server => 'redis://localhost:6379/0',
|
2012-01-21 19:42:21 -05:00
|
|
|
:pidfile => nil,
|
2012-01-23 17:05:03 -05:00
|
|
|
:rails => '.',
|
2012-01-24 01:08:38 -05:00
|
|
|
:environment => 'production',
|
2012-01-16 19:14:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@parser = OptionParser.new do |o|
|
2012-01-16 23:02:58 -05:00
|
|
|
o.on "-q", "--queue QUEUE,WEIGHT", "Queue to process, with optional weight" do |arg|
|
|
|
|
(q, weight) = arg.split(",")
|
|
|
|
(weight || 1).times do
|
|
|
|
@options[:queues] << q
|
|
|
|
end
|
2012-01-16 19:14:47 -05:00
|
|
|
end
|
|
|
|
|
2012-01-21 19:42:21 -05:00
|
|
|
o.on "-d", "Daemonize" do |arg|
|
|
|
|
@options[:daemon] = arg
|
|
|
|
end
|
|
|
|
|
2012-01-16 19:14:47 -05:00
|
|
|
o.on "--pidfile PATH", "Use PATH as a pidfile" do |arg|
|
|
|
|
@options[:pidfile] = arg
|
|
|
|
end
|
|
|
|
|
2012-01-16 23:02:58 -05:00
|
|
|
o.on "-v", "--verbose", "Print more verbose output" do
|
|
|
|
@options[:verbose] = true
|
2012-01-16 19:14:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
o.on "-s", "--server LOCATION", "Where to find the server" do |arg|
|
|
|
|
@options[:server] = arg
|
|
|
|
end
|
|
|
|
|
2012-01-24 01:08:38 -05:00
|
|
|
o.on '-e', '--environment ENV', "Rails application environment" do |arg|
|
|
|
|
@options[:environment] = arg
|
|
|
|
end
|
|
|
|
|
2012-01-23 17:05:03 -05:00
|
|
|
o.on '-r', '--rails PATH', "Rails application with workers" do |arg|
|
|
|
|
@options[:rails] = arg
|
|
|
|
end
|
|
|
|
|
2012-01-21 19:42:21 -05:00
|
|
|
o.on '-c', '--concurrency INT', "Worker threads to use" do |arg|
|
2012-01-16 23:02:58 -05:00
|
|
|
@options[:worker_count] = arg.to_i
|
2012-01-16 19:14:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-22 19:01:46 -05:00
|
|
|
@parser.banner = "sidekiq -q foo -q bar <more options>"
|
2012-01-16 19:14:47 -05:00
|
|
|
@parser.on_tail "-h", "--help", "Show help" do
|
|
|
|
log @parser
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
@parser.parse!(argv)
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_pid
|
|
|
|
if path = @options[:pidfile]
|
|
|
|
File.open(path, "w") do |f|
|
|
|
|
f.puts Process.pid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|