From c0bf316c7f4a693c8382c63f59c8923b38e57548 Mon Sep 17 00:00:00 2001 From: wmorgan Date: Fri, 5 Jan 2007 23:04:08 +0000 Subject: [PATCH] error handling improvements. messageformaterrors are non-fatal and will be skipped; source errors are fatal and will cause the source to be skipped. git-svn-id: svn://rubyforge.org/var/svn/sup/trunk@196 5c8cc53c-5e98-4d25-b20a-d8db53a31250 --- lib/sup/poll.rb | 91 ++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 43 deletions(-) diff --git a/lib/sup/poll.rb b/lib/sup/poll.rb index b1dc6a7..0abe1d3 100644 --- a/lib/sup/poll.rb +++ b/lib/sup/poll.rb @@ -8,16 +8,14 @@ class PollManager DELAY = 300 def initialize - @polling = false + @mutex = Mutex.new @last_poll = nil self.class.i_am_the_instance self end def buffer - BufferManager.spawn_unless_exists("", :hidden => true) do - PollMode.new - end + BufferManager.spawn_unless_exists("", :hidden => true) { PollMode.new } end def poll @@ -40,58 +38,65 @@ class PollManager end end + ## TODO: merge this with sup-import def do_poll - return [0, 0] if @polling - @polling = true - found = {} - total_num = 0 - total_numi = 0 + total_num = total_numi = 0 + @mutex.synchronize do + found = {} + Index.usual_sources.each do |source| + next if source.broken? || source.done? - Index.usual_sources.each do |source| - next if source.done? - yield "Loading from #{source}... " + yield "Loading from #{source}... " + start_offset = nil + num = 0 + num_inbox = 0 - start_offset = nil - num = 0 - num_inbox = 0 - begin source.each do |offset, labels| + break if source.broken? start_offset ||= offset yield "Found message at #{offset} with labels #{labels * ', '}" - m = Redwood::Message.new :source => source, :source_info => offset, - :labels => labels - if found[m.id] - yield "Skipping duplicate message #{m.id}" - next - else + + begin + begin + m = Redwood::Message.new :source => source, :source_info => offset, :labels => labels + rescue MessageFormatError => e + yield "Non-fatal error loading message #{source}##{offset}: #{e.message}" + next + end + + if found[m.id] + yield "Skipping duplicate message #{m.id}" + next + end found[m.id] = true - end - if Index.add_message m - UpdateManager.relay :add, m - num += 1 - total_num += 1 - total_numi += 1 if m.labels.include? :inbox - end - end + if Index.add_message m + UpdateManager.relay :add, m + num += 1 + total_num += 1 + total_numi += 1 if m.labels.include? :inbox + end - if num % 1000 == 0 && num > 0 - elapsed = Time.now - start - pctdone = (offset.to_f - start_offset) / (source.total.to_f - start_offset) - remaining = (source.end_offset.to_f - offset.to_f) * (elapsed.to_f / (offset.to_f - start_offset)) - yield "## #{num} (#{(pctdone * 100.0)}% done) read; #{elapsed.to_time_s} elapsed; est. #{remaining.to_time_s} remaining" + if num % 1000 == 0 && num > 0 + elapsed = Time.now - start + pctdone = source.pct_done + remaining = (100.0 - pctdone) * (elapsed.to_f / pctdone) + yield "## #{num} (#{pctdone}% done) read; #{elapsed.to_time_s} elapsed; est. #{remaining.to_time_s} remaining" + end + rescue SourceError => e + msg = "Fatal error loading from #{source}: #{e.message}" + Redwood::log msg + yield msg + break + end end - rescue SourceError, MessageFormatError => e - msg = "#{source.broken? ? 'Fatal' : 'Non-fatal'} error loading from #{source}: #{e.message}" - Redwood::log msg - yield msg + yield "Found #{num} messages" unless num == 0 end - yield "Found #{num} messages" unless num == 0 + yield "Done polling; loaded #{total_num} new messages total" + @last_poll = Time.now + @polling = false end - yield "Done polling; loaded #{total_num} new messages total" - @last_poll = Time.now - @polling = false [total_num, total_numi] end end -- 2.45.2