in src/infra/docs-lambda-index-publisher/Program.cs [26:78]
static async Task<SQSBatchResponse> Handler(SQSEvent ev, ILambdaContext context)
{
var s3Client = new AmazonS3Client();
var linkIndexProvider = new LinkIndexProvider(s3Client, context.Logger, bucketName, indexFile);
var batchItemFailures = new List<SQSBatchResponse.BatchItemFailure>();
foreach (var message in ev.Records)
{
context.Logger.LogInformation("Processing message {MessageId}", message.MessageId);
context.Logger.LogInformation("Message body: {MessageBody}", message.Body);
try
{
var s3RecordLinkReferenceTuples = await GetS3RecordLinkReferenceTuples(s3Client, message, context);
foreach (var (s3Record, linkReference) in s3RecordLinkReferenceTuples)
{
var newEntry = ConvertToLinkIndexEntry(s3Record, linkReference);
await linkIndexProvider.UpdateLinkIndexEntry(newEntry);
}
}
catch (Exception e)
{
// Add failed message identifier to the batchItemFailures list
context.Logger.LogWarning(e, "Failed to process message {MessageId}", message.MessageId);
batchItemFailures.Add(new SQSBatchResponse.BatchItemFailure
{
ItemIdentifier = message.MessageId
});
}
}
try
{
await linkIndexProvider.Save();
var response = new SQSBatchResponse(batchItemFailures);
if (batchItemFailures.Count > 0)
context.Logger.LogInformation("Failed to process {batchItemFailuresCount} of {allMessagesCount} messages. Returning them to the queue.", batchItemFailures.Count, ev.Records.Count);
var jsonStr = JsonSerializer.Serialize(response, SerializerContext.Default.SQSBatchResponse);
context.Logger.LogInformation(jsonStr);
return response;
}
catch (Exception ex)
{
// If we fail to update the link index, we need to return all messages to the queue
// so that they can be retried later.
context.Logger.LogError("Failed to update {bucketName}/{indexFile}. Returning all {recordCount} messages to the queue.", bucketName, indexFile, ev.Records.Count);
context.Logger.LogError(ex, ex.Message);
var response = new SQSBatchResponse(ev.Records.Select(r => new SQSBatchResponse.BatchItemFailure
{
ItemIdentifier = r.MessageId
}).ToList());
var jsonStr = JsonSerializer.Serialize(response, SerializerContext.Default.SQSBatchResponse);
context.Logger.LogInformation(jsonStr);
return response;
}
}