Class: FeedProcessor::Fetcher
- Inherits:
-
Object
- Object
- FeedProcessor::Fetcher
- Defined in:
- lib/feed_processor/fetcher.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(options = {}) ⇒ Fetcher
constructor
A new instance of Fetcher.
- #requests ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Fetcher
Returns a new instance of Fetcher.
11 12 13 14 15 |
# File 'lib/feed_processor/fetcher.rb', line 11 def initialize(={}) = @request_generator = [:request_generator] || FeedProcessor::FileBasedRequestGenerator.new setup_mongo([:mongo]) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/feed_processor/fetcher.rb', line 9 def end |
Instance Method Details
#execute ⇒ Object
21 22 23 24 25 26 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 |
# File 'lib/feed_processor/fetcher.rb', line 21 def execute number_of_threads = [:threads] || 16 main_queue = Beanstalk::Pool.new(['localhost:11300']) number_of_requests = requests.length main_queue.put("START #{number_of_requests}") slice_size = number_of_requests / number_of_threads slice_size = 1 if slice_size < 1 puts "Each slice has #{slice_size} urls (#{number_of_requests} requests / #{number_of_threads} threads)" requests.threadify(:each_slice, slice_size) do |slice| queue = Beanstalk::Pool.new(['localhost:11300']) HttpReactor::Client.new(slice) do |response, context| begin request = context.get_attribute('http_target_request') puts "#{response.code}:#{request.uri}" Response.create({ :url => request.uri, :data => FeedProcessor::Util.decode_content(response), :status => response.code }) if response.code == 200 queue.put(request.uri.to_s) end rescue Exception => e puts "Exception in handler: #{e.message}" end end end main_queue.put("END #{number_of_requests}") puts "Fetched #{number_of_requests} feeds" end |
#requests ⇒ Object
17 18 19 |
# File 'lib/feed_processor/fetcher.rb', line 17 def requests @request_generator.requests end |