Skip to content

Commit

Permalink
Merge branch 'sqs-wrapper' into init-db-wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
dzehnder committed Oct 20, 2023
2 parents 912dd28 + a05ac6f commit db9a7d4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ logs
test-results.xml
.env
.idea/
.npmrc
8 changes: 4 additions & 4 deletions src/db-wrapper.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.
*/

'use strict';

import DB from './db.js';

export default function dynamoDBWrapper(func) {
return async (request, context) => {
const { region } = context.runtime;
if (!region) {
throw Error('Region is undefined');
}
if (!context.db) {
context.db = new DB(context);
}

return func(request, context);
};
}
18 changes: 9 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,9 +12,9 @@
import secrets from '@adobe/helix-shared-secrets';
import wrap from '@adobe/helix-shared-wrap';
import { helixStatus } from '@adobe/helix-status';
import SQSQueue from './sqs-queue.js';
import dynamoDBWrapper from './db-wrapper.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 dynamoDBWrapper from './db-wrapper.js';
import PSIClient from './psi-client.js';
import queueWrapper from './queue-wrapper.js';

/**
* This is the main function
Expand All @@ -23,13 +23,12 @@ import PSIClient from './psi-client.js'; // Assuming the exported content of './
* @returns {Response} a response
*/
async function run(request, context) {
const { db } = context;
const sqsQueue = SQSQueue();
const { db, queue } = context;
const message = JSON.parse(context.invocation.event.Records[0].body);

const psiClient = PSIClient({
apiKey: process.env.PAGESPEED_API_KEY,
baseUrl: process.env.PAGESPEED_API_BASE_URL,
apiKey: context.env.PAGESPEED_API_KEY,
baseUrl: context.env.PAGESPEED_API_BASE_URL,
});

const site = {
Expand All @@ -38,11 +37,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 queue.sendAuditResult(auditResultMin);
return new Response('SUCCESS');
}

export const main = wrap(run)
.with(dynamoDBWrapper)
.with(queueWrapper)
.with(secrets)
.with(helixStatus);
33 changes: 33 additions & 0 deletions src/queue-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 SqsQueue from './sqs-queue.js';

export default function queueWrapper(func) {
return async (request, context) => {
const queueUrl = context.env.AUDIT_RESULTS_QUEUE_URL;

if (!queueUrl) {
throw new Error('AUDIT_RESULTS_QUEUE_URL env variable is empty/not provided');
}

context.attributes.queueUrl = queueUrl;

if (!context.queue) {
context.queue = new SqsQueue(context);
}

return func(request, context);
};
}
45 changes: 21 additions & 24 deletions src/sqs-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,44 @@
* 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, SQSClient } from '@aws-sdk/client-sqs';

// Set up the region
const REGION = 'us-east-1'; // change this to your desired region
/**
* @class SQSQueue class to send audit results to SQS
* @param {string} region - AWS region
* @param {string} queueUrl - SQS queue URL
* @param {object} log - OpenWhisk log object
*/
export default class SQSQueue {
constructor(context) {
const { region, log } = context;
const { queueUrl } = context.attributes;

// Create SQS service client object
const sqsClient = new SQSClient({ region: REGION });
this.queueUrl = queueUrl;
this.log = log;

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

function SQSQueue() {
async function sendMessage(message) {
async sendAuditResult(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: this.queueUrl,
};

try {
const data = await sqsClient.send(new SendMessageCommand(params));
console.log('Success, message sent. MessageID:', data.MessageId);
const data = await this.sqsClient.send(new SendMessageCommand(params));
this.log.info(`Success, message sent. MessageID: ${data.MessageId}`);
} catch (err) {
console.log('Error:', err);
this.log.error(`Error: ${err}`);
throw err;
}

return {
statusCode: 200,
body: JSON.stringify({ message: 'SQS message sent!' }),
};
}
return {
sendMessage,
};
}

export default SQSQueue;

0 comments on commit db9a7d4

Please sign in to comment.