Skip to content

Commit 94d08e1

Browse files
committed
Retry on error_apple non-terminal error
1 parent a9f8b35 commit 94d08e1

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

app/models/publishing_pipeline_state.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class PublishingPipelineState < ApplicationRecord
22
TERMINAL_STATUSES = [:complete, :error, :expired].freeze
3-
TERMINAL_FAILURE_STATUSES = [:error, :expired].freeze
3+
FAILURE_STATUSES = [:error, :expired, :error_apple].freeze
44
UNIQUE_STATUSES = TERMINAL_STATUSES + [:created, :started]
55

66
# Handle the max timout for a publishing pipeline: Pub RSS job + Pub Apple job + a few extra minutes of flight
@@ -173,8 +173,16 @@ def self.expire_pipelines!
173173
end
174174
end
175175

176+
def self.latest_failed_publishing_queue_items
177+
PublishingQueueItem.where(id: latest_failed_pipelines.select(:publishing_queue_item_id).distinct)
178+
end
179+
180+
def self.latest_failed_podcasts
181+
Podcast.where(id: latest_failed_publishing_queue_items.select(:podcast_id).distinct)
182+
end
183+
176184
def self.retry_failed_pipelines!
177-
Podcast.where(id: latest_failed_pipelines.select(:podcast_id).distinct).each do |podcast|
185+
latest_failed_podcasts.each do |podcast|
178186
Rails.logger.tagged("PublishingPipeLineState.retry_failed_pipelines!", "Podcast:#{podcast.id}") do
179187
start_pipeline!(podcast)
180188
end

app/models/publishing_queue_item.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class PublishingQueueItem < ApplicationRecord
88
latest_by_status(PublishingPipelineState::TERMINAL_STATUSES)
99
}
1010
scope :latest_failed, -> {
11-
latest_by_status(PublishingPipelineState::TERMINAL_FAILURE_STATUSES)
11+
latest_by_status(PublishingPipelineState::FAILURE_STATUSES)
1212
}
1313

1414
scope :latest_by_status, ->(status) {

test/models/publishing_pipeline_state_test.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,37 @@
186186
end
187187
end
188188

189+
describe ".latest_failed_pipelines" do
190+
it "returns the latest failed pipelines including intermediate and terminal errors" do
191+
# Create a publishing queue item and associated pipeline state
192+
pqi1 = PublishingQueueItem.ensure_queued!(podcast)
193+
_s1 = PublishingPipelineState.create!(podcast: podcast, publishing_queue_item: pqi1)
194+
PublishingPipelineState.error_apple!(podcast)
195+
PublishingPipelineState.complete!(podcast)
196+
197+
# Verify that the intermediate error is included in the latest failed pipelines
198+
assert_equal [podcast], PublishingPipelineState.latest_failed_podcasts
199+
assert_equal ["created", "error_apple", "complete"], PublishingPipelineState.latest_failed_pipelines.where(podcast: podcast).map(&:status)
200+
201+
# Create another publishing queue item and associated pipeline state
202+
pqi2 = PublishingQueueItem.ensure_queued!(podcast)
203+
_s2 = PublishingPipelineState.create!(podcast: podcast, publishing_queue_item: pqi2)
204+
PublishingPipelineState.error!(podcast)
205+
206+
# Verify that the terminal error is included in the latest failed pipelines
207+
assert_equal [podcast], PublishingPipelineState.latest_failed_podcasts
208+
assert_equal ["created", "error"], PublishingPipelineState.latest_failed_pipelines.where(podcast: podcast).map(&:status)
209+
210+
# Verify that a successful pipeline is not included in the latest failed pipelines
211+
pqi3 = PublishingQueueItem.ensure_queued!(podcast)
212+
_s3 = PublishingPipelineState.create!(podcast: podcast, publishing_queue_item: pqi3)
213+
PublishingPipelineState.complete!(podcast)
214+
215+
assert_equal [].sort, PublishingPipelineState.latest_failed_pipelines.where(podcast: podcast)
216+
assert ["created", "complete"], PublishingPipelineState.latest_pipelines.where(podcast: podcast).pluck(:status)
217+
end
218+
end
219+
189220
describe ".retry_failed_pipelines!" do
190221
it "should retry failed pipelines" do
191222
PublishingPipelineState.start_pipeline!(podcast)
@@ -200,6 +231,22 @@
200231
assert_equal ["created"].sort, PublishingPipelineState.latest_pipeline(podcast).map(&:status).sort
201232
end
202233

234+
it "retries pipelines with intermediate error_apple and non-error terminal status" do
235+
PublishingPipelineState.start_pipeline!(podcast)
236+
assert_equal ["created"], PublishingPipelineState.latest_pipeline(podcast).map(&:status)
237+
238+
# it fails
239+
PublishingPipelineState.error_apple!(podcast)
240+
assert_equal ["created", "error_apple"].sort, PublishingPipelineState.latest_pipeline(podcast).map(&:status).sort
241+
242+
PublishingPipelineState.complete!(podcast)
243+
assert_equal ["created", "error_apple", "complete"].sort, PublishingPipelineState.latest_pipeline(podcast).map(&:status).sort
244+
245+
# it retries
246+
PublishingPipelineState.retry_failed_pipelines!
247+
assert_equal ["created"].sort, PublishingPipelineState.latest_pipeline(podcast).map(&:status).sort
248+
end
249+
203250
it "ignores previously errored pipelines back in the queue" do
204251
# A failed pipeline
205252
PublishingPipelineState.start_pipeline!(podcast)

test/models/publishing_queue_item_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@
9494
assert_equal [pqi1].sort, PublishingQueueItem.latest_failed.where(podcast: podcast)
9595
assert_equal [].sort, PublishingQueueItem.latest_attempted.latest_failed.where(podcast: podcast)
9696
end
97+
98+
it "includes intermediate states like error_apple" do
99+
pqi1 = PublishingPipelineState.create!(podcast: podcast, publishing_queue_item: PublishingQueueItem.create!(podcast: podcast)).publishing_queue_item
100+
PublishingPipelineState.error_apple!(podcast)
101+
102+
assert_equal [pqi1].sort, PublishingQueueItem.latest_failed.where(podcast: podcast)
103+
assert_equal [pqi1].sort, PublishingQueueItem.latest_attempted.latest_failed.where(podcast: podcast)
104+
assert_equal [podcast], PublishingPipeLineState.latest_failed_podcasts
105+
106+
_pqi2 = PublishingPipelineState.create!(podcast: podcast, publishing_queue_item: PublishingQueueItem.create!(podcast: podcast)).publishing_queue_item
107+
108+
assert_equal [pqi1].sort, PublishingQueueItem.latest_failed.where(podcast: podcast)
109+
assert_equal [].sort, PublishingQueueItem.latest_attempted.latest_failed.where(podcast: podcast)
110+
end
97111
end
98112

99113
describe ".all_unfinished_items" do

0 commit comments

Comments
 (0)