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