Class: RDig::Crawler
- Inherits:
-
Object
- Object
- RDig::Crawler
- Defined in:
- lib/rdig/crawler.rb
Instance Method Summary collapse
- #add_to_index(doc) ⇒ Object
-
#add_url(url, filterchain, referring_document = nil) ⇒ Object
pipes a new document pointing to url through the filter chain, if it survives that, it gets added to the documents queue for further processing.
- #crawl ⇒ Object
-
#initialize(config = RDig.config, logger = RDig.logger) ⇒ Crawler
constructor
A new instance of Crawler.
- #process_document(doc, filterchain) ⇒ Object
- #run ⇒ Object
Constructor Details
Instance Method Details
#add_to_index(doc) ⇒ Object
78 79 80 81 82 |
# File 'lib/rdig/crawler.rb', line 78 def add_to_index(doc) if doc.needs_indexing? @indexer << doc end end |
#add_url(url, filterchain, referring_document = nil) ⇒ Object
pipes a new document pointing to url through the filter chain, if it survives that, it gets added to the documents queue for further processing
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rdig/crawler.rb', line 88 def add_url(url, filterchain, referring_document = nil) return if url.nil? || url.empty? @logger.debug "add_url #{url}" doc = if referring_document referring_document.create_child(url) else Document.create(url) end if doc = filterchain.apply(doc) @documents << doc @logger.debug "url #{url} survived filterchain" end rescue nil end |
#crawl ⇒ Object
18 19 20 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 |
# File 'lib/rdig/crawler.rb', line 18 def crawl raise 'no start urls given!' if @config.crawler.start_urls.empty? # check whether we are indexing on-disk or via http url_type = @config.crawler.start_urls.first =~ /^file:\/\// ? :file : :http chain_config = RDig.filter_chain[url_type] # the etag filter operates on the fetched document, thats why we cannot put it into the filter chain right now. @etag_filter = ETagFilter.new filterchain = UrlFilters::FilterChain.new(chain_config) @config.crawler.start_urls.each { |url| add_url(url, filterchain) } num_threads = @config.crawler.num_threads group = ThreadsWait.new num_threads.times { |i| group.join_nowait Thread.new("fetcher #{i}") { filterchain = UrlFilters::FilterChain.new(chain_config) @logger.info "thread #{i} running..." while (doc = @documents.pop) != :exit process_document doc, filterchain end @logger.info "thread #{i} is done." } } # check for an empty queue every now and then sleep_interval = @config.crawler.wait_before_leave begin sleep sleep_interval end until @documents.empty? # nothing to do any more, tell the threads to exit num_threads.times { @documents << :exit } @logger.info "waiting for threads to finish..." group.all_waits @logger.info "indexed #{@indexer.indexed_documents} documents" end |
#process_document(doc, filterchain) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rdig/crawler.rb', line 55 def process_document(doc, filterchain) @logger.info "processing document #{doc.uri}" doc.fetch case doc.status when :success if @etag_filter.apply(doc) # add links from this document to the queue doc.content[:links].each { |url| add_url(url, filterchain, doc) } unless doc.content[:links].nil? add_to_index doc end when :redirect @logger.debug "redirect to #{doc.content}" add_url(doc.content, filterchain, doc) else @logger.error "unknown doc status #{doc.status}: #{doc}" end rescue @logger.error "error processing document #{doc.uri.to_s}: #{$!}" @logger.info "Trace: #{$!.backtrace.join("\n")}" end |