lib/daemonizer/cli.rb in daemonizer-0.1.3 vs lib/daemonizer/cli.rb in daemonizer-0.1.4

- old
+ new

@@ -3,18 +3,20 @@ module Daemonizer class CLI < Thor check_unknown_options! + method_option :demfile, :type => :string, :aliases => "-D", :banner => "Path to Demfile" + def initialize(*) super + Daemonizer.demfile = options[:demfile] || "Demfile" end desc "start", "Start pool" - method_option :demfile, :type => :string, :aliases => "-D", :banner => "Path to Demfile" def start(pool_name = nil) - control_pools_loop(pool_name, "successfully started", options[:demfile]) do |pool| + control_pools_loop(pool_name, "successfully started") do |pool| # Pid file check if Daemonize.check_pid(pool.pid_file) print_pool pool.name, "Can't start, another process exists!" exit(1) end @@ -39,13 +41,12 @@ end return true end desc "stop", "Stop pool" - method_option :demfile, :type => :string, :aliases => "-D", :banner => "Path to Demfile" def stop(pool_name = nil) - control_pools_loop(pool_name, "successfully stoped", options[:demfile]) do |pool| + control_pools_loop(pool_name, "successfully stoped") do |pool| STDOUT.sync = true unless Daemonize.check_pid(pool.pid_file) print_pool pool.name, "No pid file or a stale pid file!" exit 1 end @@ -63,22 +64,32 @@ exit(0) end return true end + desc "list", "List of pools" + def list + puts "List of configured pools:" + puts "" + Daemonizer.find_pools(nil).each do |pool| + puts " * #{pool.name}" + end + puts "" + return true + end + + desc "restart", "Restart pool" - method_option :demfile, :type => :string, :aliases => "-D", :banner => "Path to Demfile" def restart(pool_name = nil) invoke :stop, pool_name invoke :start, pool_name end desc "debug", "Debug pool (do not demonize)" - method_option :demfile, :type => :string, :aliases => "-D", :banner => "Path to Demfile" def debug(pool_name = nil) puts "You should supply pool_name to debug" if pool_name.nil? - control_pools_loop(pool_name, "execution ended", options[:demfile]) do |pool| + control_pools_loop(pool_name, "execution ended") do |pool| STDOUT.sync = true print_pool pool.name, "Debugging pool: " engine = Engine.new(pool) engine.debug! @@ -88,37 +99,18 @@ end return true end private - def control_pools_loop(pool_name, message = nil, demfile = nil, &block) - find_pools(pool_name, demfile).each do |pool| + def control_pools_loop(pool_name, message = nil, &block) + Daemonizer.find_pools(pool_name).each do |pool| Process.fork do yield(pool) end Process.wait if $?.exitstatus == 0 and message print_pool pool.name, message end - end - end - - def find_pools(pool_name, demfile) - demfile_name = demfile || "Demfile" - - Daemonizer.root = File.dirname(File.expand_path(demfile_name)) - - pools = Dsl.evaluate(demfile_name) - - if pool_name - if pool = pools[pool_name.to_sym] - [pool] - else - print_pool pool_name, "pool is not configured" - [] - end - else - pools.values end end def print_pool(pool_name, message) puts "#{pool_name}: #{message}"