lib/sup/poll.rb in sup-0.0.1 vs lib/sup/poll.rb in sup-0.0.2

- old
+ new

@@ -11,41 +11,55 @@ @polling = false @last_poll = nil self.class.i_am_the_instance self - ::Thread.new do + Redwood::reporting_thread do while true sleep DELAY / 2 - if @last_poll.nil? || (Time.now - @last_poll) >= DELAY - mbid = BufferManager.say "Polling for new messages..." - num, numi = poll { |s| BufferManager.say s, mbid } - BufferManager.clear mbid - BufferManager.flash "Loaded #{num} new messages, #{numi} to inbox." if num > 0 - end + poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY end end end + def buffer + BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) do + PollMode.new + end + end + def poll + BufferManager.flash "Polling for new messages..." + num, numi = buffer.mode.poll + if num > 0 + BufferManager.flash "Loaded #{num} new messages, #{numi} to inbox." + else + BufferManager.flash "No new messages." + end + [num, numi] + end + + def do_poll return [0, 0] if @polling @polling = true found = {} total_num = 0 total_numi = 0 + Index.usual_sources.each do |source| next if source.done? yield "Loading from #{source}... " start_offset = nil num = 0 num_inbox = 0 source.each do |offset, labels| start_offset ||= offset - + yield " Found message at #{offset} with labels #{labels * ', '}" begin - m = Redwood::Message.new source, offset, labels + m = Redwood::Message.new :source => source, :source_info => offset, + :labels => labels if found[m.id] yield "Skipping duplicate message #{m.id}" next else found[m.id] = true @@ -62,10 +76,10 @@ 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.total.to_f - offset.to_f) * (elapsed.to_f / (offset.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" end end yield "Found #{num} messages" unless num == 0 end