class FootTraffic::Session

Public Class Methods

new(opts) click to toggle source
# File lib/foot_traffic/session.rb, line 24
def initialize(opts)
  opts[:headless] ||= false
  @browser ||= Ferrum::Browser.new(opts)
end
start(options: {}, duration: nil, clones: 1, quit: false, &block) click to toggle source
# File lib/foot_traffic/session.rb, line 20
def self.start(options: {}, duration: nil, clones: 1, quit: false, &block)
  new(options).start(duration: duration, clones: clones, quit: quit, &block)
end

Public Instance Methods

start(duration: nil, clones: 1, quit: false, &block) click to toggle source
# File lib/foot_traffic/session.rb, line 29
def start(duration: nil, clones: 1, quit: false, &block)
  main = Thread.new {
    threads = []
    clones.times do
      threads << Thread.new {
        window = @browser.contexts.create
        block.call(window, ThreadPool.new)
      }
    end
    threads.map(&:join)
  }

  # A sleeping thread to keep Ferrum open for a given period of time
  unless quit
    wait = Thread.new {
      duration.nil? ? sleep : sleep(duration)
    }
    wait.join
  end

  main.join
  @browser.quit
rescue ThreadError, RuntimeError, Errno::EMFILE, Errno::ECONNRESET
  raise ResourceOverloadError
end