Class: Lightpanda::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/lightpanda/process.rb

Constant Summary collapse

READY_PATTERN =
/server running.*address=(\d+\.\d+\.\d+\.\d+:\d+)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Process

Returns a new instance of Process.



9
10
11
12
13
14
15
16
17
# File 'lib/lightpanda/process.rb', line 9

def initialize(options)
  @options = options
  @pid = nil
  @ws_url = nil
  @stdout_r = nil
  @stdout_w = nil
  @stderr_r = nil
  @stderr_w = nil
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



7
8
9
# File 'lib/lightpanda/process.rb', line 7

def pid
  @pid
end

#ws_urlObject (readonly)

Returns the value of attribute ws_url.



7
8
9
# File 'lib/lightpanda/process.rb', line 7

def ws_url
  @ws_url
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
# File 'lib/lightpanda/process.rb', line 49

def alive?
  return false unless @pid

  ::Process.kill(0, @pid)
  true
rescue Errno::ESRCH, Errno::EPERM
  false
end

#startObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lightpanda/process.rb', line 19

def start
  binary_path = @options.browser_path || Binary.find_or_download

  raise BinaryNotFoundError, "Lightpanda binary not found" unless binary_path

  @stdout_r, @stdout_w = IO.pipe
  @stderr_r, @stderr_w = IO.pipe

  @pid = spawn_process(binary_path)

  @stdout_w.close
  @stderr_w.close

  wait_for_ready
end

#stopObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lightpanda/process.rb', line 35

def stop
  return unless @pid

  begin
    ::Process.kill("TERM", @pid)
    ::Process.wait(@pid)
  rescue Errno::ESRCH, Errno::ECHILD
    # Process already dead
  end

  cleanup_pipes
  @pid = nil
end