\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
@title = nil\r
@items = nil\r
@mutex = Mutex.new\r
+ @last_fetched = lf\r
sanitize_watchers(watchers)\r
end\r
\r
@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
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
\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
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