Class: Redwood::PollManager

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/sup/poll.rb

Constant Summary collapse

DELAY =
300

Instance Method Summary collapse

Constructor Details

#initializePollManager

Returns a new instance of PollManager.



32
33
34
35
36
37
38
39
# File 'lib/sup/poll.rb', line 32

def initialize
  @mutex = Mutex.new
  @thread = nil
  @last_poll = nil
  @polling = false
  
  self.class.i_am_the_instance self
end

Instance Method Details

#add_messages_from(source) ⇒ Object

this is the main mechanism for adding new messages to the index. it’s called both by sup-sync and by PollMode.

for each message in the source, starting from the source’s starting offset, this methods yields the message, the source offset, and the index entry on disk (if any). it expects the yield to return the message (possibly altered in some way), and then adds it (if new) or updates it (if previously seen).

the labels of the yielded message are the default source labels. it is likely that callers will want to replace these with the index labels, if they exist, so that state is not lost when e.g. a new version of a message from a mailing list comes in.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/sup/poll.rb', line 136

def add_messages_from source
  begin
    return if source.done? || source.has_errors?
    
    source.each do |offset, labels|
      if source.has_errors?
        Redwood::log "error loading messages from #{source}: #{source.error.message}"
        return
      end
    
      labels.each { |l| LabelManager << l }
      labels = labels + (source.archived? ? [] : [:inbox])

      begin
        m = Message.new :source => source, :source_info => offset, :labels => labels
        if m.source_marked_read?
          m.remove_label :unread
          labels.delete :unread
        end

        docid, entry = Index.load_entry_for_id m.id
        HookManager.run "before-add-message", :message => m
        m = yield(m, offset, entry) or next
        Index.sync_message m, docid, entry
        UpdateManager.relay self, :add, m unless entry
      rescue MessageFormatError => e
        Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
      end
    end
  rescue SourceError => e
    Redwood::log "problem getting messages from #{source}: #{e.message}"
    Redwood::report_broken_sources :force_to_top => true
  end
end

#bufferObject



41
42
43
44
# File 'lib/sup/poll.rb', line 41

def buffer
  b, new = BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
  b
end

#do_pollObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sup/poll.rb', line 79

def do_poll
  total_num = total_numi = 0
  from_and_subj = []
  from_and_subj_inbox = []

  @mutex.synchronize do
    Index.usual_sources.each do |source|
#        yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
      begin
        yield "Loading from #{source}... " unless source.done? || source.has_errors?
      rescue SourceError => e
        Redwood::log "problem getting messages from #{source}: #{e.message}"
        Redwood::report_broken_sources :force_to_top => true
        next
      end

      num = 0
      numi = 0
      add_messages_from source do |m, offset, entry|
        ## always preserve the labels on disk.
        m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
        yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
        unless entry
          num += 1
          from_and_subj << [m.from.longname, m.subj]
          if m.has_label?(:inbox) && ([:spam, :deleted, :killed] & m.labels).empty?
            from_and_subj_inbox << [m.from.longname, m.subj]
            numi += 1 
          end
        end
        m
      end
      yield "Found #{num} messages, #{numi} to inbox." unless num == 0
      total_num += num
      total_numi += numi
    end

    yield "Done polling; loaded #{total_num} new messages total"
    @last_poll = Time.now
    @polling = false
  end
  [total_num, total_numi, from_and_subj, from_and_subj_inbox]
end

#pollObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sup/poll.rb', line 46

def poll
  return if @polling
  @polling = true
  HookManager.run "before-poll"

  BufferManager.flash "Polling for new messages..."
  num, numi, from_and_subj, from_and_subj_inbox = buffer.mode.poll
  if num > 0
    BufferManager.flash "Loaded #{num.pluralize 'new message'}, #{numi} to inbox." 
  else
    BufferManager.flash "No new messages." 
  end

  HookManager.run "after-poll", :num => num, :num_inbox => numi, :from_and_subj => from_and_subj, :from_and_subj_inbox => from_and_subj_inbox

  @polling = false
  [num, numi]
end

#startObject



65
66
67
68
69
70
71
72
# File 'lib/sup/poll.rb', line 65

def start
  @thread = Redwood::reporting_thread("periodic poll") do
    while true
      sleep DELAY / 2
      poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
    end
  end
end

#stopObject



74
75
76
77
# File 'lib/sup/poll.rb', line 74

def stop
  @thread.kill if @thread
  @thread = nil
end