diff --git a/src/protagonist/CleanupHandler/AssetUpdatedHandler.cs b/src/protagonist/CleanupHandler/AssetUpdatedHandler.cs index 7f0ca03e8..c4130a251 100644 --- a/src/protagonist/CleanupHandler/AssetUpdatedHandler.cs +++ b/src/protagonist/CleanupHandler/AssetUpdatedHandler.cs @@ -104,7 +104,15 @@ public async Task HandleMessage(QueueMessage message, CancellationToken ca if (modifiedOrAdded.Any()) { - await CleanupModified(modifiedOrAdded, assetBefore, assetAfter, s3Objects); + try + { + await CleanupModified(modifiedOrAdded, assetBefore, assetAfter, s3Objects); + } + catch (Exception ex) + { + logger.LogError(ex, "Error cleaning modified delivery channels"); + return false; + } } if (!Equals(assetAfter.Roles ?? string.Empty, assetBefore.Roles ?? string.Empty)) @@ -215,7 +223,7 @@ private async Task CleanupChangedPolicy(ImageDeliveryChannel deliveryChannelModi private async Task CleanupChangedTimebasedDeliveryChannel(ImageDeliveryChannel imageDeliveryChannel, Asset assetAfter, HashSet objectsToRemove) { - var presetList = imageDeliveryChannel.DeliveryChannelPolicy.AsTimebasedPresets(); + var presetList = imageDeliveryChannel.DeliveryChannelPolicy.AsTimebasedPresets(); var keys = new List(); var extensions = new List(); var mediaPath = RetrieveMediaPath(assetAfter); @@ -227,7 +235,7 @@ private async Task CleanupChangedTimebasedDeliveryChannel(ImageDeliveryChannel i logger.LogWarning( "retrieved no timebased presets from engine, {AssetId} will not be cleaned up for the timebased channel", assetAfter.Id); - return; + throw new ArgumentNullException(nameof(presetDictionary), "Failed to retrieve any preset values"); } foreach (var presetIdentifier in presetList ?? new List()) diff --git a/src/protagonist/CleanupHandlerTests/AssetUpdatedHandlerTests.cs b/src/protagonist/CleanupHandlerTests/AssetUpdatedHandlerTests.cs index 5b63b9eb8..df5236114 100644 --- a/src/protagonist/CleanupHandlerTests/AssetUpdatedHandlerTests.cs +++ b/src/protagonist/CleanupHandlerTests/AssetUpdatedHandlerTests.cs @@ -576,6 +576,12 @@ public async Task Handle_DoesNothing_WhenTimebasedChannelUpdatedWithInvalidPrese }, DeliveryChannelPolicyId = 8239 }; + + A.CallTo(() => engineClient.GetAvPresets(A._)).Returns(new Dictionary() + { + { "webm-policy", new ("", "some-webm-preset", "oga") }, + { "oga-policy", new ("", "some-oga-preset", "webm") } + }); var requestDetails = CreateMinimalRequestDetails( new List() { imageDeliveryChannelTimebased }, @@ -595,6 +601,43 @@ public async Task Handle_DoesNothing_WhenTimebasedChannelUpdatedWithInvalidPrese A.CallTo(() => bucketWriter.DeleteFolder(A._, A._)).MustNotHaveHappened(); } + [Fact] + public async Task Handle_ReturnsFalse_WhenTimebasedChannelUpdatedWithNoAvPresets() + { + // Arrange + var imageDeliveryChannelAfter = new ImageDeliveryChannel() + { + Channel = AssetDeliveryChannels.Timebased, + Id = 23456, + DeliveryChannelPolicy = new DeliveryChannelPolicy() + { + Id = 8239, + Channel = AssetDeliveryChannels.Timebased, + Created = DateTime.MinValue, + Modified = DateTime.MinValue, + PolicyData = "[\"policy-not-found\"]" + }, + DeliveryChannelPolicyId = 8239 + }; + + var requestDetails = CreateMinimalRequestDetails( + new List() { imageDeliveryChannelTimebased }, + new List() { imageDeliveryChannelAfter }, + string.Empty, string.Empty, "video/*"); + + A.CallTo(() => cleanupHandlerAssetRepository.RetrieveAssetWithDeliveryChannels(A._)) + .Returns(requestDetails.assetAfter); + + // Act + var sut = GetSut(); + var response = await sut.HandleMessage(requestDetails.queueMessage); + + // Assert + response.Should().BeFalse(); + A.CallTo(() => bucketWriter.DeleteFromBucket(A._)).MustNotHaveHappened(); + A.CallTo(() => bucketWriter.DeleteFolder(A._, A._)).MustNotHaveHappened(); + } + [Fact] public async Task Handle_DoesNothing_WhenTimebasedChannelModfiedWithInvalidPresetDetails() { diff --git a/src/protagonist/DLCS.Model/Policies/DeliveryChannelPolicyX.cs b/src/protagonist/DLCS.Model/Policies/DeliveryChannelPolicyX.cs index 6a8de8c50..73ad0eea1 100644 --- a/src/protagonist/DLCS.Model/Policies/DeliveryChannelPolicyX.cs +++ b/src/protagonist/DLCS.Model/Policies/DeliveryChannelPolicyX.cs @@ -41,8 +41,10 @@ public static List AsTimebasedPresets(this DeliveryChannelPolicy deliver { throw new InvalidOperationException("Policy is not for timebased channel"); } + + var timeBasedPresets = deliveryChannelPolicy.PolicyDataAs>(); - return deliveryChannelPolicy.PolicyDataAs>() ?? new List(); + return timeBasedPresets.ThrowIfNull(nameof(timeBasedPresets)); } ///