lib/runnable.rb in runnable-0.2.0 vs lib/runnable.rb in runnable-0.2.1
- old
+ new
@@ -16,10 +16,11 @@
# along with Runnable. If not, see <http://www.gnu.org/licenses/>.
require 'runnable/gnu'
require 'runnable/extended'
+require 'fileutils'
# Convert a executable command in a Ruby-like class
# you are able to start, define params and send signals (like kill, or stop)
#
# @example Usage:
@@ -220,10 +221,14 @@
# @return [nil]
def join
@run_thread.join if @run_thread.alive?
end
+ def running?
+ Dir.exists?( "/proc/#{@pid}")
+ end
+
# Calculate the estimated memory usage in Kb.
# @return [Number] Estimated mem usage in Kb.
def mem
File.open( "/proc/#{@pid}/status" ).read.split( "\n" )[11].split( " " )[1].to_i
end
@@ -346,24 +351,39 @@
# This method is a hook so it should be overwritten in child classes.
# @param [Array] exceptions Array containing exceptions raised during the command execution.
# @return [nil]
def failed( exceptions )
end
-
- protected
# Send the desired signal to the command.
# @param [Symbol] Signal to be send to the command.
# @todo raise ESRCH if pid is not in system
# or EPERM if pid is not from user.
- def send_signal( signal )
+ def send_signal( signal )
if signal == :stop
- Process.kill( :SIGINT, @pid )
+ signal = :SIGINT
elsif signal == :kill
- Process.kill( :SIGKILL, @pid )
+ signal = :SIGKILL
end
+
+ `ps -ef`.each_line do |line|
+ line = line.split
+ pid = line[1]
+ ppid = line[2]
+
+ if ppid.to_i == @pid
+ Process.kill( signal, pid.to_i )
+ end
+ end
+
+ begin
+ Process.kill( signal, @pid )
+ rescue Errno::ESRCH
+ # As we kill child processes, main process may have exit already
+ end
end
-
+
+ protected
# Redirect command I/O to log files.
# These files are located in /var/log/runnable.
# @param [Hash] Outputs options.
# @option outputs stream [Symbol] Stream name.
# @option outputs pipes [IO] I/O stream to be redirected.