Class: ProxyTester::Actions::FetchUrls

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy_tester/actions/fetch_urls.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FetchUrls

Returns a new instance of FetchUrls.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/proxy_tester/actions/fetch_urls.rb', line 12

def initialize(options = {})
  @urls   = options.fetch(:urls).collect { |u| Addressable::URI.heuristic_parse(u).to_s }
  @proxy = options.fetch(:proxy)
  @timeout  = options.fetch(:timeout).to_i
  @count = options.fetch(:count).to_i
  @concurrent = options.fetch(:concurrent)
  
  ProxyTester.ui_logger.debug "Options: " + options.to_s

  @output   =  options.fetch(:output)
  @reporter = options.fetch(:reporter, Reporters::FetchUrls.new(output))

  @result = []
end

Instance Method Details

#runObject



27
28
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
54
55
56
57
58
59
60
61
# File 'lib/proxy_tester/actions/fetch_urls.rb', line 27

def run
  threads = []

  reporter.header

  urls.each do |u|
    count.times do |n|
      threads << Thread.new do
        ProxyTester.ui_logger.info "Starting #{n + 1}-nth thread to fetch \"#{u}\" via \"#{proxy.to_string}\"."

        begin 
          fetch_url(u)
        end while concurrent
      end
    end
  end


  sleep_time = 0
  while sleep_time < timeout and threads.any? { |t| t.alive? }
    sleep 1
    sleep_time += 1
  end

  if sleep_time < timeout
    ProxyTester.ui_logger.info "All threads finished before waiting time \"#{timeout}\" was over."
  else
    ProxyTester.ui_logger.info "Waiting time #{timeout} is over. Killing all active threads..."
  end

  threads.each do |t|
    Thread.kill(t)
  end

end