Skip to content

Commit

Permalink
Fixed fetch_not_modified in Feed class
Browse files Browse the repository at this point in the history
  • Loading branch information
martent committed Feb 27, 2017
1 parent e29040c commit f148d90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
7 changes: 5 additions & 2 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,23 @@ def refresh_entries
# The HTTP response code is set in @response_status
def fetch
err_msg = 'Flödet kunde inte hämtas. Kontrollera att adressen är korrekt.'
fetch_all = APP_CONFIG['feed_worker']['fetch_not_modified']
begin
faraday = Faraday.new do |connection|
connection.use FaradayMiddleware::FollowRedirects, limit: 5
connection.adapter :net_http
connection.options[:timeout] = 10
connection.ssl[:verify] = false
if last_modified.present?
# Set if_modified_since header only if we have a stored date
# and fetch_not_modified in config is set to false
if last_modified.present? && !fetch_all
connection.headers[:if_modified_since] = last_modified.httpdate
end
end
response = faraday.get(URI.encode(feed_url))
@response_status = response.status

if response.status == 200 && response.body.present?
if response.body.present? && response.status == 200 || fetch_all && response.status == 304
self.last_modified = response.env.response_headers[:last_modified]
self.etag = response.env.response_headers[:etag]
return response.body
Expand Down
14 changes: 7 additions & 7 deletions app/workers/feed_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.update(scope = 'main_feeds', options = {})
end

started_at = Time.now.to_f
failed = succeeded = not_modified = penalized = 0
failed = updated = not_modified = penalized = 0

feeds = Feed.public_send(scope)

Expand Down Expand Up @@ -41,11 +41,11 @@ def self.update(scope = 'main_feeds', options = {})
fetched_and_parsed = feed.fetch_and_parse(worker_logger)

# The feed was parsed and has changed since last fetch
# or config is set to fetch all feeds
if fetched_and_parsed &&
(feed.response_status == 200 ||
feed_worker_config['fetch_not_modified'] &&
feed.response_status == 304)
succeeded += 1
feed.response_status == 200 ||
feed_worker_config['fetch_not_modified'] && feed.response_status == 304
updated += 1
feed.map_feed_attributes
feed.feed_entries << feed.fresh_feed_entries
feed.save(validate: false)
Expand All @@ -72,10 +72,10 @@ def self.update(scope = 'main_feeds', options = {})

# Log stats
worker_logger.info "FeedWorker updated feeds in #{(Time.now.to_f - started_at).ceil} seconds."
worker_logger.info " Updated: #{succeeded}"
worker_logger.info " Updated: #{updated}"
worker_logger.info " Not modified: #{not_modified}"
worker_logger.info " Failed: #{failed}"
worker_logger.info " Penalized: #{penalized}"
worker_logger.info " Total: #{succeeded + not_modified + failed + penalized}"
worker_logger.info " Total: #{updated + not_modified + failed + penalized}"
end
end

0 comments on commit f148d90

Please sign in to comment.