]> git.cworth.org Git - sup/blob - lib/sup/poll.rb
labels now fully determined by sources.yaml, and lots of improvements to sup-config
[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         begin
47           yield "Loading from #{source}... " unless source.done? || source.has_errors?
48         rescue SourceError => e
49           Redwood::log "problem getting messages from #{source}: #{e.message}"
50           Redwood::report_broken_sources
51           next
52         end
53
54         num = 0
55         numi = 0
56         add_messages_from source do |m, offset, entry|
57           ## always preserve the labels on disk.
58           m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
59           yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
60           unless entry
61             num += 1
62             numi += 1 if m.labels.include? :inbox
63           end
64           m
65         end
66         yield "Found #{num} messages, #{numi} to inbox" unless num == 0
67         total_num += num
68         total_numi += numi
69       end
70
71       yield "Done polling; loaded #{total_num} new messages total"
72       @last_poll = Time.now
73       @polling = false
74     end
75     [total_num, total_numi]
76   end
77
78   ## this is the main mechanism for adding new messages to the
79   ## index. it's called both by sup-sync and by PollMode.
80   ##
81   ## for each message in the source, starting from the source's
82   ## starting offset, this methods yields the message, the source
83   ## offset, and the index entry on disk (if any). it expects the
84   ## yield to return the message (possibly altered in some way), and
85   ## then adds it (if new) or updates it (if previously seen).
86   ##
87   ## the labels of the yielded message are the default source
88   ## labels. it is likely that callers will want to replace these with
89   ## the index labels, if they exist, so that state is not lost when
90   ## e.g. a new version of a message from a mailing list comes in.
91   def add_messages_from source
92     begin
93       return if source.done? || source.has_errors?
94       
95       source.each do |offset, labels|
96         if source.has_errors?
97           Redwood::log "error loading messages from #{source}: #{source.error.message}"
98           return
99         end
100       
101         labels.each { |l| LabelManager << l }
102         labels += [:inbox] unless source.archived?
103
104         begin
105           m = Message.new :source => source, :source_info => offset, :labels => labels
106           if m.source_marked_read?
107             m.remove_label :unread
108             labels.delete :unread
109           else
110             m.add_label :unread
111             labels << :unread
112           end
113
114           docid, entry = Index.load_entry_for_id m.id
115           m = yield(m, offset, entry) or next
116           Index.sync_message m, docid, entry
117           UpdateManager.relay self, :add, m unless entry
118         rescue MessageFormatError => e
119           Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
120         end
121       end
122     rescue SourceError => e
123       Redwood::log "problem getting messages from #{source}: #{e.message}"
124       Redwood::report_broken_sources
125     end
126   end
127 end
128
129 end