2011-02-01 09:57:29 +08:00
|
|
|
#
|
|
|
|
# Copyright (C) 2011 Instructure, Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Canvas.
|
|
|
|
#
|
|
|
|
# Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
2015-04-09 01:21:08 +08:00
|
|
|
require 'atom'
|
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
class ExternalFeedAggregator
|
|
|
|
SUCCESS_WAIT_SECONDS = 1.hour # time to refresh on a successful feed load with new entries
|
|
|
|
NO_ENTRIES_WAIT_SECONDS = 2.hours # time to refresh on a successful feed load with NO new entries
|
|
|
|
FAILURE_WAIT_SECONDS = 30.minutes # time to refresh on a failed feed load
|
|
|
|
|
|
|
|
def self.process
|
|
|
|
ExternalFeedAggregator.new.process
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
2013-03-08 08:08:47 +08:00
|
|
|
@logger = Rails.logger
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def process
|
2014-08-26 10:14:22 +08:00
|
|
|
Shackles.activate(:slave) do
|
2014-10-27 23:23:09 +08:00
|
|
|
start = Time.now.utc
|
|
|
|
begin
|
2014-11-20 04:02:06 +08:00
|
|
|
feeds = ExternalFeed.to_be_polled(start).limit(1000).preload(context: :root_account).to_a
|
2014-10-27 23:23:09 +08:00
|
|
|
feeds.each do |feed|
|
2014-10-23 05:36:12 +08:00
|
|
|
Shackles.activate(:master) do
|
2014-11-20 04:02:06 +08:00
|
|
|
if !feed.context || feed.context.root_account.deleted?
|
|
|
|
feed.update_attribute(:refresh_at, Time.now.utc + NO_ENTRIES_WAIT_SECONDS)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2014-10-23 05:36:12 +08:00
|
|
|
process_feed(feed)
|
|
|
|
end
|
2014-08-26 10:14:22 +08:00
|
|
|
end
|
2014-10-27 23:23:09 +08:00
|
|
|
end while (!feeds.empty?)
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_entries(feed, body)
|
2014-12-17 04:45:12 +08:00
|
|
|
begin
|
|
|
|
require 'rss/1.0'
|
|
|
|
require 'rss/2.0'
|
|
|
|
rss = RSS::Parser.parse(body, false)
|
|
|
|
raise "Invalid rss feed" unless rss
|
|
|
|
feed.title = rss.channel.title
|
|
|
|
feed.save
|
|
|
|
@logger.info("#{rss.items.length} rss items found")
|
|
|
|
entries = feed.add_rss_entries(rss)
|
|
|
|
@logger.info("#{entries.length} new entries added")
|
|
|
|
return true
|
|
|
|
rescue
|
2011-02-01 09:57:29 +08:00
|
|
|
begin
|
2014-12-17 04:45:12 +08:00
|
|
|
require 'atom'
|
|
|
|
atom = Atom::Feed.load_feed(body)
|
|
|
|
feed.title = atom.title
|
2011-05-14 00:49:23 +08:00
|
|
|
feed.save
|
2014-12-17 04:45:12 +08:00
|
|
|
@logger.info("#{atom.entries.length} atom entries found")
|
|
|
|
entries = feed.add_atom_entries(atom)
|
2011-02-01 09:57:29 +08:00
|
|
|
@logger.info("#{entries.length} new entries added")
|
|
|
|
return true
|
2014-12-17 04:45:12 +08:00
|
|
|
rescue
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_feed(feed)
|
|
|
|
begin
|
|
|
|
@logger.info("feed found: #{feed.url}")
|
|
|
|
@logger.info('requesting entries')
|
|
|
|
require 'net/http'
|
2014-12-27 07:51:49 +08:00
|
|
|
response = CanvasHttp.get(feed.url)
|
2011-02-01 09:57:29 +08:00
|
|
|
case response
|
|
|
|
when Net::HTTPSuccess
|
|
|
|
success = parse_entries(feed, response.body)
|
|
|
|
@logger.info(success ? 'successful response' : '200 with no data returned')
|
|
|
|
feed.consecutive_failures = 0 if success
|
|
|
|
feed.update_attribute(:refresh_at, Time.now.utc + ((!@entries || @entries.empty?) ? NO_ENTRIES_WAIT_SECONDS : SUCCESS_WAIT_SECONDS))
|
|
|
|
else
|
|
|
|
@logger.info("request failed #{response.class.to_s}")
|
|
|
|
feed.increment(:consecutive_failures)
|
|
|
|
feed.increment(:failures)
|
|
|
|
feed.update_attribute(:refresh_at, Time.now.utc + (FAILURE_WAIT_SECONDS))
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
feed.increment(:consecutive_failures)
|
|
|
|
feed.increment(:failures)
|
|
|
|
feed.update_attribute(:refresh_at, Time.now.utc + (FAILURE_WAIT_SECONDS))
|
2015-04-05 10:39:49 +08:00
|
|
|
Canvas::Errors.capture(e, {
|
|
|
|
message: "External Feed aggregation failed",
|
|
|
|
feed_url: feed.url,
|
|
|
|
feed_id: feed.id,
|
|
|
|
user_id: feed.user_id,
|
2011-02-01 09:57:29 +08:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|