Class: ChildLabor::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/child_labor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd) ⇒ Task

Returns a new instance of Task.



21
22
23
24
25
26
# File 'lib/child_labor.rb', line 21

def initialize(cmd)
  @cmd = cmd
  @pid = nil
  @exit_status = nil
  @terminated  = false
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



19
20
21
# File 'lib/child_labor.rb', line 19

def cmd
  @cmd
end

#stderrObject (readonly)

Returns the value of attribute stderr.



19
20
21
# File 'lib/child_labor.rb', line 19

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



19
20
21
# File 'lib/child_labor.rb', line 19

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



19
20
21
# File 'lib/child_labor.rb', line 19

def stdout
  @stdout
end

Instance Method Details

#closeObject



122
123
124
125
126
# File 'lib/child_labor.rb', line 122

def close
  close_read
  close_write
  close_stderr
end

#close_readObject



107
108
109
110
# File 'lib/child_labor.rb', line 107

def close_read
  @stdout.close if @stdout
  @stdout = nil
end

#close_stderrObject



117
118
119
120
# File 'lib/child_labor.rb', line 117

def close_stderr
  @stderr.close if @stderr
  @stderr = nil
end

#close_writeObject



112
113
114
115
# File 'lib/child_labor.rb', line 112

def close_write
  @stdin.close if @stdin
  @stdin = nil
end

#exit_statusObject



66
67
68
69
# File 'lib/child_labor.rb', line 66

def exit_status
  poll_status
  @exit_status
end

#launchObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/child_labor.rb', line 28

def launch
  create_pipes

  @pid = Process.fork

  # child
  unless @pid
    @stdout.close
    STDOUT.reopen @stdout_child

    @stdin.close
    STDIN.reopen @stdin_child

    @stderr.close
    STDERR.reopen @stderr_child

    Process.exec cmd
  end

  @stdout_child.close
  @stdin_child.close
  @stderr_child.close

  true
end

#launched?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/child_labor.rb', line 54

def launched?
  @pid
end

#read(length = nil, buffer = nil) ⇒ Object



87
88
89
# File 'lib/child_labor.rb', line 87

def read(length = nil, buffer = nil)
  @stdout.read(length, buffer)
end

#read_stderr(length = nil, buffer = nil) ⇒ Object



95
96
97
# File 'lib/child_labor.rb', line 95

def read_stderr(length = nil, buffer = nil)
  @stderr.read(length, buffer)
end

#readlineObject



91
92
93
# File 'lib/child_labor.rb', line 91

def readline
  @stdout.readline
end

#resumeObject



79
80
81
# File 'lib/child_labor.rb', line 79

def resume
  signal('CONT')
end

#running?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/child_labor.rb', line 58

def running?
  poll_status(Process::WNOHANG)
end

#signal(signal) ⇒ Object



103
104
105
# File 'lib/child_labor.rb', line 103

def signal(signal)
  Process.kill(signal, @pid)
end

#suspendObject



75
76
77
# File 'lib/child_labor.rb', line 75

def suspend
  signal('STOP')
end

#terminate(signal = 'TERM') ⇒ Object



83
84
85
# File 'lib/child_labor.rb', line 83

def terminate(signal = 'TERM')
  signal(signal)
end

#terminated?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/child_labor.rb', line 62

def terminated?
  launched? && !running?
end

#waitObject



71
72
73
# File 'lib/child_labor.rb', line 71

def wait
  exit_status
end

#write(str) ⇒ Object



99
100
101
# File 'lib/child_labor.rb', line 99

def write(str)
  @stdin.write(str)
end