Class: ScreenRecorder::ChildProcess Private

Inherits:
Object
  • Object
show all
Defined in:
lib/screen-recorder/childprocess.rb

Overview

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.

Combined version of: github.com/SeleniumHQ/selenium/blob/trunk/rb/lib/selenium/webdriver/common/child_process.rb github.com/enkessler/childprocess/blob/master/lib/childprocess/process_spawn_process.rb

Since:

  • 1.7.0

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.

Since:

  • 1.7.0

Class.new(StandardError)
SIGINT =

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.

Since:

  • 1.7.0

'INT'
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.

Since:

  • 1.7.0

'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.

Since:

  • 1.7.0

'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.

Since:

  • 1.7.0

0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*command) ⇒ 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.

Since:

  • 1.7.0



24
25
26
27
28
29
# File 'lib/screen-recorder/childprocess.rb', line 24

def initialize(*command)
  @command = command
  @detach = false
  @pid = nil
  @status = nil
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.

Since:

  • 1.7.0



21
22
23
# File 'lib/screen-recorder/childprocess.rb', line 21

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.

Since:

  • 1.7.0



31
32
33
# File 'lib/screen-recorder/childprocess.rb', line 31

def io
  @io ||= ::IO.pipe
end

#pidObject

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.

Since:

  • 1.7.0



21
22
23
# File 'lib/screen-recorder/childprocess.rb', line 21

def pid
  @pid
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)

Since:

  • 1.7.0



66
67
68
# File 'lib/screen-recorder/childprocess.rb', line 66

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)

Since:

  • 1.7.0



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/screen-recorder/childprocess.rb', line 70

def exited?
  return true if @exit_code

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

  pid = nil if pid.zero? # may happen on jruby
  @exit_code = @status.exitstatus || @status.termsig if pid
  ScreenRecorder.logger.debug("  -> exit code is #{@exit_code.inspect}")

  !!pid
rescue Errno::ECHILD
  # may be thrown for detached processes
  true
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:

Since:

  • 1.7.0



87
88
89
90
91
92
93
94
# File 'lib/screen-recorder/childprocess.rb', line 87

def poll_for_exit(timeout)
  ScreenRecorder.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.

Since:

  • 1.7.0



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/screen-recorder/childprocess.rb', line 35

def start
  options = { :in => io, %i[out err] => io } # to log file

  if OS.windows?
    options[:new_pgroup] = true
  else
    options[:pgroup] = true
  end

  @pid = Process.spawn(*@command, options)
  ScreenRecorder.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.

Since:

  • 1.7.0



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/screen-recorder/childprocess.rb', line 50

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

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

  ScreenRecorder.logger.debug("  -> stopped #{@pid}")
rescue TimeoutError, Errno::EINVAL
  ScreenRecorder.logger.debug("    -> sending KILL to process: #{@pid}")
  kill
  wait
  ScreenRecorder.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.

Since:

  • 1.7.0



96
97
98
99
100
# File 'lib/screen-recorder/childprocess.rb', line 96

def wait
  return if exited?

  _, @status = waitpid2(@pid)
end