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

moving publish action into a lambda #296

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -11,11 +11,8 @@
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "open-search")
public class OpenSearchConfig {
private String url;
private String domain;
private String username;
private String password;
@Configuration("openSearchSqsProperties")
@ConfigurationProperties(prefix = "open-search-sqs")
public class OpenSearchSqsProperties {
private String queueUrl;
}

This file was deleted.

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) {
}
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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"));
Copy link
Contributor

@dominicwest dominicwest Apr 24, 2024

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

Copy link
Contributor Author

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.

}

updateGrantAdvertApplicationDates(advert);
Expand All @@ -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"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

}

advert.setStatus(GrantAdvertStatus.DRAFT);
Expand All @@ -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();

Expand Down Expand Up @@ -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()));
Expand Down

This file was deleted.

5 changes: 1 addition & 4 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,4 @@ aws.kms.stage=stage

spring.jpa.properties.hibernate.order_by.default_null_ordering=last

open-search.url=https://search-url
open-search.domain=domain
open-search.username=username
open-search.password=password
open-search-sqs.queueUrl=an-sqs-url
Loading
Loading