class Instance
Represents a running child process from the point of view of the child process.
Definitions
def self.for(process)
Wrap an instance around the Process
instance from within the forked child.
Signature
-
parameter
process
Process
The process intance to wrap.
Implementation
def self.for(process)
instance = self.new(process.out)
# The child process won't be reading from the channel:
process.close_read
instance.name = process.name
return instance
end
def initialize(io)
Initialize the child process instance.
Signature
-
parameter
io
IO
The IO object to use for communication.
Implementation
def initialize(io)
super
@name = nil
end
def as_json(...)
Generate a hash representation of the process.
Signature
-
returns
Hash
The process as a hash, including
process_id
andname
.
Implementation
def as_json(...)
{
process_id: ::Process.pid,
name: @name,
}
end
def to_json(...)
Generate a JSON representation of the process.
Signature
-
returns
String
The process as JSON.
Implementation
def to_json(...)
as_json.to_json(...)
end
def name= value
Set the process title to the specified value.
Signature
-
parameter
value
String
The name of the process.
Implementation
def name= value
@name = value
# This sets the process title to an empty string if the name is nil:
::Process.setproctitle(@name.to_s)
end
def name
Signature
-
returns
String
The name of the process.
Implementation
def name
@name
end
def exec(*arguments, ready: true, **options)
Replace the current child process with a different one. Forwards arguments and options to ::Process.exec
.
This method replaces the child process with the new executable, thus this method never returns.
Signature
-
parameter
arguments
Array
The arguments to pass to the new process.
-
parameter
ready
Boolean
If true, informs the parent process that the child is ready. Otherwise, the child process will need to use a notification protocol to inform the parent process that it is ready.
-
parameter
options
Hash
Additional options to pass to
::Process.exec
.
Implementation
def exec(*arguments, ready: true, **options)
if ready
self.ready!(status: "(exec)")
else
self.before_spawn(arguments, options)
end
::Process.exec(*arguments, **options)
end