Class: Ferrum::Browser::Process

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ferrum/browser/process.rb,
lib/ferrum/browser/process/killer.rb,
lib/ferrum/browser/process/output.rb,
sig/ferrum/browser/process.rbs,
sig/ferrum/browser/process/killer.rbs,
sig/ferrum/browser/process/output.rbs

Overview

Spawns and manages the lifecycle of the browser OS process: builds the launch Command, starts it (optionally under Xvfb for headful mode), waits for the CDP WebSocket endpoint to become available, and handles stopping/restarting the process and cleaning up its user data directory.

Defined Under Namespace

Modules: Killer Classes: Output

Constant Summary collapse

HTTP_SCHEMES =

Returns:

  • (::Hash[String, String])
{ "ws" => "http", "wss" => "https" }.freeze
WS_SCHEMES =

Returns:

  • (::Hash[String, String])
{ "http" => "ws", "https" => "wss" }.freeze
LOCAL_HOSTS =

Returns:

  • (::Array[String])
%w[0.0.0.0 :: 127.0.0.1 ::1 localhost].freeze
WS_URL =

Returns:

  • (Regexp)
%r{DevTools listening on (ws://.*[a-zA-Z0-9-]{36})}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Process

Returns a new instance of Process.

Parameters:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ferrum/browser/process.rb', line 54

def initialize(options)
  @pid = @xvfb = @user_data_dir = nil
  @requested_host = options.host
  @log = options.log
  @output = Output.new(@log)

  if options.ws_url || options.url
    self.ws_url = options.ws_url || fetch_ws_url(options.url, options.process_timeout, options.ssl_context)
    return
  end

  @process_timeout = options.process_timeout
  @env = Hash(options.env)

  @user_data_dir = Dir.mktmpdir("ferrum_user_data_dir_")
  @command = Command.build(options, @user_data_dir)
end

Instance Attribute Details

#command ⇒ Browser::Command? (readonly)

Returns the value of attribute command.

Returns:



46
47
48
# File 'lib/ferrum/browser/process.rb', line 46

def command
  @command
end

#host ⇒ String? (readonly)

Returns the value of attribute host.

Returns:

  • (String, nil)


46
47
48
# File 'lib/ferrum/browser/process.rb', line 46

def host
  @host
end

#output ⇒ Output (readonly)

What the browser process wrote to stdout and stderr, empty for a browser Ferrum didn't launch.

Returns:



52
53
54
# File 'lib/ferrum/browser/process.rb', line 52

def output
  @output
end

#pid ⇒ ::Integer? (readonly)

Returns the value of attribute pid.

Returns:

  • (::Integer, nil)


46
47
48
# File 'lib/ferrum/browser/process.rb', line 46

def pid
  @pid
end

#port ⇒ ::Integer? (readonly)

Returns the value of attribute port.

Returns:

  • (::Integer, nil)


46
47
48
# File 'lib/ferrum/browser/process.rb', line 46

def port
  @port
end

#ws_url ⇒ Addressable::URI?, ...

Returns the value of attribute ws_url.

Returns:

  • (Addressable::URI?, String, nil)


46
47
48
# File 'lib/ferrum/browser/process.rb', line 46

def ws_url
  @ws_url
end

#xvfb ⇒ Browser::Xvfb? (readonly)

Returns the value of attribute xvfb.

Returns:



46
47
48
# File 'lib/ferrum/browser/process.rb', line 46

def xvfb
  @xvfb
end

Class Method Details

.start(*args) ⇒ Process

Builds and starts a new browser process.

Parameters:

Returns:



42
43
44
# File 'lib/ferrum/browser/process.rb', line 42

def self.start(*args)
  new(*args).tap(&:start)
end

Instance Method Details

#inspect ⇒ String

Custom inspection that omits noisy internal command details.

Returns:

  • (String)


137
138
139
140
141
142
# File 'lib/ferrum/browser/process.rb', line 137

def inspect
  "#<#{self.class} " \
    "@user_data_dir=#{@user_data_dir.inspect} " \
    "@command=#<#{@command.class}:#{@command.object_id}> " \
    "@ws_url=#{@ws_url.inspect}>"
end

#path ⇒ String

Returns:

  • (String)


51
# File 'sig/ferrum/browser/process.rbs', line 51

def path: () -> String

#restart ⇒ void

This method returns an undefined value.

Stops and starts the browser process again.



127
128
129
130
# File 'lib/ferrum/browser/process.rb', line 127

def restart
  stop
  start
end

#start ⇒ void

This method returns an undefined value.

Spawns the browser process and waits for it to become reachable over CDP.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ferrum/browser/process.rb', line 77

def start
  # Don't do anything as browser is already running as external process.
  return if ws_url

  begin
    @xvfb = Xvfb.start(@command.options) if @command.xvfb?
    @output = Output.new(@log)
    @output.pipe { |io| spawn_browser(io) }
    parse_ws_url
    @output.follow
  ensure
    @output.close unless ws_url
  end
end

#stop(wait: true, grace: 0) ⇒ Thread?

Kills the browser process (and Xvfb, if running) and removes the user data directory.

Parameters:

  • wait (Boolean) (defaults to: true) —

    Whether to block until the process is confirmed dead and its user data directory removed (the default), or return immediately and run that cleanup on a background thread instead. Killing a stubborn process group can block for up to Ferrum::Browser::Process::Killer::KILL_TIMEOUT seconds, plus retries removing its directory, which matters when quitting many browsers in a hot path. wait: false is not used by #restart, which always waits so the old process is fully gone before the new one starts.

  • grace (Numeric) (defaults to: 0) —

    Seconds to let the browser exit on its own before it's signaled, e.g. after Browser.close. See Ferrum::Browser::Process::Killer.kill.

  • wait: (Boolean) (defaults to: true)
  • grace: (::Numeric) (defaults to: 0)

Returns:

  • (Thread, nil) —

    The background cleanup thread when wait: false; the caller can #join it if they need cleanup to have finished, e.g. before process exit or before reusing a fixed port. nil when wait: true.



116
117
118
119
120
# File 'lib/ferrum/browser/process.rb', line 116

def stop(wait: true, grace: 0)
  return sync_stop(grace) if wait

  async_stop(grace)
end