Class: Aggkit::ChildProcess::AbstractProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/aggkit/childprocess/abstract_process.rb

Direct Known Subclasses

JRuby::Process, Unix::Process, Windows::Process

Constant Summary collapse

POLL_INTERVAL =
0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ AbstractProcess

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new process with the given args.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/aggkit/childprocess/abstract_process.rb', line 45

def initialize(args)
  unless args.all? { |e| e.kind_of?(String) }
    raise ArgumentError, "all arguments must be String: #{args.inspect}"
  end

  @args        = args
  @started     = false
  @exit_code   = nil
  @io          = nil
  @cwd         = nil
  @detach      = false
  @duplex      = false
  @leader      = false
  @environment = {}
end

Instance Attribute Details

#cwdObject

Set the child’s current working directory.



28
29
30
# File 'lib/aggkit/childprocess/abstract_process.rb', line 28

def cwd
  @cwd
end

#detachObject

Set this to true if you do not care about when or if the process quits.



13
14
15
# File 'lib/aggkit/childprocess/abstract_process.rb', line 13

def detach
  @detach
end

#duplexObject

Set this to true if you want to write to the process’ stdin (process.io.stdin)



18
19
20
# File 'lib/aggkit/childprocess/abstract_process.rb', line 18

def duplex
  @duplex
end

#environmentObject (readonly)

Modify the child’s environment variables



23
24
25
# File 'lib/aggkit/childprocess/abstract_process.rb', line 23

def environment
  @environment
end

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



6
7
8
# File 'lib/aggkit/childprocess/abstract_process.rb', line 6

def exit_code
  @exit_code
end

#leaderObject

Set this to true to make the child process the leader of a new process group

This can be used to make sure that all grandchildren are killed when the child process dies.



36
37
38
# File 'lib/aggkit/childprocess/abstract_process.rb', line 36

def leader
  @leader
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'lib/aggkit/childprocess/abstract_process.rb', line 8

def status
  @status
end

Instance Method Details

#alive?Boolean

Is this process running?

Returns:

  • (Boolean)


136
137
138
# File 'lib/aggkit/childprocess/abstract_process.rb', line 136

def alive?
  started? && !exited?
end

#crashed?Boolean

Returns true if the process has exited and the exit code was not 0.

Returns:

  • (Boolean)


146
147
148
# File 'lib/aggkit/childprocess/abstract_process.rb', line 146

def crashed?
  exited? && @exit_code != 0
end

#exited?Boolean

Did the process exit?

Returns:

  • (Boolean)

Raises:



116
117
118
# File 'lib/aggkit/childprocess/abstract_process.rb', line 116

def exited?
  raise SubclassResponsibility, "exited?"
end

#ioObject

Returns a ChildProcess::AbstractIO subclass to configure the child’s IO streams.



65
66
67
# File 'lib/aggkit/childprocess/abstract_process.rb', line 65

def io
  raise SubclassResponsibility, "io"
end

#pidInteger

Returns the pid of the process after it has started.

Returns:

  • (Integer)

    the pid of the process after it has started

Raises:



73
74
75
# File 'lib/aggkit/childprocess/abstract_process.rb', line 73

def pid
  raise SubclassResponsibility, "pid"
end

#poll_for_exit(timeout) ⇒ Object

Wait for the process to exit, raising a ChildProcess::TimeoutError if the timeout expires.



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/aggkit/childprocess/abstract_process.rb', line 155

def poll_for_exit(timeout)
  log "polling #{timeout} seconds for exit"

  end_time = Time.now + timeout
  until (ok = exited?) || Time.now > end_time
    sleep POLL_INTERVAL
  end

  unless ok
    raise TimeoutError, "process still alive after #{timeout} seconds"
  end
end

#startAbstractProcess

Launch the child process

Returns:



83
84
85
86
87
88
# File 'lib/aggkit/childprocess/abstract_process.rb', line 83

def start
  launch_process
  @started = true

  self
end

#started?Boolean

Has the process started?

Returns:

  • (Boolean)


126
127
128
# File 'lib/aggkit/childprocess/abstract_process.rb', line 126

def started?
  @started
end

#stop(timeout = 3) ⇒ Object

Forcibly terminate the process, using increasingly harsher methods if possible.

Parameters:

  • timeout (Integer) (defaults to: 3)

    (3) Seconds to wait before trying the next method.

Raises:



96
97
98
# File 'lib/aggkit/childprocess/abstract_process.rb', line 96

def stop(timeout = 3)
  raise SubclassResponsibility, "stop"
end

#waitInteger

Block until the process has been terminated.

Returns:

  • (Integer)

    The exit status of the process

Raises:



106
107
108
# File 'lib/aggkit/childprocess/abstract_process.rb', line 106

def wait
  raise SubclassResponsibility, "wait"
end