]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
* (plugins/rss) maintain refresh timeouts through restarts/rescans
authorDmitry Kim <dmitry point kim at gmail point com>
Thu, 29 Nov 2007 21:33:50 +0000 (21:33 +0000)
committerDmitry Kim <dmitry point kim at gmail point com>
Thu, 29 Nov 2007 21:33:50 +0000 (21:33 +0000)
data/rbot/plugins/rss.rb

index ac47a04ce3abd703e8d5775f127bc1d5f6aa47c7..7f645c81b1dfd01044bc4bac80298b1ded35c50e 100644 (file)
@@ -162,17 +162,10 @@ end
 \r
 \r
 class ::RssBlob\r
-  attr_accessor :url\r
-  attr_accessor :handle\r
-  attr_accessor :type\r
-  attr :watchers\r
-  attr_accessor :refresh_rate\r
-  attr_accessor :xml\r
-  attr_accessor :title\r
-  attr_accessor :items\r
-  attr_accessor :mutex\r
-\r
-  def initialize(url,handle=nil,type=nil,watchers=[], xml=nil)\r
+  attr_accessor :url, :handle, :type, :refresh_rate, :xml, :title, :items,\r
+    :mutex, :watchers, :last_fetched\r
+\r
+  def initialize(url,handle=nil,type=nil,watchers=[], xml=nil, lf = nil)\r
     @url = url\r
     if handle\r
       @handle = handle\r
@@ -186,6 +179,7 @@ class ::RssBlob
     @title = nil\r
     @items = nil\r
     @mutex = Mutex.new\r
+    @last_fetched = lf\r
     sanitize_watchers(watchers)\r
   end\r
 \r
@@ -195,7 +189,8 @@ class ::RssBlob
                      @handle,\r
                      @type ? @type.dup : nil,\r
                      @watchers.dup,\r
-                     @xml ? @xml.dup : nil)\r
+                     @xml ? @xml.dup : nil,\r
+                     @last_fetched)\r
     end\r
   end\r
 \r
@@ -698,13 +693,18 @@ class RSSFeedsPlugin < Plugin
     end\r
     status = Hash.new\r
     status[:failures] = 0\r
-    status[:first_run] = true\r
-    @watch[feed.handle] = @bot.timer.add(0) {\r
-      debug "watcher for #{feed} started"\r
+    tmout = 0\r
+    if feed.last_fetched\r
+      tmout = feed.last_fetched + calculate_timeout(feed) - Time.now\r
+      tmout = 0 if tmout < 0\r
+    end\r
+    debug "scheduling a watcher for #{feed} in #{tmout} seconds"\r
+    @watch[feed.handle] = @bot.timer.add(tmout) {\r
+      debug "watcher for #{feed} wakes up"\r
       failures = status[:failures]\r
-      first_run = status.delete(:first_run)\r
       begin\r
         debug "fetching #{feed}"\r
+        first_run = !feed.last_fetched\r
         oldxml = feed.xml ? feed.xml.dup : nil\r
         unless fetchRss(feed)\r
           failures += 1\r
@@ -781,18 +781,25 @@ class RSSFeedsPlugin < Plugin
 \r
       status[:failures] = failures\r
 \r
-      timer = nil\r
+      seconds = calculate_timeout(feed, failures)\r
+      debug "watcher for #{feed} going to sleep #{seconds} seconds.."\r
+      begin\r
+        @bot.timer.reschedule(@watch[feed.handle], seconds)\r
+      rescue\r
+        warning "watcher for #{feed} failed to reschedule: #{$!.inspect}"\r
+      end\r
+    }\r
+    debug "watcher for #{feed} added"\r
+  end\r
+\r
+  def calculate_timeout(feed, failures = 0)\r
       seconds = @bot.config['rss.thread_sleep']\r
       feed.mutex.synchronize do\r
-        timer = @watch[feed.handle]\r
         seconds = feed.refresh_rate if feed.refresh_rate\r
       end\r
       seconds *= failures + 1\r
       seconds += seconds * (rand(100)-50)/100\r
-      debug "watcher for #{feed} going to sleep #{seconds} seconds.."\r
-      @bot.timer.reschedule(timer, seconds) rescue warning "watcher for #{feed} failed to reschedule: #{$!.inspect}"\r
-    }\r
-    debug "watcher for #{feed} added"\r
+      return seconds\r
   end\r
 \r
   def printFormattedRss(feed, item, opts=nil)\r
@@ -905,6 +912,7 @@ class RSSFeedsPlugin < Plugin
   end\r
 \r
   def fetchRss(feed, m=nil, cache=true)\r
+    feed.last_fetched = Time.now\r
     begin\r
       # Use 60 sec timeout, cause the default is too low\r
       xml = @bot.httputil.get(feed.url,\r