lib/daemonizer/cli.rb in daemonizer-0.4.14 vs lib/daemonizer/cli.rb in daemonizer-0.4.15

- old
+ new

@@ -1,19 +1,19 @@ require 'thor' require 'rubygems/config_file' -module Daemonizer +module Daemonizer class CLI < Thor check_unknown_options! method_option :daemonfile, :type => :string, :aliases => "-D", :banner => "Path to Daemonfile" def initialize(*) super Daemonizer.daemonfile = options[:daemonfile] || "Daemonfile" end - + desc "start", "Start pool" def start(pool_name = nil) control_pools_loop(pool_name, "successfully started") do |pool| # Pid file check if Daemonize.check_pid(pool.pid_file) @@ -22,11 +22,11 @@ end print_pool pool.name, "Starting pool" app_name = "#{pool.name} monitor\0" - + Daemonize.daemonize(app_name) Dir.chdir(Daemonizer.root) # Make sure we're in the working directory # Pid file creation @@ -39,11 +39,11 @@ # Workers exited, cleaning up File.delete(pool.pid_file) rescue nil end return true end - + desc "stop", "Stop pool" def stop(pool_name = nil) control_pools_loop(pool_name, "successfully stoped") do |pool| STDOUT.sync = true unless Daemonize.check_pid(pool.pid_file) @@ -67,13 +67,13 @@ end desc "list", "List of pools" def list puts "List of configured pools:" - puts "" + puts "" Daemonizer.find_pools(nil).each do |pool| - puts " * #{pool.name}" + puts " * #{pool.name}" end puts "" return true end @@ -86,52 +86,54 @@ desc "debug", "Debug pool (do not demonize)" def debug(pool_name = nil) if pool_name.nil? puts "You should supply pool_name to debug" exit 1 - end - control_pools_loop(pool_name, "execution ended", true) do |pool| + end + control_pools_loop(pool_name, "execution ended", true) do |pool| STDOUT.sync = true - + engine = Engine.new(pool) engine.debug! - + print_pool pool.name, " Done!" exit(0) end - return true + return true end - + desc "stats", "Pools statistics" def stats(pool_name = nil) Daemonizer.find_pools(pool_name).each do |pool| statistics = Daemonizer::Stats::MemoryStats.new(pool) statistics.print end end - + private def control_pools_loop(pool_name, message = nil, debug = false, &block) - Daemonizer.find_pools(pool_name).each do |pool| - Process.fork do - if debug - Daemonizer.init_console_logger(pool.name.to_s) - else - Daemonizer.init_logger(pool.name.to_s, pool.log_file) - end + if debug + pool = Daemonizer.find_pools(pool_name).first + Daemonizer.init_console_logger(pool.name.to_s) + begin yield(pool) + rescue Interrupt => e + puts "Interrupted from keyboard" end - begin + else + Daemonizer.find_pools(pool_name).each do |pool| + Process.fork do + Daemonizer.init_logger(pool.name.to_s, pool.log_file) + yield(pool) + end Process.wait if $?.exitstatus == 0 and message - print_pool pool.name, message + print_pool pool.name, message end - rescue Interrupt => e - puts "Interrupted from keyboard" end end end - + def print_pool(pool_name, message) puts "#{pool_name}: #{message}" end end end