Class: FootTraffic::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/foot_traffic/session.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Session

Returns a new instance of Session.



24
25
26
27
# File 'lib/foot_traffic/session.rb', line 24

def initialize(opts)
  opts[:headless] ||= false
  @browser ||= Ferrum::Browser.new(opts)
end

Class Method Details

.start(options: {}, duration: nil, clones: 1, quit: false, &block) ⇒ Object



20
21
22
# 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

Instance Method Details

#start(duration: nil, clones: 1, quit: false, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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