From fa202af26d9f6c6e4284b94e4da9a794079665e0 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Tue, 16 Apr 2024 16:18:51 -0700 Subject: [PATCH] Time out if manifest loading takes too long This hasn't been causing issues as far as I know, I just noticed *months ago* that I forgot to do this, and have had a sticky note about it on my desk since then lol. I tested this by temporarily setting the timeout to `0.5`, and watching it fail! --- app/models/swf_asset.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/models/swf_asset.rb b/app/models/swf_asset.rb index d2503b55..2687b2ae 100644 --- a/app/models/swf_asset.rb +++ b/app/models/swf_asset.rb @@ -78,9 +78,18 @@ class SwfAsset < ApplicationRecord end end + # Try loading the manifest. If we fail, record that we failed and return. begin - NeopetsMediaArchive.load_file(manifest_url, return_content:) => - {content:, source:} + Sync do |task| + task.with_timeout(5) do + NeopetsMediaArchive.load_file(manifest_url, return_content:) + end + end => {content:, source:} + rescue Async::TimeoutError + # If the request times out, record nothing and return nothing! We'll try + # again sometime, on the assumption that this is intermittent. + Rails.logger.warn("Timed out loading manifest for asset #{id}") + return nil rescue NeopetsMediaArchive::ResponseNotOK => error Rails.logger.warn "Failed to load manifest for asset #{id}: " + error.message @@ -90,14 +99,17 @@ class SwfAsset < ApplicationRecord return nil end + # If this was a fresh load over the network (or for some reason we're + # missing the timestamp), record that we succeeded. if source == "network" || manifest_loaded_at.blank? self.manifest_loaded_at = Time.now self.manifest_status_code = 200 save! if save_changes end - return nil unless return_content + return nil unless return_content # skip parsing if not needed! + # Parse the manifest as JSON, and return it! begin JSON.parse(content) rescue JSON::ParserError => error