Class: ScreenRecorder::ChildProcess Private
- Inherits:
-
Object
- Object
- ScreenRecorder::ChildProcess
- 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
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)
- 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.
'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.
'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
- #detach ⇒ Object private
- #io ⇒ Object private
- #pid ⇒ Object private
Instance Method Summary collapse
- #alive? ⇒ Boolean private
- #exited? ⇒ Boolean private
-
#initialize(*command) ⇒ ChildProcess
constructor
private
A new instance of ChildProcess.
- #poll_for_exit(timeout) ⇒ Object private
- #start ⇒ Object private
- #stop(timeout = 3) ⇒ Object private
- #wait ⇒ Object private
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.
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
#detach ⇒ 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.
21 22 23 |
# File 'lib/screen-recorder/childprocess.rb', line 21 def detach @detach end |
#io ⇒ 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.
31 32 33 |
# File 'lib/screen-recorder/childprocess.rb', line 31 def io @io ||= ::IO.pipe end |
#pid ⇒ 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.
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.
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.
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.
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 |
#start ⇒ 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.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/screen-recorder/childprocess.rb', line 35 def start = { :in => io, %i[out err] => io } # to log file if OS.windows? [:new_pgroup] = true else [:pgroup] = true end @pid = Process.spawn(*@command, ) 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.
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 |
#wait ⇒ 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.
96 97 98 99 100 |
# File 'lib/screen-recorder/childprocess.rb', line 96 def wait return if exited? _, @status = waitpid2(@pid) end |