lib/rabbit_wq/cli.rb in rabbit-wq-1.9.0 vs lib/rabbit_wq/cli.rb in rabbit-wq-2.0.0
- old
+ new
@@ -1,130 +1,15 @@
-require 'rubygems'
-require 'rabbit_wq'
-require 'trollop'
-require 'yell'
+require 'servitude'
module RabbitWQ
- class Cli
+ class Cli < ::Servitude::Cli::Service
- attr_reader :cmd,
- :options
+ no_commands do
- SUB_COMMANDS = %w(
- restart
- start
- status
- stop
- )
-
- DEFAULT_CONFIG_PATH = "/etc/#{APP_ID}/#{APP_ID}.conf"
- DEFAULT_LOG_PATH = "/var/log/#{APP_ID}/#{APP_ID}.log"
- DEFAULT_PID_PATH = "/var/run/#{APP_ID}/#{APP_ID}.pid"
-
- def initialize( args )
- Trollop::options do
- version VERSION_COPYRIGHT
- banner <<-EOS
-#{APP_NAME} #{VERSION_COPYRIGHT}
-
-Usage:
- #{APP_ID} [command] [options]
-
- commands:
-#{SUB_COMMANDS.map { |sub_cmd| " #{sub_cmd}" }.join( "\n" )}
-
- (For help with a command: #{APP_ID} [command] -h)
-
-options:
- EOS
- stop_on SUB_COMMANDS
+ def configuration_class
+ RabbitWQ::Configuration
end
- # Get the sub-command and its options
- #
- @cmd = ARGV.shift || ''
- @options = case( cmd )
- when "restart"
- Trollop::options do
- opt :config, "The path for the config file", :type => String, :short => '-c', :default => DEFAULT_CONFIG_PATH
- opt :log_level, "The log level", :type => String, :default => 'info'
- opt :log, "The path for the log file", :type => String, :short => '-l', :default => DEFAULT_LOG_PATH
- opt :pid, "The path for the PID file", :type => String, :default => DEFAULT_PID_PATH
- opt :threads, "The number of threads", :type => Integer, :short => '-t'
- end
- when "start"
- Trollop::options do
- opt :config, "The path for the config file", :type => String, :short => '-c', :default => DEFAULT_CONFIG_PATH
- opt :interactive, "Execute the server in interactive mode", :short => '-i'
- opt :log_level, "The log level", :type => String, :default => 'info'
- opt :log, "The path for the log file", :type => String, :short => '-l', :default => DEFAULT_LOG_PATH
- opt :pid, "The path for the PID file", :type => String, :default => DEFAULT_PID_PATH
- opt :threads, "The number of threads", :type => Integer, :short => '-t'
- end
- when "status"
- Trollop::options do
- opt :pid, "The path for the PID file", :type => String, :default => DEFAULT_PID_PATH
- opt :quiet, "Do not prompt to remove an old PID file", :type => :boolean, :default => false, :short => '-q'
- end
- when "stop"
- Trollop::options do
- opt :pid, "The path for the PID file", :type => String, :default => DEFAULT_PID_PATH
- opt :quiet, "Do not prompt to remove an old PID file", :type => :boolean, :default => false, :short => '-q'
- end
- else
- Trollop::die "unknown command #{cmd.inspect}"
- end
-
- if cmd == 'start'
- unless options[:interactive]
- Trollop::die( :config, "is required when running as daemon" ) unless options[:config]
- Trollop::die( :log, "is required when running as daemon" ) unless options[:log]
- Trollop::die( :pid, "is required when running as daemon" ) unless options[:pid]
- end
- end
-
- if %w(restart status stop).include?( cmd )
- Trollop::die( :pid, "is required" ) unless options[:pid]
- end
- end
-
- def run
- send( cmd )
- end
-
- protected
-
- def start
- if options[:interactive]
- start_interactive
- else
- start_daemon
- end
- end
-
- def start_interactive
- server = RabbitWQ::Server.new( options.merge( log: nil ))
- server.start
- end
-
- def start_daemon
- server = RabbitWQ::ServerDaemon.new( options )
- server.start
- end
-
- def stop
- server = RabbitWQ::ServerDaemon.new( options )
- server.stop
- end
-
- def restart
- stop
- start_daemon
- end
-
- def status
- result = RabbitWQ::ServerDaemon.new( options ).status
- at_exit { exit result }
end
end
end