-
Notifications
You must be signed in to change notification settings - Fork 0
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
moving publish action into a lambda #296
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
811f316
replacing synchronous calls to publish adverts into opensearch with a…
GavCookCO 0e3440f
updating code to match lambda + cleanup
GavCookCO 481ab28
updating queue action
GavCookCO 5fc5b7a
updating queue action (again)
GavCookCO 03eb29f
adding to the queue after saving
GavCookCO 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 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
25 changes: 0 additions & 25 deletions
25
src/main/java/gov/cabinetoffice/gap/adminbackend/config/WebClientConfig.java
This file was deleted.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/SendAdvertToSqsDto.java
This file contains 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,4 @@ | ||
package gov.cabinetoffice.gap.adminbackend.dtos; | ||
|
||
public record SendAdvertToSqsDto(String contentfulEntryId, String type) { | ||
} |
This file contains 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 |
---|---|---|
@@ -1,14 +1,19 @@ | ||
package gov.cabinetoffice.gap.adminbackend.services; | ||
|
||
import com.amazonaws.services.sqs.AmazonSQS; | ||
import com.amazonaws.services.sqs.model.SendMessageRequest; | ||
import com.contentful.java.cda.CDAArray; | ||
import com.contentful.java.cda.CDAClient; | ||
import com.contentful.java.cda.CDAEntry; | ||
import com.contentful.java.cda.QueryOperation; | ||
import com.contentful.java.cma.CMAClient; | ||
import com.contentful.java.cma.model.CMAEntry; | ||
import com.contentful.java.cma.model.rich.CMARichDocument; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import gov.cabinetoffice.gap.adminbackend.config.ContentfulConfigProperties; | ||
import gov.cabinetoffice.gap.adminbackend.config.FeatureFlagsConfigurationProperties; | ||
import gov.cabinetoffice.gap.adminbackend.config.OpenSearchSqsProperties; | ||
import gov.cabinetoffice.gap.adminbackend.dtos.SendAdvertToSqsDto; | ||
import gov.cabinetoffice.gap.adminbackend.dtos.grantadvert.GetGrantAdvertPageResponseDTO; | ||
import gov.cabinetoffice.gap.adminbackend.dtos.grantadvert.GetGrantAdvertPublishingInformationResponseDTO; | ||
import gov.cabinetoffice.gap.adminbackend.dtos.grantadvert.GetGrantAdvertStatusResponseDTO; | ||
|
@@ -62,14 +67,15 @@ public class GrantAdvertService { | |
private final CMAClient contentfulManagementClient; | ||
private final CDAClient contentfulDeliveryClient; | ||
private final UserService userService; | ||
private final OpenSearchService openSearchService; | ||
private final WebClient.Builder webClientBuilder; | ||
|
||
private final WebClient webClient; | ||
private final AmazonSQS amazonSqs; | ||
private final ObjectMapper mapper; | ||
private final Clock clock; | ||
private final ContentfulConfigProperties contentfulProperties; | ||
private final FeatureFlagsConfigurationProperties featureFlagsProperties; | ||
|
||
private final OpenSearchSqsProperties openSearchSqsProperties; | ||
|
||
public GrantAdvert save(GrantAdvert advert) { | ||
final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
Optional.ofNullable(auth) | ||
|
@@ -320,7 +326,7 @@ public GrantAdvert publishAdvert(UUID advertId) { | |
|
||
if (Boolean.FALSE.equals(contentfulAdvert.isPublished())) { | ||
final CMAEntry publishedAdvert = contentfulManagementClient.entries().publish(contentfulAdvert); | ||
openSearchService.indexEntry(publishedAdvert); | ||
sendMessageToQueue(new SendAdvertToSqsDto(publishedAdvert.getId(), "ADD")); | ||
} | ||
|
||
updateGrantAdvertApplicationDates(advert); | ||
|
@@ -333,7 +339,7 @@ public void unpublishAdvert(UUID advertId) { | |
|
||
if (Boolean.TRUE.equals(contentfulAdvert.isPublished())) { | ||
final CMAEntry unpublishedAd = contentfulManagementClient.entries().unPublish(contentfulAdvert); | ||
openSearchService.removeIndexEntry(unpublishedAd); | ||
sendMessageToQueue(new SendAdvertToSqsDto(unpublishedAd.getId(), "REMOVE")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
} | ||
|
||
advert.setStatus(GrantAdvertStatus.DRAFT); | ||
|
@@ -343,6 +349,19 @@ public void unpublishAdvert(UUID advertId) { | |
save(advert); | ||
} | ||
|
||
public void sendMessageToQueue(final SendAdvertToSqsDto advertDto) { | ||
final UUID messageId = UUID.randomUUID(); | ||
final String messageBody = mapper.valueToTree(advertDto).toString(); | ||
final SendMessageRequest messageRequest = new SendMessageRequest() | ||
.withQueueUrl(openSearchSqsProperties.getQueueUrl()) | ||
.withMessageGroupId(messageId.toString()) | ||
.withMessageBody(messageBody) | ||
.withMessageDeduplicationId(messageId.toString()); | ||
|
||
amazonSqs.sendMessage(messageRequest); | ||
log.info("Message sent to queue for advert with contentful ID {}"); | ||
} | ||
|
||
private CMAEntry createAdvertInContentful(final GrantAdvert grantAdvert) { | ||
final CMAEntry contentfulAdvert = new CMAEntry(); | ||
|
||
|
@@ -438,7 +457,8 @@ private void createRichTextQuestionsInContentful(final GrantAdvert advert, final | |
} | ||
|
||
final Instant now = Instant.now(); | ||
webClient.patch() | ||
webClientBuilder.build() | ||
.patch() | ||
.uri(contentfulUrl) | ||
.headers(h -> { | ||
h.set("Authorization", String.format("Bearer %s", contentfulProperties.getAccessToken())); | ||
|
96 changes: 0 additions & 96 deletions
96
src/main/java/gov/cabinetoffice/gap/adminbackend/services/OpenSearchService.java
This file was deleted.
Oops, something went wrong.
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this function should be the very last thing this endpoint does - as if we fail to save to the DB for whatever reason upon a scheduled publishing, this endpoint will be hit multiple times due to the publish advert lambdas retry behaviour, which will result in us sending multiple messages to our new publish elastic lambda.
I think the end state would then be duplicate adverts showing up in elastic in this edge case, which would require manually cleaning up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jamie and I tested elastic last week, it's idempotent so not sure there would be duplicates but the lambda on the other end of this exchange might end up with errors from contentful so good shout nonetheless.