]> git.cworth.org Git - sup/blob - lib/sup/poll.rb
moved sup-import to sup-sync and changed interface a lot. note that trollop 1.5 is...
[sup] / lib / sup / poll.rb
1 require 'thread'
2
3 module Redwood
4
5 class PollManager
6   include Singleton
7
8   DELAY = 300
9
10   def initialize
11     @mutex = Mutex.new
12     @last_poll = nil
13     
14     self.class.i_am_the_instance self
15   end
16
17   def buffer
18     BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
19   end
20
21   def poll
22     BufferManager.flash "Polling for new messages..."
23     num, numi = buffer.mode.poll
24     if num > 0
25       BufferManager.flash "Loaded #{num} new messages, #{numi} to inbox." 
26     else
27       BufferManager.flash "No new messages." 
28     end
29     [num, numi]
30   end
31
32   def start_thread
33     Redwood::reporting_thread do
34       while true
35         sleep DELAY / 2
36         poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
37       end
38     end
39   end
40
41   def do_poll
42     total_num = total_numi = 0
43     @mutex.synchronize do
44       Index.usual_sources.each do |source|
45 #        yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
46         yield "Loading from #{source}... " unless source.done? || source.broken?
47         num = 0
48         numi = 0
49         add_messages_from source do |m, offset, entry|
50           ## always preserve the labels on disk.
51           m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
52           yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
53           unless entry
54             num += 1
55             numi += 1 if m.labels.include? :inbox
56           end
57           m
58         end
59         yield "Found #{num} messages, #{numi} to inbox" unless num == 0
60         total_num += num
61         total_numi += numi
62       end
63
64       yield "Done polling; loaded #{total_num} new messages total"
65       @last_poll = Time.now
66       @polling = false
67     end
68     [total_num, total_numi]
69   end
70
71   ## this is the main mechanism for adding new messages to the
72   ## index. it's called both by sup-sync and by PollMode.
73   ##
74   ## for each message in the source, starting from the source's
75   ## starting offset, this methods yields the message, the source
76   ## offset, and the index entry on disk (if any). it expects the
77   ## yield to return the message (possibly altered in some way), and
78   ## then adds it (if new) or updates it (if previously seen).
79   ##
80   ## the labels of the yielded message are the default source
81   ## labels. it is likely that callers will want to replace these with
82   ## the index labels, if they exist, so that state is not lost when
83   ## e.g. a new version of a message from a mailing list comes in.
84   def add_messages_from source
85     return if source.done? || source.broken?
86
87     begin
88       source.each do |offset, labels|
89         if source.broken?
90           Redwood::log "error loading messages from #{source}: #{source.broken_msg}"
91           return
92         end
93       
94         labels.each { |l| LabelManager << l }
95         labels += [:inbox] unless source.archived?
96
97         begin
98           m = Message.new :source => source, :source_info => offset, :labels => labels
99           if m.source_marked_read?
100             m.remove_label :unread
101             labels.delete :unread
102           end
103
104           docid, entry = Index.load_entry_for_id m.id
105           m = yield(m, offset, entry) or next
106           Index.sync_message m, docid, entry
107           UpdateManager.relay self, :add, m unless entry
108         rescue MessageFormatError => e
109           Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
110         end
111       end
112     rescue SourceError => e
113       Redwood::log "problem getting messages from #{source}: #{e.message}"
114       Redwood::report_broken_sources
115     end
116   end
117 end
118
119 end