Class: Async::Container::Forked::Child::Instance

Inherits:
Notify::Pipe show all
Defined in:
lib/async/container/forked.rb

Overview

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

Constant Summary

Constants inherited from Notify::Pipe

Notify::Pipe::NOTIFY_PIPE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Notify::Pipe

#before_spawn, open!, #send

Methods inherited from Notify::Client

#error!, #ready!, #reloading!, #restarting!, #status!, #stopping!

Constructor Details

#initialize(io) ⇒ Instance

Initialize the child process instance.



41
42
43
44
45
# File 'lib/async/container/forked.rb', line 41

def initialize(io)
  super
  
  @name = nil
end

Class Method Details

.for(process) ⇒ Object

Wrap an instance around the Process instance from within the forked child.



27
28
29
30
31
32
33
34
35
36
# File 'lib/async/container/forked.rb', line 27

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

Instance Method Details

#as_jsonObject

Generate a hash representation of the process.



50
51
52
53
54
55
# File 'lib/async/container/forked.rb', line 50

def as_json(...)
  {
    process_id: ::Process.pid,
    name: @name,
  }
end

#exec(*arguments, ready: true, **options) ⇒ Object

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.



85
86
87
88
89
90
91
92
93
# File 'lib/async/container/forked.rb', line 85

def exec(*arguments, ready: true, **options)
  if ready
    self.ready!(status: "(exec)")
  else
    self.before_spawn(arguments, options)
  end
  
  ::Process.exec(*arguments, **options)
end

#nameObject



75
76
77
# File 'lib/async/container/forked.rb', line 75

def name
  @name
end

#name=(value) ⇒ Object

Set the process title to the specified value.



67
68
69
70
71
72
# File 'lib/async/container/forked.rb', line 67

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

#to_jsonObject

Generate a JSON representation of the process.



60
61
62
# File 'lib/async/container/forked.rb', line 60

def to_json(...)
  as_json.to_json(...)
end