Class: Ferrum::Browser::Process
- Inherits:
-
Object
- Object
- Ferrum::Browser::Process
- Extended by:
- Forwardable
- Defined in:
- lib/ferrum/browser/process.rb
Constant Summary collapse
- KILL_TIMEOUT =
2
- WAIT_KILLED =
0.05
Instance Attribute Summary collapse
-
#browser_version ⇒ Object
readonly
Returns the value of attribute browser_version.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#default_user_agent ⇒ Object
readonly
Returns the value of attribute default_user_agent.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#protocol_version ⇒ Object
readonly
Returns the value of attribute protocol_version.
-
#v8_version ⇒ Object
readonly
Returns the value of attribute v8_version.
-
#webkit_version ⇒ Object
readonly
Returns the value of attribute webkit_version.
-
#ws_url ⇒ Object
readonly
Returns the value of attribute ws_url.
-
#xvfb ⇒ Object
readonly
Returns the value of attribute xvfb.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options) ⇒ Process
constructor
A new instance of Process.
- #inspect ⇒ Object
- #restart ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(options) ⇒ Process
Returns a new instance of Process.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ferrum/browser/process.rb', line 62 def initialize() @pid = @xvfb = @user_data_dir = nil if .ws_url response = parse_json_version(.ws_url) self.ws_url = response&.[]("webSocketDebuggerUrl") || .ws_url return end if .url response = parse_json_version(.url) self.ws_url = response&.[]("webSocketDebuggerUrl") return end @logger = .logger @process_timeout = .process_timeout @env = Hash(.env) tmpdir = Dir.mktmpdir("ferrum_user_data_dir_") ObjectSpace.define_finalizer(self, self.class.directory_remover(tmpdir)) @user_data_dir = tmpdir @command = Command.build(, tmpdir) end |
Instance Attribute Details
#browser_version ⇒ Object (readonly)
Returns the value of attribute browser_version.
19 20 21 |
# File 'lib/ferrum/browser/process.rb', line 19 def browser_version @browser_version end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
19 20 21 |
# File 'lib/ferrum/browser/process.rb', line 19 def command @command end |
#default_user_agent ⇒ Object (readonly)
Returns the value of attribute default_user_agent.
19 20 21 |
# File 'lib/ferrum/browser/process.rb', line 19 def default_user_agent @default_user_agent end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
19 20 21 |
# File 'lib/ferrum/browser/process.rb', line 19 def host @host end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
19 20 21 |
# File 'lib/ferrum/browser/process.rb', line 19 def pid @pid end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
19 20 21 |
# File 'lib/ferrum/browser/process.rb', line 19 def port @port end |
#protocol_version ⇒ Object (readonly)
Returns the value of attribute protocol_version.
19 20 21 |
# File 'lib/ferrum/browser/process.rb', line 19 def protocol_version @protocol_version end |
#v8_version ⇒ Object (readonly)
Returns the value of attribute v8_version.
19 20 21 |
# File 'lib/ferrum/browser/process.rb', line 19 def v8_version @v8_version end |
#webkit_version ⇒ Object (readonly)
Returns the value of attribute webkit_version.
19 20 21 |
# File 'lib/ferrum/browser/process.rb', line 19 def webkit_version @webkit_version end |
#ws_url ⇒ Object
Returns the value of attribute ws_url.
19 20 21 |
# File 'lib/ferrum/browser/process.rb', line 19 def ws_url @ws_url end |
#xvfb ⇒ Object (readonly)
Returns the value of attribute xvfb.
19 20 21 |
# File 'lib/ferrum/browser/process.rb', line 19 def xvfb @xvfb end |
Class Method Details
.directory_remover(path) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/ferrum/browser/process.rb', line 52 def self.directory_remover(path) proc { begin FileUtils.remove_entry(path) rescue StandardError Errno::ENOENT end } end |
.process_killer(pid) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ferrum/browser/process.rb', line 30 def self.process_killer(pid) proc do if Utils::Platform.windows? # Process.kill is unreliable on Windows ::Process.kill("KILL", pid) unless system("taskkill /f /t /pid #{pid} >NUL 2>NUL") else ::Process.kill("USR1", pid) start = Utils::ElapsedTime.monotonic_time while ::Process.wait(pid, ::Process::WNOHANG).nil? sleep(WAIT_KILLED) next unless Utils::ElapsedTime.timeout?(start, KILL_TIMEOUT) ::Process.kill("KILL", pid) ::Process.wait(pid) break end end rescue Errno::ESRCH, Errno::ECHILD # nop end end |
.start(*args) ⇒ Object
26 27 28 |
# File 'lib/ferrum/browser/process.rb', line 26 def self.start(*args) new(*args).tap(&:start) end |
Instance Method Details
#inspect ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/ferrum/browser/process.rb', line 129 def inspect "#<#{self.class} " \ "@user_data_dir=#{@user_data_dir.inspect} " \ "@command=#<#{@command.class}:#{@command.object_id}> " \ "@default_user_agent=#{@default_user_agent.inspect} " \ "@ws_url=#{@ws_url.inspect} " \ "@v8_version=#{@v8_version.inspect} " \ "@browser_version=#{@browser_version.inspect} " \ "@webkit_version=#{@webkit_version.inspect}>" end |
#restart ⇒ Object
124 125 126 127 |
# File 'lib/ferrum/browser/process.rb', line 124 def restart stop start end |
#start ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ferrum/browser/process.rb', line 87 def start # Don't do anything as browser is already running as external process. return if ws_url begin read_io, write_io = IO.pipe = { in: File::NULL } [:pgroup] = true unless Utils::Platform.windows? [:out] = [:err] = write_io if @command.xvfb? @xvfb = Xvfb.start(@command.) ObjectSpace.define_finalizer(self, self.class.process_killer(@xvfb.pid)) end env = Hash(@xvfb&.to_env).merge(@env) @pid = ::Process.spawn(env, *@command.to_a, ) ObjectSpace.define_finalizer(self, self.class.process_killer(@pid)) parse_ws_url(read_io, @process_timeout) parse_json_version(ws_url) ensure close_io(read_io, write_io) end end |
#stop ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/ferrum/browser/process.rb', line 113 def stop if @pid kill(@pid) kill(@xvfb.pid) if @xvfb&.pid @pid = nil end remove_user_data_dir if @user_data_dir ObjectSpace.undefine_finalizer(self) end |