Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix API returning 500 after a successful reingest #812

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 EngineClient(
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<bool> AsynchronousIngest(Asset asset,
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<int> AsynchronousIngestBatch(IReadOnlyCollection<Asset> assets
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
Loading