-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92cfe38
commit 1418c3d
Showing
5 changed files
with
129 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# frozen_string_literal: true | ||
|
||
module ScreenRecorder | ||
# | ||
# @api private | ||
# | ||
# @since 1.7.0 | ||
# | ||
# Original from https://github.com/SeleniumHQ/selenium/blob/trunk/rb/lib/selenium/webdriver/common/child_process.rb | ||
class Process | ||
TimeoutError = Class.new(StandardError) | ||
|
||
SIGTERM = 'TERM' | ||
SIGKILL = 'KILL' | ||
|
||
POLL_INTERVAL = 0.1 | ||
|
||
attr_accessor :detach, :pid | ||
attr_writer :io | ||
|
||
def self.build(*command) | ||
new(*command) | ||
end | ||
|
||
def initialize(*command) | ||
@command = command | ||
@detach = false | ||
@pid = nil | ||
@status = nil | ||
end | ||
|
||
def io | ||
@io ||= ::IO.pipe | ||
end | ||
|
||
def start | ||
options = { :in => io, %i[out err] => io } | ||
options[:pgroup] = true unless OS.windows? # NOTE: this is a bug only in Windows 7 | ||
|
||
@pid = ::Process.spawn(*@command, options) | ||
ScreenRecorder.logger.debug(" -> pid: #{@pid}") | ||
|
||
::Process.detach(@pid) if detach | ||
end | ||
|
||
def stop(timeout = 3) | ||
return unless @pid | ||
return if exited? | ||
|
||
ScreenRecorder.logger.debug("Sending TERM to process: #{@pid}") | ||
terminate(-@pid) | ||
poll_for_exit(timeout) | ||
|
||
ScreenRecorder.logger.debug(" -> stopped #{@pid}") | ||
rescue TimeoutError, Errno::EINVAL | ||
ScreenRecorder.logger.debug(" -> sending KILL to process: #{@pid}") | ||
kill(@pid) | ||
wait | ||
ScreenRecorder.logger.debug(" -> killed #{@pid}") | ||
end | ||
|
||
def alive? | ||
@pid && !exited? | ||
end | ||
|
||
def exited? | ||
return false unless @pid | ||
|
||
ScreenRecorder.logger.debug("Checking if #{@pid} is exited:") | ||
_, @status = waitpid2(@pid, ::Process::WNOHANG | ::Process::WUNTRACED) if @status.nil? | ||
return false if @status.nil? | ||
|
||
exit_code = @status.exitstatus || @status.termsig | ||
ScreenRecorder.logger.debug(" -> exit code is #{exit_code.inspect}") | ||
|
||
!!exit_code | ||
end | ||
|
||
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 | ||
|
||
def wait | ||
return if exited? | ||
|
||
_, @status = waitpid2(@pid) | ||
end | ||
|
||
private | ||
|
||
def terminate(pid) | ||
::Process.kill(SIGTERM, pid) | ||
end | ||
|
||
def kill(pid) | ||
::Process.kill(SIGKILL, pid) | ||
rescue Errno::ECHILD, Errno::ESRCH | ||
# already dead | ||
end | ||
|
||
def waitpid2(pid, flags = 0) | ||
::Process.waitpid2(pid, flags) | ||
rescue Errno::ECHILD | ||
# already dead | ||
end | ||
end # Process | ||
end # Common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters