class Async::Container::Process

Represents a running child process from the point of view of the parent container.

Attributes

name[R]

The name of the process. @attribute [String]

Public Class Methods

fork(**options) { |for| ... } click to toggle source

Fork a child process appropriate for a container. @returns [Process]

# File lib/async/container/process.rb, line 83
def self.fork(**options)
        self.new(**options) do |process|
                ::Process.fork do
                        Signal.trap(:INT) {raise Interrupt}
                        Signal.trap(:TERM) {raise Terminate}
                        
                        begin
                                yield Instance.for(process)
                        rescue Interrupt
                                # Graceful exit.
                        rescue Exception => error
                                Console.logger.error(self) {error}
                                
                                exit!(1)
                        end
                end
        end
end
new(name: nil) { |self| ... } click to toggle source

Initialize the process. @parameter name [String] The name to use for the child process.

Calls superclass method Async::Container::Channel::new
# File lib/async/container/process.rb, line 114
def initialize(name: nil)
        super()
        
        @name = name
        @status = nil
        @pid = nil
        
        @pid = yield(self)
        
        # The parent process won't be writing to the channel:
        self.close_write
end

Public Instance Methods

close() click to toggle source

Invoke {#terminate!} and then {#wait} for the child process to exit.

Calls superclass method Async::Container::Channel#close
# File lib/async/container/process.rb, line 147
def close
        self.terminate!
        self.wait
ensure
        super
end
interrupt!() click to toggle source

Send `SIGINT` to the child process.

# File lib/async/container/process.rb, line 155
def interrupt!
        unless @status
                ::Process.kill(:INT, @pid)
        end
end
name=(value) click to toggle source

Set the name of the process. Invokes {::Process.setproctitle} if invoked in the child process.

# File lib/async/container/process.rb, line 129
def name= value
        @name = value
        
        # If we are the child process:
        ::Process.setproctitle(@name) if @pid.nil?
end
terminate!() click to toggle source

Send `SIGTERM` to the child process.

# File lib/async/container/process.rb, line 162
def terminate!
        unless @status
                ::Process.kill(:TERM, @pid)
        end
end
to_s() click to toggle source

A human readable representation of the process. @returns [String]

# File lib/async/container/process.rb, line 142
def to_s
        "\#<#{self.class} #{@name}>"
end
wait() click to toggle source

Wait for the child process to exit. @returns [::Process::Status] The process exit status.

# File lib/async/container/process.rb, line 170
def wait
        if @pid && @status.nil?
                _, @status = ::Process.wait2(@pid, ::Process::WNOHANG)
                
                if @status.nil?
                        sleep(0.01)
                        _, @status = ::Process.wait2(@pid, ::Process::WNOHANG)
                end
                
                if @status.nil?
                        Console.logger.warn(self) {"Process #{@pid} is blocking, has it exited?"}
                        _, @status = ::Process.wait2(@pid)
                end
        end
        
        return @status
end