1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/cli.rb

107 lines
2.8 KiB
Ruby
Raw Normal View History

2012-01-16 19:14:47 -05:00
require 'optparse'
require 'sidekiq/version'
require 'sidekiq/util'
require 'sidekiq/client'
require 'sidekiq/manager'
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!
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-24 01:08:38 -05:00
::Sidekiq::Client.redis = ConnectionPool.new { Redis.connect(:url => @options[:server]) }
manager = Sidekiq::Manager.new(@options[:server], @options)
2012-01-16 19:14:47 -05:00
begin
log 'Starting processing, hit Ctrl-C to stop'
manager.start!
2012-02-05 16:22:57 -05:00
# HACK need to determine how to pause main thread while
# waiting for signals.
2012-01-24 01:08:38 -05:00
sleep FOREVER
2012-01-16 19:14:47 -05:00
rescue Interrupt
2012-02-05 16:22:57 -05:00
# TODO Need clean shutdown support from Celluloid
log 'Shutting down, pausing 5 seconds to let workers finish...'
manager.stop!
manager.wait(:shutdown)
2012-01-16 19:14:47 -05:00
end
end
2012-01-16 19:18:36 -05:00
private
def boot_rails
ENV['RAILS_ENV'] = @options[:environment] || ENV['RAILS_ENV'] || 'development'
require File.expand_path("#{@options[:rails]}/config/environment.rb")
::Rails.application.eager_load!
2012-01-16 23:02:58 -05:00
end
def validate!
@options[:queues] << 'default' if @options[:queues].empty?
@options[:queues].shuffle!
2012-01-22 19:01:46 -05:00
$DEBUG = @options[:verbose]
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-16 23:02:58 -05:00
:verbose => false,
:queues => [],
:processor_count => 25,
2012-02-05 15:59:24 -05:00
:server => ENV['REDISTOGO_URL'] || 'redis://localhost:6379/0',
:rails => '.',
:environment => nil,
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-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
2012-01-26 00:12:18 -05:00
o.on "-s", "--server LOCATION", "Where to find Redis" do |arg|
2012-01-16 19:14:47 -05:00
@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-26 00:12:18 -05:00
o.on '-r', '--rails PATH', "Location of Rails application with workers" do |arg|
@options[:rails] = arg
end
o.on '-c', '--concurrency INT', "processor threads to use" do |arg|
@options[:processor_count] = arg.to_i
2012-01-16 19:14:47 -05:00
end
end
@parser.banner = "sidekiq [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
end
end