lib/rexec/daemon/controller.rb in rexec-1.5.0 vs lib/rexec/daemon/controller.rb in rexec-1.5.1

- old
+ new

@@ -19,10 +19,12 @@ # THE SOFTWARE. require 'rexec/daemon/process_file' require 'rexec/task' +require 'rainbow' + module RExec module Daemon # Daemon startup timeout TIMEOUT = 5 @@ -51,20 +53,20 @@ end end # This function starts the supplied daemon def self.start(daemon) - puts "Starting daemon..." + puts "Starting daemon...".color(:blue) case ProcessFile.status(daemon) when :running - $stderr.puts "Daemon already running!" + $stderr.puts "Daemon already running!".color(:blue) return when :stopped # We are good to go... else - $stderr.puts "Daemon in unknown state! Will clear previous state and continue." + $stderr.puts "Daemon in unknown state! Will clear previous state and continue.".color(:red) status(daemon) ProcessFile.clear(daemon) end daemon.prefork @@ -123,18 +125,18 @@ $stderr.puts "=== Daemon Stopping @ #{Time.now.to_s} ===" $stderr.flush end end - puts "Waiting for daemon to start..." + puts "Waiting for daemon to start...".color(:blue) sleep 0.1 timer = TIMEOUT pid = ProcessFile.recall(daemon) while pid == nil and timer > 0 # Wait a moment for the forking to finish... - puts "Waiting for daemon to start (#{timer}/#{TIMEOUT})" + puts "Waiting for daemon to start (#{timer}/#{TIMEOUT})".color(:blue) sleep 1 # If the daemon has crashed, it is never going to start... break if daemon.crashed? @@ -146,40 +148,41 @@ # Prints out the status of the daemon def self.status(daemon) case ProcessFile.status(daemon) when :running - puts "Daemon status: running pid=#{ProcessFile.recall(daemon)}" + puts "Daemon status: running pid=#{ProcessFile.recall(daemon)}".color(:green) when :unknown if daemon.crashed? - puts "Daemon status: crashed" + puts "Daemon status: crashed".color(:red) $stdout.flush - daemon.tail_error_log($stderr) + $stderr.puts "Dumping daemon crash log:".color(:red) + daemon.tail_log($stderr) else - puts "Daemon status: unknown" + puts "Daemon status: unknown".color(:red) end when :stopped - puts "Daemon status: stopped" + puts "Daemon status: stopped".color(:blue) end end # Stops the daemon process. def self.stop(daemon) - puts "Stopping daemon..." + puts "Stopping daemon...".color(:blue) # Check if the pid file exists... unless File.file?(daemon.process_file_path) - puts "Pid file not found. Is the daemon running?" + puts "Pid file not found. Is the daemon running?".color(:red) return end pid = ProcessFile.recall(daemon) # Check if the daemon is already stopped... unless ProcessFile.running(daemon) - puts "Pid #{pid} is not running. Has daemon crashed?" + puts "Pid #{pid} is not running. Has daemon crashed?".color(:red) return end pid = ProcessFile.recall(daemon) Process.kill("INT", pid) @@ -189,19 +192,19 @@ # it a few more times. attempts = 5 while ProcessFile.running(daemon) and attempts > 0 sig = (attempts < 2) ? "KILL" : "TERM" - puts "Sending #{sig} to pid #{pid}..." + puts "Sending #{sig} to pid #{pid}...".color(:red) Process.kill(sig, pid) sleep 1 attempts -= 1 end # If after doing our best the daemon is still running (pretty odd)... if ProcessFile.running(daemon) - puts "Daemon appears to be still running!" + puts "Daemon appears to be still running!".color(:red) return end # Otherwise the daemon has been stopped. ProcessFile.clear(daemon)