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
- PROCESS_TIMEOUT =
ENV.fetch("FERRUM_PROCESS_TIMEOUT", 2).to_i
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.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options) ⇒ Process
constructor
A new instance of Process.
- #restart ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(options) ⇒ Process
Returns a new instance of Process.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ferrum/browser/process.rb', line 57 def initialize() if [:url] url = URI.join([:url].to_s, "/json/version") response = JSON.parse(::Net::HTTP.get(url)) set_ws_url(response["webSocketDebuggerUrl"]) parse_browser_versions return end @logger = [:logger] @process_timeout = .fetch(:process_timeout, PROCESS_TIMEOUT) tmpdir = Dir.mktmpdir ObjectSpace.define_finalizer(self, self.class.directory_remover(tmpdir)) @command = Command.build(, tmpdir) end |
Instance Attribute Details
#browser_version ⇒ Object (readonly)
Returns the value of attribute browser_version.
20 21 22 |
# File 'lib/ferrum/browser/process.rb', line 20 def browser_version @browser_version end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
20 21 22 |
# File 'lib/ferrum/browser/process.rb', line 20 def command @command end |
#default_user_agent ⇒ Object (readonly)
Returns the value of attribute default_user_agent.
20 21 22 |
# File 'lib/ferrum/browser/process.rb', line 20 def default_user_agent @default_user_agent end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
20 21 22 |
# File 'lib/ferrum/browser/process.rb', line 20 def host @host end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
20 21 22 |
# File 'lib/ferrum/browser/process.rb', line 20 def pid @pid end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
20 21 22 |
# File 'lib/ferrum/browser/process.rb', line 20 def port @port end |
#protocol_version ⇒ Object (readonly)
Returns the value of attribute protocol_version.
20 21 22 |
# File 'lib/ferrum/browser/process.rb', line 20 def protocol_version @protocol_version end |
#v8_version ⇒ Object (readonly)
Returns the value of attribute v8_version.
20 21 22 |
# File 'lib/ferrum/browser/process.rb', line 20 def v8_version @v8_version end |
#webkit_version ⇒ Object (readonly)
Returns the value of attribute webkit_version.
20 21 22 |
# File 'lib/ferrum/browser/process.rb', line 20 def webkit_version @webkit_version end |
#ws_url ⇒ Object (readonly)
Returns the value of attribute ws_url.
20 21 22 |
# File 'lib/ferrum/browser/process.rb', line 20 def ws_url @ws_url end |
Class Method Details
.directory_remover(path) ⇒ Object
53 54 55 |
# File 'lib/ferrum/browser/process.rb', line 53 def self.directory_remover(path) proc { FileUtils.remove_entry(path) rescue Errno::ENOENT } end |
.process_killer(pid) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ferrum/browser/process.rb', line 32 def self.process_killer(pid) proc do begin if Ferrum.windows? ::Process.kill("KILL", pid) else ::Process.kill("USR1", pid) start = Ferrum.monotonic_time while ::Process.wait(pid, ::Process::WNOHANG).nil? sleep(WAIT_KILLED) next unless Ferrum.timeout?(start, KILL_TIMEOUT) ::Process.kill("KILL", pid) ::Process.wait(pid) break end end rescue Errno::ESRCH, Errno::ECHILD end end end |
.start(*args) ⇒ Object
28 29 30 |
# File 'lib/ferrum/browser/process.rb', line 28 def self.start(*args) new(*args).tap(&:start) end |
Instance Method Details
#restart ⇒ Object
100 101 102 103 |
# File 'lib/ferrum/browser/process.rb', line 100 def restart stop start end |
#start ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ferrum/browser/process.rb', line 74 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 Ferrum.windows? [:out] = [:err] = write_io @pid = ::Process.spawn(*@command.to_a, ) ObjectSpace.define_finalizer(self, self.class.process_killer(@pid)) parse_ws_url(read_io, @process_timeout) parse_browser_versions ensure close_io(read_io, write_io) end end |
#stop ⇒ Object
94 95 96 97 98 |
# File 'lib/ferrum/browser/process.rb', line 94 def stop kill if @pid remove_user_data_dir if @user_data_dir ObjectSpace.undefine_finalizer(self) end |