2020-10-27 00:50:13 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
#
|
2017-04-28 04:03:05 +08:00
|
|
|
# Copyright (C) 2011 - present Instructure, Inc.
|
2011-02-01 09:57:29 +08:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
def self.process
|
|
|
|
ExternalFeedAggregator.new.process
|
|
|
|
end
|
2015-05-11 21:31:29 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def initialize
|
2013-03-08 08:08:47 +08:00
|
|
|
@logger = Rails.logger
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
2015-05-11 21:31:29 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def process
|
2020-10-06 06:42:27 +08:00
|
|
|
GuardRail.activate(:secondary) do
|
2014-10-27 23:23:09 +08:00
|
|
|
start = Time.now.utc
|
2015-05-11 21:31:29 +08:00
|
|
|
loop do
|
2014-11-20 04:02:06 +08:00
|
|
|
feeds = ExternalFeed.to_be_polled(start).limit(1000).preload(context: :root_account).to_a
|
2015-05-11 21:31:29 +08:00
|
|
|
break if feeds.empty?
|
|
|
|
|
2014-10-27 23:23:09 +08:00
|
|
|
feeds.each do |feed|
|
2020-10-06 06:42:27 +08:00
|
|
|
GuardRail.activate(:primary) do
|
2018-02-14 07:00:55 +08:00
|
|
|
if feed.inactive?
|
|
|
|
feed.update_attribute(:refresh_at, inactive_wait_seconds.seconds.from_now)
|
2014-11-20 04:02:06 +08:00
|
|
|
next
|
|
|
|
end
|
2014-10-23 05:36:12 +08:00
|
|
|
process_feed(feed)
|
|
|
|
end
|
2014-08-26 10:14:22 +08:00
|
|
|
end
|
2015-05-11 21:31:29 +08:00
|
|
|
end
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
|
|
|
end
|
2015-05-11 21:31:29 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
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)
|
2016-03-15 23:21:05 +08:00
|
|
|
feed.title = atom.title.to_s
|
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
|
2019-10-10 22:37:11 +08:00
|
|
|
LiveEvents.set_context(Canvas::LiveEvents.amended_context(feed.context))
|
2011-02-01 09:57:29 +08:00
|
|
|
@logger.info("feed found: #{feed.url}")
|
|
|
|
@logger.info('requesting entries')
|
|
|
|
require 'net/http'
|
2015-05-11 21:31:29 +08:00
|
|
|
|
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
|
2016-01-14 22:42:51 +08:00
|
|
|
feed.update_attribute(:refresh_at, success_wait_seconds.seconds.from_now)
|
2011-02-01 09:57:29 +08:00
|
|
|
else
|
2015-04-28 00:55:43 +08:00
|
|
|
@logger.info("request failed #{response.class}")
|
2015-05-11 21:31:29 +08:00
|
|
|
handle_failure(feed)
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
2017-10-14 05:29:11 +08:00
|
|
|
rescue CanvasHttp::Error,
|
|
|
|
CanvasHttp::RelativeUriError,
|
|
|
|
CanvasHttp::InsecureUriError,
|
|
|
|
Timeout::Error,
|
|
|
|
SocketError,
|
|
|
|
SystemCallError => e
|
|
|
|
|
2015-05-11 21:31:29 +08:00
|
|
|
@logger.info("request error: #{e}")
|
|
|
|
handle_failure(feed)
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
|
|
|
end
|
2015-05-11 21:31:29 +08:00
|
|
|
|
|
|
|
def handle_failure(feed)
|
|
|
|
feed.increment(:failures)
|
|
|
|
feed.increment(:consecutive_failures)
|
2016-01-14 22:42:51 +08:00
|
|
|
feed.update_attribute(:refresh_at, failure_wait_seconds.seconds.from_now)
|
2015-05-11 21:31:29 +08:00
|
|
|
end
|
|
|
|
|
2018-02-14 07:00:55 +08:00
|
|
|
def inactive_wait_seconds
|
|
|
|
Setting.get('external_feed_success_wait_seconds', 48.hours.to_s).to_f
|
|
|
|
end
|
|
|
|
|
2015-05-11 21:31:29 +08:00
|
|
|
def success_wait_seconds
|
|
|
|
Setting.get('external_feed_success_wait_seconds', 2.hours.to_s).to_f
|
|
|
|
end
|
|
|
|
|
|
|
|
def failure_wait_seconds
|
|
|
|
Setting.get('external_feed_failure_wait_seconds', 30.minutes.to_s).to_f
|
|
|
|
end
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|