Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/wait-for-timeouts-raise-exception'
Browse files Browse the repository at this point in the history
  • Loading branch information
svevang committed Jul 25, 2023
2 parents 25d0629 + 11801c4 commit 7d220c3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
9 changes: 6 additions & 3 deletions app/models/apple/api_waiting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ module ApiWaiting
def self.wait_for(remaining_records)
t_beg = Time.now.utc
loop do
# TODO: handle timeout
break [false, remaining_records] if Time.now.utc - t_beg > self::API_WAIT_TIMEOUT
Rails.logger.info(".wait_for", {remaining_records: remaining_records, have_waited: Time.now.utc - t_beg})

# Return `timeout == true` if we've waited too long
break [true, remaining_records] if Time.now.utc - t_beg > self::API_WAIT_TIMEOUT

remaining_records = yield(remaining_records)

break [true, []] if remaining_records.empty?
# All done, return `timeout == false`
break [false, []] if remaining_records.empty?

sleep(self::API_WAIT_INTERVAL)
end
Expand Down
14 changes: 7 additions & 7 deletions app/models/apple/episode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ def self.wait_for_asset_state(api, eps)

remaining_eps.each do |ep|
Rails.logger.info("Waiting for audio asset state?", {episode_id: ep.feeder_id,
delivery_file_count: ep.podcast_delivery_files.count,
delivery_files_processed_errors: ep.podcast_delivery_files.all?(&:processed_errors?),
delivery_files_processed: ep.podcast_delivery_files.all?(&:processed?),
delivery_files_delivered: ep.podcast_delivery_files.all?(&:delivered?),
asset_state: ep.audio_asset_state,
has_podcast_audio: ep&.podcast_container&.has_podcast_audio?,
waiting_for_asset_state: ep.waiting_for_asset_state?})
delivery_file_count: ep.podcast_delivery_files.count,
delivery_files_processed_errors: ep.podcast_delivery_files.all?(&:processed_errors?),
delivery_files_processed: ep.podcast_delivery_files.all?(&:processed?),
delivery_files_delivered: ep.podcast_delivery_files.all?(&:delivered?),
asset_state: ep.audio_asset_state,
has_podcast_audio: ep&.podcast_container&.has_podcast_audio?,
waiting_for_asset_state: ep.waiting_for_asset_state?})
end

rem =
Expand Down
10 changes: 8 additions & 2 deletions app/models/apple/publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,20 @@ def wait_for_upload_processing(eps)
Rails.logger.tagged("##{__method__}") do
pdfs = eps.map(&:podcast_delivery_files).flatten

Apple::PodcastDeliveryFile.wait_for_delivery_files(api, pdfs)
(waiting_timed_out, _) = Apple::PodcastDeliveryFile.wait_for_delivery(api, pdfs)
raise "Timed out waiting for delivery" if waiting_timed_out

(waiting_timed_out, _) = Apple::PodcastDeliveryFile.wait_for_processing(api, pdfs)
raise "Timed out waiting for processing" if waiting_timed_out
end
end

def wait_for_asset_state(eps)
Rails.logger.tagged("##{__method__}") do
eps = eps.filter { |e| e.podcast_delivery_files.any?(&:api_marked_as_uploaded?) }
Apple::Episode.wait_for_asset_state(api, eps)

(waiting_timed_out, _) = Apple::Episode.wait_for_asset_state(api, eps)
raise "Timed out waiting for asset state" if waiting_timed_out
end
end

Expand Down
8 changes: 4 additions & 4 deletions test/models/apple/api_waiting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TestTimeout

step = 0

(finished_waiting, remaining) = TestWait.wait_for(records) do |remaining|
(timed_out, remaining) = TestWait.wait_for(records) do |remaining|
rem = remaining.dup

assert_equal intervals[step], rem
Expand All @@ -37,16 +37,16 @@ class TestTimeout
rem
end

assert_equal finished_waiting, true
assert_equal timed_out, false
assert_equal remaining, []
end

it "times out" do
(finished_waiting, remaining) = TestTimeout.wait_for(["a", "b", "c"]) do |remaining|
(timed_out, remaining) = TestTimeout.wait_for(["a", "b", "c"]) do |remaining|
remaining
end

assert_equal finished_waiting, false
assert_equal timed_out, true
assert_equal remaining, ["a", "b", "c"]
end
end
Expand Down

0 comments on commit 7d220c3

Please sign in to comment.