Sha256: 982d6b06f341ab3b2385b9b84e845295986a1d10b25ff3664c82eb2ad4b3e8d5

Contents?: true

Size: 675 Bytes

Versions: 5

Compression:

Stored size: 675 Bytes

Contents

# frozen_string_literal: true

module Postdoc
  # Spins up a Chrome process.
  class ChromeProcess
    attr_reader :pid, :port

    def initialize(port: Random.rand(65_535 - 1024), **_options)
      @port = port
      @pid = Process.spawn "chrome --remote-debugging-port=#{port} --headless",
          out: File::NULL, err: File::NULL
    end

    def alive?
      @alive ||= test_socket!
    rescue Errno::ECONNREFUSED
      false
    end

    def kill
      Process.kill 'INT', pid
      Process.wait pid
    rescue
      true
    end

    def client
      Client.new port
    end

    private

    def test_socket!
      TCPSocket.new('localhost', port)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
postdoc-0.4.2 lib/postdoc/chrome_process.rb
postdoc-0.4.1 lib/postdoc/chrome_process.rb
postdoc-0.4.0 lib/postdoc/chrome_process.rb
postdoc-0.3.9 lib/postdoc/chrome_process.rb
postdoc-0.3.8 lib/postdoc/chrome_process.rb