Class: Postdoc::ChromeProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/postdoc/chrome_process.rb

Overview

Spins up a Chrome process.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port: Random.rand(1025..65535), **_options) ⇒ ChromeProcess

Returns a new instance of ChromeProcess.



8
9
10
11
12
# File 'lib/postdoc/chrome_process.rb', line 8

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

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



6
7
8
# File 'lib/postdoc/chrome_process.rb', line 6

def pid
  @pid
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/postdoc/chrome_process.rb', line 6

def port
  @port
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
# File 'lib/postdoc/chrome_process.rb', line 14

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

#clientObject



27
28
29
# File 'lib/postdoc/chrome_process.rb', line 27

def client
  Client.new port
end

#killObject



20
21
22
23
24
25
# File 'lib/postdoc/chrome_process.rb', line 20

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