]> git.cworth.org Git - sup/blob - lib/sup/poll.rb
better source error reporting, and better message loading
[sup] / lib / sup / poll.rb
1 require 'thread'
2
3 module Redwood
4
5 class PollManager
6   include Singleton
7
8   HookManager.register "before-add-message", <<EOS
9 Executes immediately before a message is added to the index.
10 Variables:
11   message: the new message
12 EOS
13
14   HookManager.register "before-poll", <<EOS
15 Executes immediately before a poll for new messages commences.
16 No variables.
17 EOS
18
19   HookManager.register "after-poll", <<EOS
20 Executes immediately after a poll for new messages completes.
21 Variables:
22                   num: the total number of new messages
23             num_inbox: the number of new messages appearing in the inbox (i.e.
24                        not auto-archived).
25         from_and_subj: an array of (from email address, subject) pairs
26   from_and_subj_inbox: an array of (from email address, subject) pairs for
27                        only those messages appearing in the inbox
28 EOS
29
30   DELAY = 300
31
32   def initialize
33     @mutex = Mutex.new
34     @thread = nil
35     @last_poll = nil
36     @polling = false
37     
38     self.class.i_am_the_instance self
39   end
40
41   def buffer
42     BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
43   end
44
45   def poll
46     return if @polling
47     @polling = true
48     HookManager.run "before-poll"
49
50     BufferManager.flash "Polling for new messages..."
51     num, numi, from_and_subj, from_and_subj_inbox = buffer.mode.poll
52     if num > 0
53       BufferManager.flash "Loaded #{num.pluralize 'new message'}, #{numi} to inbox." 
54     else
55       BufferManager.flash "No new messages." 
56     end
57
58     HookManager.run "after-poll", :num => num, :num_inbox => numi, :from_and_subj => from_and_subj, :from_and_subj_inbox => from_and_subj_inbox
59
60     @polling = false
61     [num, numi]
62   end
63
64   def start
65     @thread = Redwood::reporting_thread do
66       while true
67         sleep DELAY / 2
68         poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
69       end
70     end
71   end
72
73   def stop
74     @thread.kill if @thread
75     @thread = nil
76   end
77
78   def do_poll
79     total_num = total_numi = 0
80     from_and_subj = []
81     from_and_subj_inbox = []
82
83     @mutex.synchronize do
84       Index.usual_sources.each do |source|
85 #        yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
86         begin
87           yield "Loading from #{source}... " unless source.done? || source.has_errors?
88         rescue SourceError => e
89           Redwood::log "problem getting messages from #{source}: #{e.message}"
90           Redwood::report_broken_sources :force_to_top => true
91           next
92         end
93
94         num = 0
95         numi = 0
96         add_messages_from source do |m, offset, entry|
97           ## always preserve the labels on disk.
98           m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
99           yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
100           unless entry
101             num += 1
102             from_and_subj << [m.from.longname, m.subj]
103             if m.has_label?(:inbox) && ([:spam, :deleted, :killed] & m.labels).empty?
104               from_and_subj_inbox << [m.from.longname, m.subj]
105               numi += 1 
106             end
107           end
108           m
109         end
110         yield "Found #{num} messages, #{numi} to inbox." unless num == 0
111         total_num += num
112         total_numi += numi
113       end
114
115       yield "Done polling; loaded #{total_num} new messages total"
116       @last_poll = Time.now
117       @polling = false
118     end
119     [total_num, total_numi, from_and_subj, from_and_subj_inbox]
120   end
121
122   ## this is the main mechanism for adding new messages to the
123   ## index. it's called both by sup-sync and by PollMode.
124   ##
125   ## for each message in the source, starting from the source's
126   ## starting offset, this methods yields the message, the source
127   ## offset, and the index entry on disk (if any). it expects the
128   ## yield to return the message (possibly altered in some way), and
129   ## then adds it (if new) or updates it (if previously seen).
130   ##
131   ## the labels of the yielded message are the default source
132   ## labels. it is likely that callers will want to replace these with
133   ## the index labels, if they exist, so that state is not lost when
134   ## e.g. a new version of a message from a mailing list comes in.
135   def add_messages_from source
136     begin
137       return if source.done? || source.has_errors?
138       
139       source.each do |offset, labels|
140         if source.has_errors?
141           Redwood::log "error loading messages from #{source}: #{source.error.message}"
142           return
143         end
144       
145         labels.each { |l| LabelManager << l }
146         labels = labels + (source.archived? ? [] : [:inbox])
147
148         begin
149           m = Message.new :source => source, :source_info => offset, :labels => labels
150           if m.source_marked_read?
151             m.remove_label :unread
152             labels.delete :unread
153           end
154
155           docid, entry = Index.load_entry_for_id m.id
156           HookManager.run "before-add-message", :message => m
157           m = yield(m, offset, entry) or next
158           Index.sync_message m, docid, entry
159           UpdateManager.relay self, :add, m unless entry
160         rescue MessageFormatError => e
161           Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
162         end
163       end
164     rescue SourceError => e
165       Redwood::log "problem getting messages from #{source}: #{e.message}"
166       Redwood::report_broken_sources :force_to_top => true
167     end
168   end
169 end
170
171 end