From 549046096c53b591c9c86dd14ab57afc4cc8f7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arwid=20Thornstr=C3=B6m?= Date: Thu, 10 Oct 2024 14:34:29 +0200 Subject: [PATCH] added support for sqs sample ripper when creating new products (#167) --- .env.example | 1 + cloudformation/Makefile | 8 ++++++++ cloudformation/ecs-service.yaml | 10 ++++++++++ docker-compose-prodlike.yml | 1 + docker-compose.yml.example | 1 + src/aws/sqs.ts | 21 +++++++++++++++++++++ src/bernard/client.ts | 22 +--------------------- src/resolvers/products-resolver.ts | 12 +++++++++++- 8 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 src/aws/sqs.ts diff --git a/.env.example b/.env.example index b44a0c0..110ad38 100644 --- a/.env.example +++ b/.env.example @@ -11,3 +11,4 @@ AWS_SECRET_ACCESS_KEY= AWS_ACCESS_KEY_ID= AWS_DEFAULT_REGION=eu-west-1 BERNARD_QUEUE_URL= +SAMPLE_SQS_QUEUE_URL= diff --git a/cloudformation/Makefile b/cloudformation/Makefile index 6c63508..5056460 100644 --- a/cloudformation/Makefile +++ b/cloudformation/Makefile @@ -13,6 +13,9 @@ GITHUB_REPO := Photowall/graphql-api-js AWS_REGION=$(shell aws configure get region) AWS_ACCOUNT_ID=$(shell aws sts get-caller-identity --query Account --output text) +$(eval SAMPLE_RIPPER_QUEUE_ARN_STAGING=$(shell aws ssm get-parameter --name /staging/SAMPLE_RIPPER_QUEUE_ARN --output text --query Parameter.Value)) +$(eval SAMPLE_RIPPER_QUEUE_ARN_PRODUCTION=$(shell aws ssm get-parameter --name /production/SAMPLE_RIPPER_QUEUE_ARN --output text --query Parameter.Value)) + # HOSTED_ZONE=photowall.com default in the template # Cert ARN for the hosted zone CERTIFICATE_ARN='arn:aws:acm:eu-west-1:954747537408:certificate/1278df32-006d-48a4-abd6-2bfccabb11fd' @@ -27,6 +30,7 @@ ifeq ($(STACK_ENVIRONMENT_NAME),production) SUBDOMAIN=$(STACK_NAME) ENVIRONMENT_NAME=production BERNARD_QUEUE_ARN=arn:aws:sqs:eu-west-1:954747537408:photowall-bernard-production + SAMPLE_RIPPER_QUEUE_ARN=$(SAMPLE_RIPPER_QUEUE_ARN_PRODUCTION) GITHUB_BRANCH=master DB_SECURITY_GROUP_ID=$(DB_SECURITY_GROUP_ID_PRODUCTION) CPU_SIZE=1024 @@ -37,6 +41,7 @@ else ifeq ($(STACK_ENVIRONMENT_NAME),production-web) SUBDOMAIN=data ENVIRONMENT_NAME=production BERNARD_QUEUE_ARN=arn:aws:sqs:eu-west-1:954747537408:photowall-bernard-production + SAMPLE_RIPPER_QUEUE_ARN=$(SAMPLE_RIPPER_QUEUE_ARN_PRODUCTION) GITHUB_BRANCH=master DB_SECURITY_GROUP_ID=$(DB_SECURITY_GROUP_ID_PRODUCTION) CPU_SIZE=1024 @@ -47,6 +52,7 @@ else SUBDOMAIN=$(STACK_NAME)-$(STACK_ENVIRONMENT_NAME) ENVIRONMENT_NAME=$(STACK_ENVIRONMENT_NAME) BERNARD_QUEUE_ARN=arn:aws:sqs:eu-west-1:954747537408:photowall-bernard-stage + SAMPLE_RIPPER_QUEUE_ARN=$(SAMPLE_RIPPER_QUEUE_ARN_STAGING) GITHUB_BRANCH=$(STACK_ENVIRONMENT_NAME) DB_SECURITY_GROUP_ID=$(DB_SECURITY_GROUP_ID_STAGING) CPU_SIZE=512 @@ -106,6 +112,7 @@ create-stack: check-create-variables-set check-create-initial-ecr-image --tags Key=EnvironmentName,Value=$(STACK_ENVIRONMENT_NAME) \ --parameters ParameterKey="EnvironmentName",ParameterValue="$(ENVIRONMENT_NAME)" \ ParameterKey="BernardQueueArn",ParameterValue="${BERNARD_QUEUE_ARN}" \ + ParameterKey="SampleRipperQueueArn",ParameterValue="${SAMPLE_RIPPER_QUEUE_ARN}" \ ParameterKey="Image",ParameterValue="$(DOCKER_IMAGE)" \ ParameterKey="Subdomain",ParameterValue="$(SUBDOMAIN)" \ ParameterKey="Certificate",ParameterValue="$(CERTIFICATE_ARN)" \ @@ -125,6 +132,7 @@ update-stack: set-repository-lifecycle-policy --tags Key=EnvironmentName,Value=$(STACK_ENVIRONMENT_NAME) \ --parameters ParameterKey="EnvironmentName",UsePreviousValue=true \ ParameterKey="BernardQueueArn",ParameterValue="${BERNARD_QUEUE_ARN}" \ + ParameterKey="SampleRipperQueueArn",ParameterValue="${SAMPLE_RIPPER_QUEUE_ARN}" \ ParameterKey="Image",UsePreviousValue=true \ ParameterKey="Subdomain",UsePreviousValue=true \ ParameterKey="Certificate",UsePreviousValue=true \ diff --git a/cloudformation/ecs-service.yaml b/cloudformation/ecs-service.yaml index d4c4335..daf4e6a 100644 --- a/cloudformation/ecs-service.yaml +++ b/cloudformation/ecs-service.yaml @@ -30,6 +30,10 @@ Parameters: Type: String Description: For policy connection the Bernard queue arn is required + SampleRipperQueueArn: + Type: String + Description: For policy connection the Sample Ripper queue arn is required + # CPU # 256 (.25 vCPU) - Available memory values: 0.5GB, 1GB, 2GB # 512 (.5 vCPU) - Available memory values: 1GB, 2GB, 3GB, 4GB @@ -155,6 +159,8 @@ Resources: ValueFrom: !Sub 'arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${EnvironmentName}/INTERIORS_TOKEN' - Name: 'BERNARD_QUEUE_URL' ValueFrom: !Sub 'arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${EnvironmentName}/sqs/PHOTOWALL_BERNARD_TASKS_QUEUE_URL' + - Name: 'SAMPLE_SQS_QUEUE_URL' + ValueFrom: !Sub 'arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${EnvironmentName}/SAMPLE_RIPPER_QUEUE' - Name: 'NEW_RELIC_LICENSE_KEY' ValueFrom: !Sub 'arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/newrelic/LICENSE_KEY' # Send logs to CloudWatch Logs @@ -213,6 +219,10 @@ Resources: Action: - 'sqs:SendMessage' Resource: !Sub ${BernardQueueArn} + - Effect: Allow + Action: + - 'sqs:SendMessage' + Resource: !Sub ${SampleRipperQueueArn} # A role for the containers TaskRole: diff --git a/docker-compose-prodlike.yml b/docker-compose-prodlike.yml index d63205f..d49e542 100644 --- a/docker-compose-prodlike.yml +++ b/docker-compose-prodlike.yml @@ -23,6 +23,7 @@ services: - AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} - NODE_ENV=development - BERNARD_QUEUE_URL=${BERNARD_QUEUE_URL} + - SAMPLE_SQS_QUEUE_URL=${SAMPLE_SQS_QUEUE_URL} - NEW_RELIC_LICENSE_KEY=${NEW_RELIC_LICENSE_KEY} - NEW_RELIC_APP_NAME=${NEW_RELIC_APP_NAME} diff --git a/docker-compose.yml.example b/docker-compose.yml.example index 3da3082..1d8dc64 100644 --- a/docker-compose.yml.example +++ b/docker-compose.yml.example @@ -26,6 +26,7 @@ services: - AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} - NODE_ENV=development - BERNARD_QUEUE_URL=${BERNARD_QUEUE_URL} + - SAMPLE_SQS_QUEUE_URL=${SAMPLE_SQS_QUEUE_URL} - NEW_RELIC_LICENSE_KEY=${NEW_RELIC_LICENSE_KEY} - NEW_RELIC_APP_NAME=${NEW_RELIC_APP_NAME} # - DEBUG=knex:* diff --git a/src/aws/sqs.ts b/src/aws/sqs.ts new file mode 100644 index 0000000..b7f11f7 --- /dev/null +++ b/src/aws/sqs.ts @@ -0,0 +1,21 @@ +import { + SQSClient, + SendMessageCommand, + SendMessageCommandOutput, + SendMessageCommandInput, +} from '@aws-sdk/client-sqs'; + +export async function sendMessage( + body: string, + queueUrl: string, +): Promise { + const client = new SQSClient({ region: 'eu-west-1' }); + + const params: SendMessageCommandInput = { + MessageBody: body, + QueueUrl: queueUrl, + }; + const sendMessageCommand = new SendMessageCommand(params); + + return client.send(sendMessageCommand); +} diff --git a/src/bernard/client.ts b/src/bernard/client.ts index 5f264c4..a2be2c1 100644 --- a/src/bernard/client.ts +++ b/src/bernard/client.ts @@ -1,9 +1,4 @@ -import { - SQSClient, - SendMessageCommand, - SendMessageCommandOutput, - SendMessageCommandInput, -} from '@aws-sdk/client-sqs'; +import { sendMessage } from '../aws/sqs'; export async function sendPathChangedCmd( oldPath: string, @@ -20,18 +15,3 @@ export async function sendPathChangedCmd( }; await sendMessage(JSON.stringify(msg), process.env.BERNARD_QUEUE_URL); } - -export async function sendMessage( - body: string, - queueUrl: string, -): Promise { - const client = new SQSClient({ region: 'eu-west-1' }); - - const params: SendMessageCommandInput = { - MessageBody: body, - QueueUrl: queueUrl, - }; - const sendMessageCommand = new SendMessageCommand(params); - - return client.send(sendMessageCommand); -} diff --git a/src/resolvers/products-resolver.ts b/src/resolvers/products-resolver.ts index 552035b..4a4da96 100644 --- a/src/resolvers/products-resolver.ts +++ b/src/resolvers/products-resolver.ts @@ -23,6 +23,7 @@ import { PrintProductDefaultsAPI } from '../datasources/printproduct-defaults-ap import { PrintProductDefaults } from '../types/printproduct-defaults-types'; import { GraphQLError } from 'graphql'; import { getOrientationString, isRepeatingPattern } from '../datasources/utils'; +import { sendMessage } from '../aws/sqs'; const ProductBlacklist = { async market(parent, _args, { dataSources }) { @@ -223,7 +224,16 @@ export const productMutationTypeDefs = { batch, designerId, ); - return (dataSources.productApi).getProduct(id); + + const product = await (dataSources.productApi).getProduct(id); + + // Send sqs message to trigger sample ripper + await sendMessage( + JSON.stringify({ productId: id, artNo: product.fields.artNo }), + process.env.SAMPLE_SQS_QUEUE_URL, + ); + + return product; }, /**