Skip loading manifest if we recently failed

This helps speed up some item search result pages a lot in the new API
endpoint I'm building!
This commit is contained in:
Emi Matchu 2024-02-25 15:46:50 -08:00
parent a684c915a9
commit cc33ce1d6e

View file

@ -56,19 +56,36 @@ class SwfAsset < ApplicationRecord
end end
def load_manifest def load_manifest
# If we recently tried loading the manifest and got a 4xx HTTP status code
# (e.g. a 404, there's a surprising amount of these!), don't try again. But
# after enough time passes, if this is called again, we will!
#
# (We always retry 5xx errors, on the assumption that they probably
# represent intermittent failures, whereas 4xx errors are not likely to
# succeed just by retrying.)
if manifest_loaded_at.present?
last_try_was_4xx =(400...500).include?(manifest_status_code)
last_try_was_recent = (Time.now - manifest_loaded_at) <= 1.day
if last_try_was_4xx and last_try_was_recent
Rails.logger.debug "Skipping loading manifest for asset #{id}: " +
"last try was status #{manifest_status_code} at #{manifest_loaded_at}"
return nil
end
end
begin begin
NeopetsMediaArchive.load_file(manifest_url) => {content:, source:} NeopetsMediaArchive.load_file(manifest_url) => {content:, source:}
rescue NeopetsMediaArchive::ResponseNotOK => error rescue NeopetsMediaArchive::ResponseNotOK => error
Rails.logger.warn "Failed to load manifest for asset #{id}: " + Rails.logger.warn "Failed to load manifest for asset #{id}: " +
error.message error.message
self.manifest_loaded_at = DateTime.now self.manifest_loaded_at = Time.now
self.manifest_status_code = error.status self.manifest_status_code = error.status
save! save!
return nil return nil
end end
if source == "network" || manifest_loaded_at.blank? if source == "network" || manifest_loaded_at.blank?
self.manifest_loaded_at = DateTime.now self.manifest_loaded_at = Time.now
self.manifest_status_code = 200 self.manifest_status_code = 200
save! save!
end end