Module: CMD::SmartIO

Defined in:
lib/rbbt/util/cmd.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cmdObject

Returns the value of attribute cmd.



7
8
9
# File 'lib/rbbt/util/cmd.rb', line 7

def cmd
  @cmd
end

#errObject

Returns the value of attribute err.



7
8
9
# File 'lib/rbbt/util/cmd.rb', line 7

def err
  @err
end

#inObject

Returns the value of attribute in.



7
8
9
# File 'lib/rbbt/util/cmd.rb', line 7

def in
  @in
end

#logObject

Returns the value of attribute log.



7
8
9
# File 'lib/rbbt/util/cmd.rb', line 7

def log
  @log
end

#outObject

Returns the value of attribute out.



7
8
9
# File 'lib/rbbt/util/cmd.rb', line 7

def out
  @out
end

#pidObject

Returns the value of attribute pid.



7
8
9
# File 'lib/rbbt/util/cmd.rb', line 7

def pid
  @pid
end

#postObject

Returns the value of attribute post.



7
8
9
# File 'lib/rbbt/util/cmd.rb', line 7

def post
  @post
end

Class Method Details

.tie(io, pid = nil, cmd = "", post = nil, sin = nil, out = nil, err = nil, log = true) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rbbt/util/cmd.rb', line 8

def self.tie(io, pid = nil, cmd = "",  post = nil, sin = nil, out = nil, err = nil, log = true)
  io.extend SmartIO
  io.pid = pid
  io.cmd = cmd
  io.in  = sin 
  io.out  = out 
  io.err  = err 
  io.post = post
  io.log = log

  io.class.send(:alias_method, :original_close, :close)
  io.class.send(:alias_method, :original_read, :read)
  io
end

Instance Method Details

#closeObject



44
45
46
47
48
49
50
51
52
# File 'lib/rbbt/util/cmd.rb', line 44

def close
  self.original_read unless self.closed? or self.eof?

  wait_and_status

  @post.call if @post

  original_close unless self.closed?
end

#force_closeObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rbbt/util/cmd.rb', line 54

def force_close
  if @pid
    Log.debug{"Forcing close by killing '#{@pid}'" if log}
    begin
      Process.kill("KILL", @pid)
      Process.waitpid(@pid)
    rescue
      Log.low{"Exception in forcing close of command [#{ @pid }, #{cmd}]: #{$!.message}"}
    end
  end

  @post.call if @post

  original_close unless self.closed?
end

#read(*args) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/rbbt/util/cmd.rb', line 70

def read(*args)
  data = original_read(*args) unless self.closed? #or self.eof?

  self.close if not self.closed? and self.eof?

  data #|| ""
end

#wait_and_statusObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rbbt/util/cmd.rb', line 23

def wait_and_status
  if @pid
    begin
      Process.waitpid(@pid)
    rescue
    end

    Log.debug{"Process #{ cmd } succeded" if $? and $?.success? and log}

    if $? and not $?.success?
      Log.debug{"Raising exception" if log}
      exception = ProcessFailed.new "Command [#{@pid}] '#{@cmd}' failed with error status #{$?.exitstatus}"
      begin
        original_close
      ensure
        raise exception
      end
    end
  end
end