Class: TRMNLP::BrowserPool

Inherits:
Object
  • Object
show all
Defined in:
lib/trmnlp/browser_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(driver_factory:, max_size: 2) ⇒ BrowserPool

Returns a new instance of BrowserPool.



5
6
7
8
9
10
11
12
13
14
# File 'lib/trmnlp/browser_pool.rb', line 5

def initialize(driver_factory:, max_size: 2)
  @driver_factory = driver_factory
  @max_size = max_size
  @drivers = []
  @available = Queue.new
  @mutex = Mutex.new
  @shutdown = false

  at_exit { shutdown }
end

Instance Method Details

#shutdownObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/trmnlp/browser_pool.rb', line 23

def shutdown
  @mutex.synchronize do
    return if @shutdown

    @shutdown = true
    @drivers.each do |driver|
      driver.quit
    rescue StandardError
      nil
    end
    @drivers.clear
  end
end

#with_driverObject



16
17
18
19
20
21
# File 'lib/trmnlp/browser_pool.rb', line 16

def with_driver
  driver = checkout
  yield driver
ensure
  checkin(driver) if driver
end