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