Skip to content

Commit

Permalink
Vary log level output by error count
Browse files Browse the repository at this point in the history
  • Loading branch information
svevang committed Oct 18, 2024
1 parent e12ae3c commit 7d47984
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
14 changes: 11 additions & 3 deletions app/jobs/publish_feed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,18 @@ def publish_rss(podcast, feed)
handle_rss_error(podcast, feed, e)
end

def handle_apple_timeout_error(podcast, error, raise_error: true)
def handle_apple_timeout_error(podcast, asset_timeout_error)
PublishingPipelineState.retry!(podcast)
Rails.logger.error("Asset processing timeout", {podcast_id: podcast.id, error: e.message, backtrace: e.backtrace.join("\n")})
# TODO Does not re-raise the error
Rails.logger.send(asset_timeout_error.log_level) do
[
"Asset processing timeout",
{
podcast_id: podcast.id,
error: asset_timeout_error.message,
backtrace: asset_timeout_error&.backtrace&.join("\n")
}
]
end
end

def handle_apple_error(podcast, error)
Expand Down
11 changes: 11 additions & 0 deletions app/models/apple/asset_state_timeout_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@ def initialize(episodes)
def episode_ids
episodes.map(&:feeder_id)
end

def log_level
case attempts
when 0..4
:warn
when 5
:error
else
:fatal
end
end
end
end
53 changes: 36 additions & 17 deletions test/jobs/publish_feed_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,16 @@
end

describe "publishing to apple" do
let(:podcast) { create(:podcast) }
let(:public_feed) { podcast.default_feed }
let(:private_feed) { create(:apple_feed, podcast: podcast) }
let(:apple_feed) { private_feed }
let(:apple_config) { podcast.apple_config }
let(:apple_config) { private_feed.apple_config }
let(:apple_publisher) { apple_config.build_publisher }

before do
assert private_feed.persisted?
assert podcast.reload.apple_config.present?
assert apple_config.persisted?
end

describe "#perform" do
Expand Down Expand Up @@ -163,25 +167,40 @@
PublishingQueueItem.create!(podcast: feed.podcast)
end

it "raises an error if the apple publishing fails" do
assert apple_feed.apple_config.present?
assert apple_feed.apple_config.publish_enabled

PublishAppleJob.stub(:do_perform, ->(*, **) { raise "some apple error" }) do
assert_raises(RuntimeError) { PublishingPipelineState.attempt!(feed.podcast, perform_later: false) }
let(:episode1) { build(:uploaded_apple_episode, show: apple_publisher.show) }
let(:episode2) { build(:uploaded_apple_episode, show: apple_publisher.show) }
let(:episodes) { [episode1, episode2] }

assert_equal ["created", "started", "error", "error_apple"].sort, PublishingPipelineState.where(podcast: feed.podcast).latest_pipelines.pluck(:status).sort
end
end

it "raises an error if the apple publishing times out" do
it "logs message if the apple publishing times out" do
assert apple_feed.apple_config.present?
assert apple_feed.apple_config.publish_enabled

PublishAppleJob.stub(:do_perform, ->(*, **) { raise Apple::AssetStateTimeoutError.new([]) }) do
assert_raises(Apple::AssetStateTimeoutError) { PublishingPipelineState.attempt!(feed.podcast, perform_later: false) }

assert_equal ["created", "started", "error", "error_apple"].sort, PublishingPipelineState.where(podcast: feed.podcast).latest_pipelines.pluck(:status).sort
expected_level_for_timeouts = [
[0, 40],
[1, 40],
[2, 40],
[3, 40],
[4, 40],
[5, 50],
[6, 60]
]

expected_level_for_timeouts.each do |(attempts, level)|
# simulate a episode waiting n times
episodes.first.apple_episode_delivery_status.update(asset_processing_attempts: attempts)

PublishAppleJob.stub(:do_perform, ->(*, **) { raise Apple::AssetStateTimeoutError.new(episodes) }) do
lines = capture_json_logs do
PublishingQueueItem.ensure_queued!(feed.podcast)
PublishingPipelineState.attempt!(feed.podcast, perform_later: false)
end

log = lines.find { |l| l["msg"].include?("Asset processing timeout") }
assert log.present?
assert_equal level, log["level"]

assert_equal ["created", "started", "retry", "error_apple"].sort, PublishingPipelineState.where(podcast: feed.podcast).latest_pipelines.pluck(:status).sort
end
end
end
end
Expand Down

0 comments on commit 7d47984

Please sign in to comment.