Skip to content

Commit

Permalink
Merge pull request #812 from dlcs/fix/reingest_returns_500
Browse files Browse the repository at this point in the history
Fix API returning 500 after a successful reingest
  • Loading branch information
griffri committed Apr 17, 2024
2 parents b33da37 + 8b71892 commit a5b5153
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ public class AssetNotificationSender : IAssetNotificationSender
private readonly ILogger<AssetNotificationSender> logger;
private readonly ITopicPublisher topicPublisher;
private readonly IPathCustomerRepository customerPathRepository;
private readonly JsonSerializerOptions settings = new(JsonSerializerDefaults.Web);
private readonly JsonSerializerOptions settings = new(JsonSerializerDefaults.Web)
{
ReferenceHandler = ReferenceHandler.IgnoreCycles
};

private readonly Dictionary<int, CustomerPathElement> customerPathElements = new();

Expand Down Expand Up @@ -103,8 +106,6 @@ private async Task<string> GetSerialisedAssetCreatedNotification(Asset modifiedA
CustomerPathElement = customerPathElement
};

modifiedAsset = RefreshImageDeliveryChannelsForAsset(modifiedAsset);

return JsonSerializer.Serialize(request, settings);
}

Expand All @@ -119,9 +120,6 @@ private async Task<string> GetSerialisedAssetUpdatedNotification(Asset modifiedA
CustomerPathElement = customerPathElement
};

request.AssetBeforeUpdate = RefreshImageDeliveryChannelsForAsset(request.AssetBeforeUpdate);
request.AssetAfterUpdate = RefreshImageDeliveryChannelsForAsset(request.AssetAfterUpdate);

return JsonSerializer.Serialize(request, settings);
}

Expand All @@ -145,20 +143,4 @@ private async Task<bool> SendAssetModifiedRequest(Dictionary<ChangeType, List<st

return await topicPublisher.PublishToAssetModifiedTopic(toSend, cancellationToken);
}


private Asset RefreshImageDeliveryChannelsForAsset(Asset asset)
{
if (!asset.ImageDeliveryChannels.IsNullOrEmpty())
{
asset.ImageDeliveryChannels = asset.ImageDeliveryChannels.Select(x => new ImageDeliveryChannel()
{
ImageId = x.ImageId,
Channel = x.Channel,
DeliveryChannelPolicyId = x.DeliveryChannelPolicyId
}).ToList();
}

return asset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
using System.Text.Json.Serialization;
using System.Threading;
using DLCS.AWS.SQS;
using DLCS.Core.Settings;
using DLCS.Core.Types;
using DLCS.Model.Assets;
using DLCS.Model.Messaging;
using DLCS.Repository.Messaging;
using FakeItEasy;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using Test.Helpers.Http;

namespace DLCS.Repository.Tests.Messaging;
Expand All @@ -37,12 +34,7 @@ public EngineClientTests()
queueLookup = A.Fake<IQueueLookup>();
queueSender = A.Fake<IQueueSender>();

var engineClientOptions = Options.Create(new DlcsSettings
{
EngineRoot = new Uri("http://engine.dlcs/")
});

sut = new EngineClient(queueLookup, queueSender, httpClient, engineClientOptions, new NullLogger<EngineClient>());
sut = new EngineClient(queueLookup, queueSender, httpClient, new NullLogger<EngineClient>());
}

[Fact]
Expand Down
13 changes: 2 additions & 11 deletions src/protagonist/DLCS.Repository/Messaging/EngineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
using System.Threading;
using System.Threading.Tasks;
using DLCS.AWS.SQS;
using DLCS.Core.Settings;
using DLCS.Model.Assets;
using DLCS.Model.Messaging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace DLCS.Repository.Messaging;

Expand All @@ -27,7 +25,6 @@ public class EngineClient : IEngineClient
private readonly IQueueSender queueSender;
private readonly HttpClient httpClient;
private readonly ILogger<EngineClient> logger;
private readonly DlcsSettings dlcsSettings;

private static readonly JsonSerializerOptions SerializerOptions = new (JsonSerializerDefaults.Web)
{
Expand All @@ -38,14 +35,12 @@ public class EngineClient : IEngineClient
IQueueLookup queueLookup,
IQueueSender queueSender,
HttpClient httpClient,
IOptions<DlcsSettings> dlcsSettings,
ILogger<EngineClient> logger)
{
this.queueLookup = queueLookup;
this.queueSender = queueSender;
this.httpClient = httpClient;
this.logger = logger;
this.dlcsSettings = dlcsSettings.Value;
}

public async Task<HttpStatusCode> SynchronousIngest(Asset asset, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -87,18 +82,16 @@ public async Task<HttpStatusCode> SynchronousIngest(Asset asset, CancellationTok
CancellationToken cancellationToken = default)
{
var queueName = queueLookup.GetQueueNameForFamily(asset.Family ?? new AssetFamily());
var ingestAssetRequest = new IngestAssetRequest(asset.Id, DateTime.UtcNow);

var jsonString = GetJsonString(asset);
var success = await queueSender.QueueMessage(queueName, jsonString, cancellationToken);

if (!success)
{
logger.LogInformation("Error queueing ingest request {IngestRequest}", ingestAssetRequest);
logger.LogInformation("Error queueing ingest request for {AssetId}", asset.Id);
}
else
{
logger.LogDebug("Successfully enqueued ingest request {IngestRequest}", ingestAssetRequest);
logger.LogDebug("Successfully enqueued ingest request for {AssetId}", asset.Id);
}

return success;
Expand Down Expand Up @@ -156,8 +149,6 @@ public async Task<HttpStatusCode> SynchronousIngest(Asset asset, CancellationTok
private string GetJsonString(Asset asset)
{
var ingestAssetRequest = new IngestAssetRequest(asset.Id, DateTime.UtcNow);

// Otherwise, it should contain only the Asset ID - for now, this is an Asset object containing just the ID
var jsonString = JsonSerializer.Serialize(ingestAssetRequest, SerializerOptions);
return jsonString;
}
Expand Down

0 comments on commit a5b5153

Please sign in to comment.