Class: ProxyTester::Actions::FetchUrls
- Inherits:
-
Object
- Object
- ProxyTester::Actions::FetchUrls
- Defined in:
- lib/proxy_tester/actions/fetch_urls.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ FetchUrls
constructor
A new instance of FetchUrls.
- #run ⇒ Object
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( = {}) @urls = .fetch(:urls).collect { |u| Addressable::URI.heuristic_parse(u).to_s } @proxy = .fetch(:proxy) @timeout = .fetch(:timeout).to_i @count = .fetch(:count).to_i @concurrent = .fetch(:concurrent) ProxyTester.ui_logger.debug "Options: " + .to_s @output = .fetch(:output) @reporter = .fetch(:reporter, Reporters::FetchUrls.new(output)) @result = [] end |
Instance Method Details
#run ⇒ Object
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 |