Class: Rubish::UnixExecutable::UnixJob

Inherits:
Job
  • Object
show all
Defined in:
lib/rubish/unix_executable.rb

Defined Under Namespace

Classes: BadExit

Instance Attribute Summary collapse

Attributes inherited from Job

#job_control, #result

Instance Method Summary collapse

Methods inherited from Job

#__finish, #__start, #done?

Constructor Details

#initialize(exe) ⇒ UnixJob

Returns a new instance of UnixJob.



16
17
18
19
20
21
22
23
24
# File 'lib/rubish/unix_executable.rb', line 16

def initialize(exe)
  # prepare_io returns an instance of ExeIO
  @ios = EIO.ios([exe.i || Rubish::Context.current.i,"r"],
                 [exe.o || Rubish::Context.current.o,"w"],
                 [exe.err || Rubish::Context.current.err,"w"])
  i,o,err = @ios
  @pids = exe.exec_with(i.io,o.io,err.io)
  __start
end

Instance Attribute Details

#badsObject (readonly)

Returns the value of attribute bads.



7
8
9
# File 'lib/rubish/unix_executable.rb', line 7

def bads
  @bads
end

#goodsObject (readonly)

Returns the value of attribute goods.



6
7
8
# File 'lib/rubish/unix_executable.rb', line 6

def goods
  @goods
end

#pidsObject (readonly)

Returns the value of attribute pids.



5
6
7
# File 'lib/rubish/unix_executable.rb', line 5

def pids
  @pids
end

Instance Method Details

#stop(sig = "TERM") ⇒ Object



48
49
50
51
52
53
# File 'lib/rubish/unix_executable.rb', line 48

def stop(sig="TERM")
  self.pids.each do |pid|
    Process.kill(sig,pid)
  end
  self.wait
end

#stop!Object



55
56
57
# File 'lib/rubish/unix_executable.rb', line 55

def stop!
  self.stop("KILL")
end

#waitObject

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubish/unix_executable.rb', line 26

def wait
  raise Rubish::Error.new("already waited") if self.done?
  begin
    exits = self.pids.map do |pid|
      Process.wait(pid)
      $?
    end
    @ios.each do |io|
      io.close
    end
    @goods, @bads = exits.partition { |status| status.exitstatus == 0}
    @result = goods # set result to processes that exit properly
    if !bads.empty?
      raise Rubish::Job::Failure.new(self,BadExit.new(bads))
    else
      return self.result
    end
  ensure
    __finish
  end
end