-
Notifications
You must be signed in to change notification settings - Fork 30
Change link-index-updater lambda to react on SQS events #1169
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
Merged
reakaleek
merged 5 commits into
main
from
feature/link-index-updater-lambda-sqs-trigger
Apr 28, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7e448c1
Change link index updater lambda to react on SQS events
reakaleek 49df98a
Remove temporary workflow for building the binary
reakaleek 1aa3e4a
Update src/infra/docs-lambda-index-publisher/Program.cs
reakaleek 2cd9d47
Merge branch 'main' into feature/link-index-updater-lambda-sqs-trigger
reakaleek bc4da9e
Merge branch 'main' into feature/link-index-updater-lambda-sqs-trigger
reakaleek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/infra/docs-lambda-index-publisher/LinkIndexProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Amazon.Lambda.Core; | ||
using Amazon.S3; | ||
using Amazon.S3.Model; | ||
using Elastic.Markdown.Links.CrossLinks; | ||
|
||
namespace Elastic.Documentation.Lambda.LinkIndexUploader; | ||
|
||
/// <summary> | ||
/// Gets the link index from S3 once. | ||
/// You can then update the link index with <see cref="UpdateLinkIndexEntry(LinkIndexEntry)"/> and save it with <see cref="Save()"/>. | ||
/// If the link index changed in the meantime, <see cref="Save()"/> will throw an exception, | ||
/// thus all the messages from the queue will be sent back to the queue. | ||
/// </summary> | ||
public class LinkIndexProvider(IAmazonS3 s3Client, ILambdaLogger logger, string bucketName, string key) | ||
{ | ||
private string? _etag; | ||
private LinkIndex? _linkIndex; | ||
|
||
private async Task<LinkIndex> GetLinkIndex() | ||
{ | ||
var getObjectRequest = new GetObjectRequest | ||
{ | ||
BucketName = bucketName, | ||
Key = key | ||
}; | ||
logger.LogInformation("Getting link index from s3://{bucketName}/{key}", bucketName, key); | ||
var getObjectResponse = await s3Client.GetObjectAsync(getObjectRequest); | ||
await using var stream = getObjectResponse.ResponseStream; | ||
_etag = getObjectResponse.ETag; | ||
logger.LogInformation("Successfully got link index from s3://{bucketName}/{key}", bucketName, key); | ||
_linkIndex = LinkIndex.Deserialize(stream); | ||
return _linkIndex; | ||
} | ||
|
||
public async Task UpdateLinkIndexEntry(LinkIndexEntry linkIndexEntry) | ||
{ | ||
_linkIndex ??= await GetLinkIndex(); | ||
if (_linkIndex.Repositories.TryGetValue(linkIndexEntry.Repository, out var existingEntry)) | ||
{ | ||
var newEntryIsNewer = DateTime.Compare(linkIndexEntry.UpdatedAt, existingEntry[linkIndexEntry.Branch].UpdatedAt) > 0; | ||
if (newEntryIsNewer) | ||
{ | ||
existingEntry[linkIndexEntry.Branch] = linkIndexEntry; | ||
logger.LogInformation("Updated existing entry for {repository}@{branch}", linkIndexEntry.Repository, linkIndexEntry.Branch); | ||
} | ||
else | ||
logger.LogInformation("Skipping update for {repository}@{branch} because the existing entry is newer", linkIndexEntry.Repository, linkIndexEntry.Branch); | ||
} | ||
else | ||
{ | ||
_linkIndex.Repositories.Add(linkIndexEntry.Repository, new Dictionary<string, LinkIndexEntry> | ||
{ | ||
{ linkIndexEntry.Branch, linkIndexEntry } | ||
}); | ||
logger.LogInformation("Added new entry for {repository}@{branch}", linkIndexEntry.Repository, linkIndexEntry.Branch); | ||
} | ||
} | ||
|
||
public async Task Save() | ||
{ | ||
if (_etag == null || _linkIndex == null) | ||
throw new InvalidOperationException("You must call UpdateLinkIndexEntry() before Save()"); | ||
var json = LinkIndex.Serialize(_linkIndex); | ||
logger.LogInformation("Saving link index to s3://{bucketName}/{key}", bucketName, key); | ||
var putObjectRequest = new PutObjectRequest | ||
{ | ||
BucketName = bucketName, | ||
Key = key, | ||
ContentBody = json, | ||
ContentType = "application/json", | ||
IfMatch = _etag // Only update if the ETag matches. Meaning the object has not been changed in the meantime. | ||
}; | ||
_ = await s3Client.PutObjectAsync(putObjectRequest); | ||
logger.LogInformation("Successfully saved link index to s3://{bucketName}/{key}", bucketName, key); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/infra/docs-lambda-index-publisher/LinkReferenceProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Amazon.Lambda.Core; | ||
using Amazon.S3; | ||
using Amazon.S3.Model; | ||
using Elastic.Markdown.IO.State; | ||
|
||
namespace Elastic.Documentation.Lambda.LinkIndexUploader; | ||
|
||
public class LinkReferenceProvider(IAmazonS3 s3Client, ILambdaLogger logger, string bucketName) | ||
{ | ||
public async Task<LinkReference> GetLinkReference(string key, Cancel ctx) | ||
{ | ||
var getObjectRequest = new GetObjectRequest | ||
{ | ||
BucketName = bucketName, | ||
Key = key | ||
}; | ||
logger.LogInformation("Getting object {key} from bucket {bucketName}", key, bucketName); | ||
var getObjectResponse = await s3Client.GetObjectAsync(getObjectRequest, ctx); | ||
await using var stream = getObjectResponse.ResponseStream; | ||
logger.LogInformation("Successfully got object {key} from bucket {bucketName}", key, bucketName); | ||
return LinkReference.Deserialize(stream); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.