Skip to content

Commit

Permalink
fix: PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
dzehnder committed Oct 19, 2023
1 parent 9d0971b commit 83e34ce
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 68 deletions.
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { logger } from '@adobe/helix-universal-logger';
import { helixStatus } from '@adobe/helix-status';
import DB from './db.js';
import PSIClient from './psi-client.js';
import { SQSWrapper } from './sqs-wrapper.js';
import { queueWrapper } from './queue-wrapper.js';

/**
* This is the main function
Expand Down Expand Up @@ -43,13 +43,13 @@ async function run(request, context) {
};
const auditResult = await psiClient.runAudit(`https://${site.domain}/${site.path}`);
const auditResultMin = await db.saveAuditIndex(site, auditResult);
await context.sqsQueue.sendMessage(auditResultMin);
await context.queue.sendAuditResult(auditResultMin);
return new Response('SUCCESS');
}

export const main = wrap(run)
.with(SQSWrapper)
.with(helixStatus)
.with(logger.trace)
.with(logger)
.with(secrets);
.with(secrets)
.with(queueWrapper);
23 changes: 7 additions & 16 deletions src/sqs-wrapper.js → src/queue-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,25 @@

'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) {
function queueWrapper(func) {
return async (request, context) => {
const region = process.env.AWS_REGION;
const queueUrl = process.env.QUEUE_URL;
const queueUrl = process.env.AUDIT_RESULTS_QUEUE_URL;
const { log } = context;

if (!region) {
throw new Error('region is required');
throw new Error('AWS_REGION env variable is empty/not provided');
}
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 });
throw new Error('AUDIT_RESULTS_QUEUE_URL env variable is empty/not provided');
}

context.sqsQueue = SqsQueue(sqsClient, queueUrl);
context.queue = SqsQueue(region, queueUrl, log);

return func(request, context);
};
}

export default SQSWrapper;
export default queueWrapper;
26 changes: 11 additions & 15 deletions src/sqs-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { SendMessageCommand } from '@aws-sdk/client-sqs';
import { log } from './util.js';
import { SendMessageCommand, SQSClient } from '@aws-sdk/client-sqs';

// Set up the region
const REGION = 'us-east-1'; // change this to your desired region
let sqsClient;

// Your SQS queue URL
const queueURL = 'https://sqs.us-east-1.amazonaws.com/282898975672/spacecat-audit-results';
function SQSQueue(region, queueUrl, log) {
if (!sqsClient) {
sqsClient = new SQSClient({ region });
log.info(`Creating SQS client in region ${region}`);
}

function SQSQueue(sqsClient, queueUrl) {
async function sendMessage(message) {
async function sendAuditResult(message) {
const body = {
message,
timestamp: new Date().toISOString(),
Expand All @@ -33,17 +33,13 @@ function SQSQueue(sqsClient, queueUrl) {

try {
const data = await sqsClient.send(new SendMessageCommand(params));
log('info', 'Success, message sent. MessageID:', data.MessageId);
return {
statusCode: 200,
body: JSON.stringify({ message: 'SQS message sent!' }),
};
log.info('Success, message sent. MessageID:', data.MessageId);
} catch (err) {
log('error', 'Error:', err);
log.error('Error:', err);
throw err;
}
}
return { sendMessage };
return { sendAuditResult };
}

export default SQSQueue;
33 changes: 0 additions & 33 deletions src/util.js

This file was deleted.

0 comments on commit 83e34ce

Please sign in to comment.