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
75 76 77 |
# File 'lib/rdig/crawler.rb', line 75 def add_to_index(doc) @indexer << doc if doc.needs_indexing? 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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rdig/crawler.rb', line 83 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 doc = filterchain.apply(doc) if doc @documents << doc @logger.debug "url #{url} survived filterchain" end rescue nil end |
#crawl ⇒ Object
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 |
# File 'lib/rdig/crawler.rb', line 19 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] @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) while (doc = @documents.pop) != :exit process_document doc, filterchain end } } # 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 end |
#process_document(doc, filterchain) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rdig/crawler.rb', line 52 def process_document(doc, filterchain) @logger.debug "processing document #{doc}" 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.debug "Trace: #{$!.backtrace.join("\n")}" end |