From 9d0971bf43ff38ae79b52704f862d1f016cedb9c Mon Sep 17 00:00:00 2001 From: Damian Zehnder Date: Thu, 19 Oct 2023 11:18:42 +0200 Subject: [PATCH] feat: create wrapper for SQSClient --- .gitignore | 1 + src/index.js | 12 ++++++------ src/sqs-queue.js | 28 +++++++++++----------------- src/sqs-wrapper.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 src/sqs-wrapper.js diff --git a/.gitignore b/.gitignore index 4562b131..76f81824 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ logs test-results.xml .env .idea/ +.npmrc diff --git a/src/index.js b/src/index.js index d0c80753..47d99b7b 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Adobe. All rights reserved. + * Copyright 2023 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 @@ -13,9 +13,9 @@ import secrets from '@adobe/helix-shared-secrets'; import wrap from '@adobe/helix-shared-wrap'; import { logger } from '@adobe/helix-universal-logger'; import { helixStatus } from '@adobe/helix-status'; -import SQSQueue from './sqs-queue.js'; -import DB from './db.js'; // Assuming the exported content of './db' is default exported -import PSIClient from './psi-client.js'; // Assuming the exported content of './psi-client' is default exported +import DB from './db.js'; +import PSIClient from './psi-client.js'; +import { SQSWrapper } from './sqs-wrapper.js'; /** * This is the main function @@ -27,7 +27,6 @@ async function run(request, context) { const db = DB({ region: process.env.REGION, }); - const sqsQueue = SQSQueue(); const { message } = JSON.parse(context.invocation.event.Records[0].body); const psiClient = PSIClient({ @@ -44,11 +43,12 @@ async function run(request, context) { }; const auditResult = await psiClient.runAudit(`https://${site.domain}/${site.path}`); const auditResultMin = await db.saveAuditIndex(site, auditResult); - await sqsQueue.sendMessage(auditResultMin); + await context.sqsQueue.sendMessage(auditResultMin); return new Response('SUCCESS'); } export const main = wrap(run) + .with(SQSWrapper) .with(helixStatus) .with(logger.trace) .with(logger) diff --git a/src/sqs-queue.js b/src/sqs-queue.js index 0278c8b5..fb84d460 100644 --- a/src/sqs-queue.js +++ b/src/sqs-queue.js @@ -9,47 +9,41 @@ * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ -import { SQSClient, SendMessageCommand } from '@aws-sdk/client-sqs'; +import { SendMessageCommand } from '@aws-sdk/client-sqs'; +import { log } from './util.js'; // Set up the region const REGION = 'us-east-1'; // change this to your desired region -// Create SQS service client object -const sqsClient = new SQSClient({ region: REGION }); - // Your SQS queue URL const queueURL = 'https://sqs.us-east-1.amazonaws.com/282898975672/spacecat-audit-results'; -function SQSQueue() { +function SQSQueue(sqsClient, queueUrl) { async function sendMessage(message) { const body = { message, timestamp: new Date().toISOString(), }; - // Set up the parameters for the send message command const params = { DelaySeconds: 10, MessageBody: JSON.stringify(body), - QueueUrl: queueURL, + QueueUrl: queueUrl, }; try { const data = await sqsClient.send(new SendMessageCommand(params)); - console.log('Success, message sent. MessageID:', data.MessageId); + log('info', 'Success, message sent. MessageID:', data.MessageId); + return { + statusCode: 200, + body: JSON.stringify({ message: 'SQS message sent!' }), + }; } catch (err) { - console.log('Error:', err); + log('error', 'Error:', err); throw err; } - - return { - statusCode: 200, - body: JSON.stringify({ message: 'SQS message sent!' }), - }; } - return { - sendMessage, - }; + return { sendMessage }; } export default SQSQueue; diff --git a/src/sqs-wrapper.js b/src/sqs-wrapper.js new file mode 100644 index 00000000..6ddcebc8 --- /dev/null +++ b/src/sqs-wrapper.js @@ -0,0 +1,45 @@ +/* + * Copyright 2023 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +'use strict'; + +import { SQSClient } from '@aws-sdk/client-sqs'; +import { log } from './util.js'; +import SqsQueue from './sqs-queue.js'; + +let sqsClient; + +function SQSWrapper(func) { + return async (request, context) => { + const region = process.env.AWS_REGION; + const queueUrl = process.env.QUEUE_URL; + + if (!region) { + throw new Error('region is required'); + } + if (!queueUrl) { + throw new Error('queueUrl is required'); + } + + // Initialize the SQSClient only if it hasn't been initialized yet + if (!sqsClient) { + log('info', `Creating SQS client in region ${region}`); + sqsClient = new SQSClient({ region }); + } + + context.sqsQueue = SqsQueue(sqsClient, queueUrl); + + return func(request, context); + }; +} + +export default SQSWrapper;