123456789_123456789_123456789_123456789_123456789_

Class: Selenium::WebDriver::ServiceManager Private

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: rb/lib/selenium/webdriver/common/service_manager.rb

Overview

Base class implementing default behavior of service_manager object, responsible for starting and stopping driver implementations.

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(config) ⇒ ServiceManager

End users should use a class method for the desired driver, rather than using this directly.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 39

def initialize(config)
  @executable_path = config.executable_path
  @host = Platform.localhost
  @port = config.port
  @io = config.log
  @extra_args = config.args
  @shutdown_supported = config.shutdown_supported

  raise Error::WebDriverError, "invalid port: #{@port}" if @port < 1
end

Instance Attribute Details

#process_exited?Boolean (readonly, private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 124

def process_exited?
  @process.nil? || @process.exited?
end

#process_running?Boolean (readonly, private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 120

def process_running?
  defined?(@process) && @process&.alive?
end

Instance Method Details

#build_process(*command) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 80

def build_process(*command)
  WebDriver.logger.debug("Executing Process #{command}", id: :driver_service)
  @process = ChildProcess.build(*command)
  @io ||= WebDriver.logger.io if WebDriver.logger.debug?
  @process.io = @io if @io

  @process
end

#cannot_connect_error_text (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 135

def cannot_connect_error_text
  "unable to connect to #{@executable_path} #{@host}:#{@port}"
end

#connect_to_server (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 89

def connect_to_server
  Net::HTTP.start(@host, @port) do |http|
    http.open_timeout = STOP_TIMEOUT / 2
    http.read_timeout = STOP_TIMEOUT / 2

    yield http
  end
end

#connect_until_stable (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 128

def connect_until_stable
  socket_poller = SocketPoller.new @host, @port, START_TIMEOUT
  return if socket_poller.connected?

  raise Error::WebDriverError, cannot_connect_error_text
end

#find_free_port (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 98

def find_free_port
  @port = PortProber.above(@port)
end

#socket_lock (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 139

def socket_lock
  @socket_lock ||= SocketLock.new(@port - 1, SOCKET_LOCK_TIMEOUT)
end

#start

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 50

def start
  raise "already started: #{uri.inspect} #{@executable_path.inspect}" if process_running?

  Platform.exit_hook { stop } # make sure we don't leave the server running

  socket_lock.locked do
    find_free_port
    start_process
    connect_until_stable
  end
end

#start_process (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 102

def start_process
  @process = build_process(@executable_path, "--port=#{@port}", *@extra_args)
  @process.start
end

#stop

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 62

def stop
  return unless @shutdown_supported
  return if process_exited?

  stop_server
  @process.poll_for_exit STOP_TIMEOUT
rescue ChildProcess::TimeoutError, Errno::ECONNREFUSED
  nil # noop
ensure
  stop_process
end

#stop_process (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 107

def stop_process
  return if process_exited?

  @process.stop STOP_TIMEOUT
end

#stop_server (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 113

def stop_server
  connect_to_server do |http|
    headers = WebDriver::Remote::Http::Common::DEFAULT_HEADERS.dup
    http.get('/shutdown', headers)
  end
end

#uri

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/service_manager.rb', line 74

def uri
  @uri ||= URI.parse("http://#{@host}:#{@port}")
end