Class: TRMNLP::ScreenGenerator::BrowserPool
- Inherits:
-
Object
- Object
- TRMNLP::ScreenGenerator::BrowserPool
- Defined in:
- lib/trmnlp/screen_generator.rb
Overview
Browser pool management for efficient resource usage
Instance Method Summary collapse
-
#initialize(max_size: 2) ⇒ BrowserPool
constructor
A new instance of BrowserPool.
- #shutdown ⇒ Object
- #with_page ⇒ Object
Constructor Details
#initialize(max_size: 2) ⇒ BrowserPool
Returns a new instance of BrowserPool.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/trmnlp/screen_generator.rb', line 10 def initialize(max_size: 2) @browsers = [] @available = Queue.new @mutex = Mutex.new @max_size = max_size @shutdown = false # Register cleanup on exit at_exit { shutdown } end |
Instance Method Details
#shutdown ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/trmnlp/screen_generator.rb', line 36 def shutdown @mutex.synchronize do return if @shutdown @shutdown = true # Close all browsers @browsers.each do |browser| browser.close rescue nil end @browsers.clear end end |
#with_page ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/trmnlp/screen_generator.rb', line 21 def with_page browser = nil page = nil begin browser = checkout_browser page = browser.new_page yield page ensure # Clean up page but keep browser alive page&.close rescue nil checkin_browser(browser) if browser end end |