Class: MinioRunner::ChildProcess Private

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

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

Constant Summary collapse

TimeoutError =

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

Class.new(StandardError)
SIGTERM =

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

"TERM"
SIGKILL =

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

"KILL"
POLL_INTERVAL =

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

0.1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, env: {}, log_file: File::NULL) ⇒ ChildProcess

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.

Returns a new instance of ChildProcess.



43
44
45
46
47
48
49
50
# File 'lib/minio_runner/child_process.rb', line 43

def initialize(command, env: {}, log_file: File::NULL)
  @command = command
  @detach = false
  @pid = nil
  @status = nil
  @env = env
  @log_file = log_file
end

Instance Attribute Details

#detachObject

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.



35
36
37
# File 'lib/minio_runner/child_process.rb', line 35

def detach
  @detach
end

#ioObject

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.



52
53
54
# File 'lib/minio_runner/child_process.rb', line 52

def io
  @io ||= File::NULL
end

#pidObject (readonly)

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.



37
38
39
# File 'lib/minio_runner/child_process.rb', line 37

def pid
  @pid
end

Class Method Details

.build(command, env: {}, log_file: File::NULL) ⇒ Object

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.



39
40
41
# File 'lib/minio_runner/child_process.rb', line 39

def self.build(command, env: {}, log_file: File::NULL)
  new(command, env: env, log_file: log_file)
end

Instance Method Details

#alive?Boolean

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.

Returns:

  • (Boolean)


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

def alive?
  @pid && !exited?
end

#exited?Boolean

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.

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/minio_runner/child_process.rb', line 87

def exited?
  return unless @pid

  MinioRunner.logger.debug("Checking if #{@pid} is exited:")
  _, @status = Process.waitpid2(@pid, Process::WNOHANG | Process::WUNTRACED) if @status.nil?
  return if @status.nil?

  exit_code = @status.exitstatus || @status.termsig
  MinioRunner.logger.debug("  -> exit code is #{exit_code.inspect}")

  !!exit_code
end

#poll_for_exit(timeout) ⇒ Object

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.

Raises:



100
101
102
103
104
105
106
107
# File 'lib/minio_runner/child_process.rb', line 100

def poll_for_exit(timeout)
  MinioRunner.logger.debug("Polling #{timeout} seconds for exit of #{@pid}")

  end_time = Time.now + timeout
  sleep POLL_INTERVAL until exited? || Time.now > end_time

  raise TimeoutError, "  ->  #{@pid} still alive after #{timeout} seconds" unless exited?
end

#startObject

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.



56
57
58
59
60
61
62
63
64
65
# File 'lib/minio_runner/child_process.rb', line 56

def start
  options = { %i[out err] => @log_file }

  # TODO (martin) Maybe don't log ENV here?
  MinioRunner.logger.debug("Starting process: #{@command} with #{options} and ENV #{@env}")
  @pid = Process.spawn(@env, @command.join(" "), options)
  MinioRunner.logger.debug("  -> pid: #{@pid}")

  Process.detach(@pid) if detach
end

#stop(timeout = 3) ⇒ Object

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.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/minio_runner/child_process.rb', line 67

def stop(timeout = 3)
  return unless @pid
  return if exited?

  MinioRunner.logger.debug("Sending TERM to process: #{@pid}")
  terminate(@pid)
  poll_for_exit(timeout)

  MinioRunner.logger.debug("  -> stopped #{@pid}")
rescue TimeoutError, Errno::EINVAL
  MinioRunner.logger.debug("    -> sending KILL to process: #{@pid}")
  kill(@pid)
  wait
  MinioRunner.logger.debug("      -> killed #{@pid}")
end

#waitObject

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.



109
110
111
112
113
# File 'lib/minio_runner/child_process.rb', line 109

def wait
  return if exited?

  _, @status = Process.waitpid2(@pid)
end