Class: FeedProcessor::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/feed_processor/fetcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options={})
  @options = options
  @request_generator = options[:request_generator] || FeedProcessor::FileBasedRequestGenerator.new
  setup_mongo(options[:mongo])
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/feed_processor/fetcher.rb', line 9

def options
  @options
end

Instance Method Details

#executeObject



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 = options[: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

#requestsObject



17
18
19
# File 'lib/feed_processor/fetcher.rb', line 17

def requests
  @request_generator.requests
end