Class: Redwood::PollManager
- Includes:
- Singleton
- Defined in:
- lib/sup/poll.rb
Instance Method Summary collapse
- #clear_running_totals ⇒ Object
- #do_poll ⇒ Object
- #handle_idle_update(sender, idle_since) ⇒ Object
- #handle_unidle_update(sender, idle_since) ⇒ Object
-
#initialize ⇒ PollManager
constructor
A new instance of PollManager.
- #poll ⇒ Object
-
#poll_from(source, opts = {}) ⇒ Object
like Source#poll, but yields successive Message objects, which have their labels and locations set correctly.
- #poll_unusual ⇒ Object
- #poll_with_sources ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Methods included from Singleton
Constructor Details
#initialize ⇒ PollManager
Returns a new instance of PollManager.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sup/poll.rb', line 33 def initialize @delay = $config[:poll_interval] || 300 @mutex = Mutex.new @thread = nil @last_poll = nil @polling = Mutex.new @poll_sources = nil @mode = nil @should_clear_running_totals = false clear_running_totals # defines @running_totals UpdateManager.register self end |
Instance Method Details
#clear_running_totals ⇒ Object
224 |
# File 'lib/sup/poll.rb', line 224 def clear_running_totals; @running_totals = {:num => 0, :numi => 0, :loaded_labels => Set.new}; end |
#do_poll ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 |
# File 'lib/sup/poll.rb', line 117 def do_poll total_num = total_numi = 0 from_and_subj = [] from_and_subj_inbox = [] loaded_labels = Set.new @mutex.synchronize do @poll_sources.each do |source| begin yield "Loading from #{source}... " rescue SourceError => e warn "problem getting messages from #{source}: #{e.}" next end num = 0 numi = 0 poll_from source do |action,m,old_m,progress| if action == :delete yield "Deleting #{m.id}" elsif action == :add if old_m new_locations = (m.locations - old_m.locations) if not new_locations.empty? yield "Message at #{new_locations[0].info} is an update of an old message. Updating labels from #{old_m.labels.to_a * ','} => #{m.labels.to_a * ','}" else yield "Skipping already-imported message at #{m.locations[-1].info}" end else yield "Found new message at #{m.source_info} with labels #{m.labels.to_a * ','}" loaded_labels.merge m.labels num += 1 from_and_subj << [m.from && m.from.longname, m.subj] if (m.labels & [:inbox, :spam, :deleted, :killed]) == Set.new([:inbox]) from_and_subj_inbox << [m.from && m.from.longname, m.subj] numi += 1 end end else fail end end yield "Found #{num} messages, #{numi} to inbox." unless num == 0 total_num += num total_numi += numi end loaded_labels = loaded_labels - LabelManager::HIDDEN_RESERVED_LABELS - [:inbox, :killed] yield "Done polling; loaded #{total_num} new messages total" @last_poll = Time.now end [total_num, total_numi, from_and_subj, from_and_subj_inbox, loaded_labels] end |
#handle_idle_update(sender, idle_since) ⇒ Object
222 |
# File 'lib/sup/poll.rb', line 222 def handle_idle_update sender, idle_since; @should_clear_running_totals = false; end |
#handle_unidle_update(sender, idle_since) ⇒ Object
223 |
# File 'lib/sup/poll.rb', line 223 def handle_unidle_update sender, idle_since; @should_clear_running_totals = true; clear_running_totals; end |
#poll ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sup/poll.rb', line 79 def poll if @polling.try_lock @poll_sources = SourceManager.usual_sources num, numi = poll_with_sources @polling.unlock [num, numi] else debug "poll already in progress." return end end |
#poll_from(source, opts = {}) ⇒ Object
like Source#poll, but yields successive Message objects, which have their labels and locations set correctly. The Messages are saved to or removed from the index after being yielded.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/sup/poll.rb', line 173 def poll_from source, opts={} debug "trying to acquire poll lock for: #{source}.." if source.poll_lock.try_lock debug "lock acquired for: #{source}." begin source.poll do |sym, args| case sym when :add m = Message.build_from_source source, args[:info] old_m = Index. m.id m.labels += args[:labels] m.labels.delete :inbox if source.archived? m.labels.delete :unread if source.read? m.labels.delete :unread if m.source_marked_read? # preserve read status if possible m.labels.each { |l| LabelManager << l } m.labels = old_m.labels + (m.labels - [:unread, :inbox]) if old_m m.locations = old_m.locations + m.locations if old_m HookManager.run "before-add-message", :message => m yield :add, m, old_m, args[:progress] if block_given? Index. m, true ## We need to add or unhide the message when it either did not exist ## before at all or when it was updated. We do *not* add/unhide when ## the same message was found at a different location if !old_m or not old_m.locations.member? m.location UpdateManager.relay self, :added, m end when :delete Index. :location => [source.id, args[:info]] do |m| m.locations.delete Location.new(source, args[:info]) yield :delete, m, [source,args[:info]], args[:progress] if block_given? Index. m, false #UpdateManager.relay self, :deleted, m end end end rescue SourceError => e warn "problem getting messages from #{source}: #{e.}" ensure source.go_idle source.poll_lock.unlock end else debug "source #{source} is already being polled." end end |
#poll_unusual ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/sup/poll.rb', line 91 def poll_unusual if @polling.try_lock @poll_sources = SourceManager.unusual_sources num, numi = poll_with_sources @polling.unlock [num, numi] else debug "poll_unusual already in progress." return end end |
#poll_with_sources ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/sup/poll.rb', line 46 def poll_with_sources @mode ||= PollMode.new if HookManager.enabled? "before-poll" HookManager.run("before-poll") else BufferManager.flash "Polling for new messages..." end num, numi, from_and_subj, from_and_subj_inbox, loaded_labels = @mode.poll clear_running_totals if @should_clear_running_totals @running_totals[:num] += num @running_totals[:numi] += numi @running_totals[:loaded_labels] += loaded_labels || [] if HookManager.enabled? "after-poll" hook_args = { :num => num, :num_inbox => numi, :num_total => @running_totals[:num], :num_inbox_total => @running_totals[:numi], :from_and_subj => from_and_subj, :from_and_subj_inbox => from_and_subj_inbox, :num_inbox_total_unread => lambda { Index.num_results_for :labels => [:inbox, :unread] } } HookManager.run("after-poll", hook_args) else if @running_totals[:num] > 0 BufferManager.flash "Loaded #{@running_totals[:num].pluralize 'new message'}, #{@running_totals[:numi]} to inbox. Labels: #{@running_totals[:loaded_labels].map{|l| l.to_s}.join(', ')}" else BufferManager.flash "No new messages." end end end |
#start ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/sup/poll.rb', line 103 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 |
#stop ⇒ Object
112 113 114 115 |
# File 'lib/sup/poll.rb', line 112 def stop @thread.kill if @thread @thread = nil end |