:default => 300, :validate => Proc.new{|v| v > 30},\r
:desc => "How many seconds to sleep before checking RSS feeds again")\r
\r
- @@watchThreads = Hash.new\r
- @@mutex = Mutex.new\r
-\r
def initialize\r
super\r
- kill_threads\r
if @registry.has_key?(:feeds)\r
@feeds = @registry[:feeds]\r
@feeds.keys.grep(/[A-Z]/) { |k|\r
else\r
@feeds = Hash.new\r
end\r
+ @watch = Hash.new\r
rewatch_rss\r
end\r
\r
end\r
\r
def cleanup\r
- kill_threads\r
+ stop_watches\r
end\r
\r
def save\r
@registry[:feeds] = @feeds\r
end\r
\r
- def kill_threads\r
- @@mutex.synchronize {\r
- # Abort all running threads.\r
- @@watchThreads.each { |url, thread|\r
- debug "Killing thread for #{url}"\r
- thread.kill\r
- }\r
- @@watchThreads = Hash.new\r
+ def stop_watch(handle)\r
+ if @watch.has_key?(handle)\r
+ begin\r
+ debug "Stopping watch #{handle}"\r
+ @bot.timer.remove(@watch[handle])\r
+ @watch.delete(handle)\r
+ rescue => e\r
+ report_problem("Failed to stop watch for #{handle}", e, nil)\r
+ end\r
+ end\r
+ end\r
+\r
+ def stop_watches\r
+ @watch.each_key { |k|\r
+ stop_watch(k)\r
}\r
end\r
\r
\r
m.reply "lemme fetch it..."\r
title = items = nil\r
- @@mutex.synchronize {\r
- title, items = fetchRss(feed, m)\r
- }\r
+ title, items = fetchRss(feed, m)\r
return unless items\r
\r
# We sort the feeds in freshness order (newer ones first)\r
def list_rss(m, params)\r
wanted = params[:handle]\r
reply = String.new\r
- @@mutex.synchronize {\r
- @feeds.each { |handle, feed|\r
- next if wanted and !handle.match(/#{wanted}/i)\r
- reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})"\r
- (reply << " (watched)") if feed.watched_by?(m.replyto)\r
- reply << "\n"\r
- }\r
+ @feeds.each { |handle, feed|\r
+ next if wanted and !handle.match(/#{wanted}/i)\r
+ reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})"\r
+ (reply << " (watched)") if feed.watched_by?(m.replyto)\r
+ reply << "\n"\r
}\r
if reply.empty?\r
reply = "no feeds found"\r
def watched_rss(m, params)\r
wanted = params[:handle]\r
reply = String.new\r
- @@mutex.synchronize {\r
- watchlist.each { |handle, feed|\r
- next if wanted and !handle.match(/#{wanted}/i)\r
- next unless feed.watched_by?(m.replyto)\r
- reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})\n"\r
- }\r
+ watchlist.each { |handle, feed|\r
+ next if wanted and !handle.match(/#{wanted}/i)\r
+ next unless feed.watched_by?(m.replyto)\r
+ reply << "#{feed.handle}: #{feed.url} (in format: #{feed.type ? feed.type : 'default'})\n"\r
}\r
if reply.empty?\r
reply = "no watched feeds"\r
m.reply "You must specify both a handle and an url to add an RSS feed"\r
return\r
end\r
- @@mutex.synchronize {\r
- @feeds[handle.downcase] = RssBlob.new(url,handle,type)\r
- }\r
+ @feeds[handle.downcase] = RssBlob.new(url,handle,type)\r
reply = "Added RSS #{url} named #{handle}"\r
if type\r
reply << " (format: #{type})"\r
m.reply "someone else is watching #{feed.handle}, I won't remove it from my list"\r
return\r
end\r
- @@mutex.synchronize {\r
- @feeds.delete(feed.handle.downcase)\r
- }\r
+ @feeds.delete(feed.handle.downcase)\r
m.okay unless pass\r
return\r
end\r
if url\r
add_rss(m, params)\r
end\r
- feed = nil\r
- @@mutex.synchronize {\r
- feed = @feeds.fetch(handle.downcase, nil)\r
- }\r
+ feed = @feeds.fetch(handle.downcase, nil)\r
if feed\r
- @@mutex.synchronize {\r
- if feed.add_watch(m.replyto)\r
- watchRss(feed, m)\r
- m.okay\r
- else\r
- m.reply "Already watching #{feed.handle}"\r
- end\r
- }\r
+ if feed.add_watch(m.replyto)\r
+ watchRss(feed, m)\r
+ m.okay\r
+ else\r
+ m.reply "Already watching #{feed.handle}"\r
+ end\r
else\r
m.reply "Couldn't watch feed #{handle} (no such feed found)"\r
end\r
m.reply("#{m.replyto} wasn't watching #{feed.handle}") unless pass\r
end\r
if !feed.watched?\r
- @@mutex.synchronize {\r
- if @@watchThreads[handle].kind_of? Thread\r
- @@watchThreads[handle].kill\r
- debug "rmwatch: Killed thread for #{handle}"\r
- @@watchThreads.delete(handle)\r
- end\r
- }\r
+ stop_watch(handle)\r
end\r
return feed\r
end\r
\r
def rewatch_rss(m=nil, params=nil)\r
- kill_threads\r
+ stop_watches\r
\r
# Read watches from list.\r
watchlist.each{ |handle, feed|\r
\r
private\r
def watchRss(feed, m=nil)\r
- if @@watchThreads.has_key?(feed.handle)\r
+ if @watch.has_key?(feed.handle)\r
report_problem("watcher thread for #{feed.handle} is already running", nil, m)\r
return\r
end\r
- @@watchThreads[feed.handle] = Thread.new do\r
+ status = Hash.new\r
+ status[:oldItems] = []\r
+ status[:firstRun] = true\r
+ status[:failures] = 0\r
+ @watch[feed.handle] = @bot.timer.add(@bot.config['rss.thread_sleep'], status) {\r
debug "watcher for #{feed} started"\r
- oldItems = []\r
- firstRun = true\r
- failures = 0\r
- loop do\r
- begin\r
- debug "fetching #{feed}"\r
- title = newItems = nil\r
- @@mutex.synchronize {\r
- title, newItems = fetchRss(feed)\r
- }\r
- unless newItems\r
- debug "no items in feed #{feed}"\r
- failures +=1\r
+ oldItems = status[:oldItems]\r
+ firstRun = status[:firstRun]\r
+ failures = status[:failures]\r
+ begin\r
+ debug "fetching #{feed}"\r
+ title = newItems = nil\r
+ title, newItems = fetchRss(feed)\r
+ unless newItems\r
+ debug "no items in feed #{feed}"\r
+ failures +=1\r
+ else\r
+ debug "Checking if new items are available for #{feed}"\r
+ if firstRun\r
+ debug "First run, we'll see next time"\r
+ firstRun = false\r
else\r
- debug "Checking if new items are available for #{feed}"\r
- if firstRun\r
- debug "First run, we'll see next time"\r
- firstRun = false\r
- else\r
- otxt = oldItems.map { |item| item.to_s }\r
- dispItems = newItems.reject { |item|\r
- otxt.include?(item.to_s)\r
+ otxt = oldItems.map { |item| item.to_s }\r
+ dispItems = newItems.reject { |item|\r
+ otxt.include?(item.to_s)\r
+ }\r
+ if dispItems.length > 0\r
+ debug "Found #{dispItems.length} new items in #{feed}"\r
+ # When displaying watched feeds, publish them from older to newer\r
+ dispItems.reverse.each { |item|\r
+ printFormattedRss(feed, item)\r
}\r
- if dispItems.length > 0\r
- debug "Found #{dispItems.length} new items in #{feed}"\r
- # When displaying watched feeds, publish them from older to newer\r
- dispItems.reverse.each { |item|\r
- @@mutex.synchronize {\r
- printFormattedRss(feed, item)\r
- }\r
- }\r
- else\r
- debug "No new items found in #{feed}"\r
- end\r
+ else\r
+ debug "No new items found in #{feed}"\r
end\r
- oldItems = newItems.dup\r
end\r
- rescue Exception => e\r
- error "Error watching #{feed}: #{e.inspect}"\r
- debug e.backtrace.join("\n")\r
- failures += 1\r
+ oldItems = newItems.dup\r
end\r
-\r
- seconds = @bot.config['rss.thread_sleep'] * (failures + 1)\r
- seconds += seconds * (rand(100)-50)/100\r
- debug "watcher for #{feed} going to sleep #{seconds} seconds.."\r
- sleep seconds\r
+ rescue Exception => e\r
+ error "Error watching #{feed}: #{e.inspect}"\r
+ debug e.backtrace.join("\n")\r
+ failures += 1\r
end\r
- end\r
+\r
+ status[:oldItems] = oldItems\r
+ status[:firstRun] = firstRun\r
+ status[:failures] = failures\r
+\r
+ seconds = @bot.config['rss.thread_sleep'] * (failures + 1)\r
+ seconds += seconds * (rand(100)-50)/100\r
+ debug "watcher for #{feed} going to sleep #{seconds} seconds.."\r
+ @bot.timer.reschedule(@watch[feed.handle], seconds)\r
+ }\r
end\r
\r
def printFormattedRss(feed, item, opts=nil)\r
def fetchRss(feed, m=nil)\r
begin\r
# Use 60 sec timeout, cause the default is too low\r
- # Do not use get_cached for RSS until we have proper cache handling\r
- # xml = @bot.httputil.get_cached(feed.url,60,60)\r
xml = @bot.httputil.get_cached(feed.url, 60, 60)\r
rescue URI::InvalidURIError, URI::BadURIError => e\r
report_problem("invalid rss feed #{feed.url}", e, m)\r