Class: Bidi2pdf::ChromedriverManager

Inherits:
Object
  • Object
show all
Includes:
Chromedriver::Binary::Platform
Defined in:
lib/bidi2pdf/chromedriver_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port: 0, headless: true, chrome_args: Bidi::Session::DEFAULT_CHROME_ARGS) ⇒ ChromedriverManager

Returns a new instance of ChromedriverManager.



13
14
15
16
17
18
19
20
# File 'lib/bidi2pdf/chromedriver_manager.rb', line 13

def initialize(port: 0, headless: true, chrome_args: Bidi::Session::DEFAULT_CHROME_ARGS)
  @port = port
  @headless = headless
  @session = nil
  @started = false
  @chrome_args = chrome_args
  @shutdown_mutex ||= Mutex.new
end

Instance Attribute Details

#chrome_argsObject (readonly)

Returns the value of attribute chrome_args.



10
11
12
# File 'lib/bidi2pdf/chromedriver_manager.rb', line 10

def chrome_args
  @chrome_args
end

#headlessObject (readonly)

Returns the value of attribute headless.



10
11
12
# File 'lib/bidi2pdf/chromedriver_manager.rb', line 10

def headless
  @headless
end

#pidObject (readonly)

Returns the value of attribute pid.



10
11
12
# File 'lib/bidi2pdf/chromedriver_manager.rb', line 10

def pid
  @pid
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/bidi2pdf/chromedriver_manager.rb', line 10

def port
  @port
end

#reader_threadObject

Returns the value of attribute reader_thread.



11
12
13
# File 'lib/bidi2pdf/chromedriver_manager.rb', line 11

def reader_thread
  @reader_thread
end

#shutdown_mutexObject (readonly)

Returns the value of attribute shutdown_mutex.



10
11
12
# File 'lib/bidi2pdf/chromedriver_manager.rb', line 10

def shutdown_mutex
  @shutdown_mutex
end

#startedObject (readonly)

Returns the value of attribute started.



10
11
12
# File 'lib/bidi2pdf/chromedriver_manager.rb', line 10

def started
  @started
end

Instance Method Details

#sessionObject



41
42
43
44
45
# File 'lib/bidi2pdf/chromedriver_manager.rb', line 41

def session
  return unless @started

  @session ||= Bidi::Session.new(session_url: session_url, headless: @headless, chrome_args: @chrome_args)
end

#session_urlObject



47
48
49
50
51
# File 'lib/bidi2pdf/chromedriver_manager.rb', line 47

def session_url
  return unless @started

  "http://localhost:#{@port}/session"
end

#startObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bidi2pdf/chromedriver_manager.rb', line 22

def start
  return @pid if @pid

  @started = true

  update_chromedriver
  cmd = build_cmd
  Bidi2pdf.logger.info "Starting Chromedriver with command: #{cmd}"

  spawn_process(cmd)

  Bidi2pdf.logger.info "Started Chromedriver on port #{@port}, PID #{@pid}"
  wait_until_chromedriver_ready

  at_exit { stop }

  @pid
end

#stop(timeout: 5) ⇒ Object

rubocop: disable Metrics/AbcSize



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bidi2pdf/chromedriver_manager.rb', line 54

def stop(timeout: 5)
  shutdown_mutex.synchronize do
    return unless @pid

    if reader_thread&.alive?
      begin
        reader_thread.kill
        reader_thread.join
      rescue StandardError => e
        Bidi2pdf.logger.error "Error killing reader thread: #{e.message}"
      end
    end

    @started = false

    close_session

    debug_show_all_children

    old_childprocesses = term_chromedriver

    detect_zombie_processes old_childprocesses

    return unless process_alive?

    kill_chromedriver timeout: timeout
  ensure
    @pid = nil
    @started = false
  end
end