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

105 lines
2.3 KiB
Ruby
Raw Normal View History

2012-01-16 16:14:47 -08:00
require 'optparse'
2012-01-16 16:18:36 -08:00
require 'sidekiq'
2012-01-16 16:14:47 -08:00
module Sidekiq
class CLI
def initialize
parse_options
2012-01-16 20:02:58 -08:00
validate!
enable_rails3 if File.exist?("config/application.rb")
2012-01-16 16:14:47 -08:00
end
def run
write_pid
server = Sidekiq::Server.new(@options[:server], @options)
begin
log 'Starting processing, hit Ctrl-C to stop'
2012-01-16 16:18:36 -08:00
server.run
2012-01-16 16:14:47 -08:00
rescue Interrupt
log 'Shutting down...'
server.stop
log '...bye!'
end
end
2012-01-16 16:18:36 -08:00
private
2012-01-16 20:02:58 -08:00
def enable_rails3
2012-01-21 16:42:21 -08:00
#APP_PATH = File.expand_path('config/application.rb')
2012-01-16 20:02:58 -08:00
require File.expand_path('config/boot.rb')
end
2012-01-16 16:14:47 -08:00
def log(str)
STDOUT.puts str
end
def error(str)
@STDERR.puts "ERROR: #{str}"
end
2012-01-16 20:02:58 -08:00
def validate!
if @options[:queues].size == 0
log "========== Please configure at least one queue to process =========="
log @parser
end
end
2012-01-16 16:14:47 -08:00
def parse_options(argv=ARGV)
@options = {
2012-01-21 16:42:21 -08:00
:daemon => false,
2012-01-16 20:02:58 -08:00
:verbose => false,
2012-01-16 16:14:47 -08:00
:queues => [],
2012-01-16 20:02:58 -08:00
:worker_count => 25,
2012-01-21 16:42:21 -08:00
:server => 'localhost:6379',
:pidfile => nil,
2012-01-16 16:14:47 -08:00
}
@parser = OptionParser.new do |o|
2012-01-16 20:02:58 -08: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 16:14:47 -08:00
end
2012-01-21 16:42:21 -08:00
o.on "-d", "Daemonize" do |arg|
@options[:daemon] = arg
end
2012-01-16 16:14:47 -08:00
o.on "--pidfile PATH", "Use PATH as a pidfile" do |arg|
@options[:pidfile] = arg
end
2012-01-16 20:02:58 -08:00
o.on "-v", "--verbose", "Print more verbose output" do
@options[:verbose] = true
2012-01-16 16:14:47 -08:00
end
o.on "-s", "--server LOCATION", "Where to find the server" do |arg|
@options[:server] = arg
end
2012-01-21 16:42:21 -08:00
o.on '-c', '--concurrency INT', "Worker threads to use" do |arg|
2012-01-16 20:02:58 -08:00
@options[:worker_count] = arg.to_i
2012-01-16 16:14:47 -08:00
end
end
2012-01-21 16:42:21 -08:00
@parser.banner = "sidekiq -q foo,1 -q bar,2 <more options>"
2012-01-16 16:14:47 -08: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