lib/runnable.rb in runnable-0.2.2 vs lib/runnable.rb in runnable-0.2.3
- old
+ new
@@ -38,11 +38,17 @@
attr_reader :owner
# Process group.
attr_reader :group
# Directory where process was called from.
attr_reader :pwd
-
+
+ # Input file
+ attr_accessor :input
+
+ # Set the output file
+ attr_accessor :output
+
# Metaprogramming part of the class
# Define the parameter style to be used.
# @return [nil]
def self.command_style( style )
@@ -94,15 +100,21 @@
else
@delete_log = option_hash[:delete_log]
end
# Store input options
- @input = Array.new
+ @input = String.new
# Store output options
- @output = Array.new
+ @output = String.new
+ # Standar Outputs
+ @std_output = {
+ :out => "",
+ :err => ""
+ }
+
# @todo: checks that command is in the PATH
# ...
# we dont set the pid, because we dont know until run
@pid = nil
@@ -131,14 +143,14 @@
err_rd, err_wr = IO.pipe
# Set up the command line
command = []
command << @command
- command << @input.join( " " )
+ command << @input
command << @options
command << @command_line_interface.parse
- command << @output.join( " " )
+ command << @output
command = command.join( " " )
@pid = Process.spawn( command, { :out => out_wr, :err => err_wr } )
# Include instance in class variable
@@ -228,14 +240,28 @@
# @return [nil]
def join
@run_thread.join if @run_thread.alive?
end
+ # Check if prcess is running on the system.
+ # @return [Bool] True if process is running, false if it is not.
def running?
Dir.exists?( "/proc/#{@pid}")
end
+ # Standar output of command
+ # @return [String] Standar output
+ def std_out
+ @std_output[:out]
+ end
+
+ # Standar error output of the command
+ # @return [String] Standar error output
+ def std_err
+ @std_output[:err]
+ 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
@@ -273,22 +299,26 @@
0
end
end
- # Set the input files.
- # @param [String] param Input to be parsed as command options.
- # @return [nil]
- def input( param )
- @input << param
- end
+ # Estimated bandwidth in kb/s.
+ # @param [String] iface Interface to be scaned.
+ # @param [Number] sample_time Time passed between samples in seconds.
+ # The longest lapse the more accurate stimation.
+ # @return [Number] The estimated bandwidth used.
+ def bandwidth( iface, sample_lapse = 0.1 )
+ file = "/proc/#{@pid}/net/dev"
+ File.open( file ).read =~ /#{iface}:\s+(\d+)\s+/
+ init = $1.to_i
+
+ sleep sample_lapse
- # Set the output files.
- # @param [String] param Output to be parsed as command options.
- # @return [nil]
- def output( param )
- @output << param
+ File.open( file ).read =~ /#{iface}:\s+(\d+)\s+/
+ finish = $1.to_i
+
+ (finish - init)*(1/sample_lapse)/1024
end
# Convert undefined methods (ruby-like syntax) into parameters
# to be parsed at the execution time.
# This only convert methods with zero or one parameters. A hash can be passed
@@ -405,10 +435,14 @@
# stream and write it in a log file
outputs.each do |output_name, pipes|
@output_threads << Thread.new do
pipes[0].close
+ @std_output[output_name] = ""
+
pipes[1].each_line do |line|
+ @std_output[output_name] << line
+
File.open("#{@log_path}#{@command}_#{@pid}.log", "a") do |log_file|
log_file.puts( "[#{Time.new.inspect} || [STD#{output_name.to_s.upcase} || [#{@pid}]] #{line}" )
end
# Match custom exceptions
# if we get a positive match, add it to the exception array