Skip to content

Commit

Permalink
feat: change SQS queue from functional to class component
Browse files Browse the repository at this point in the history
  • Loading branch information
dzehnder committed Oct 20, 2023
1 parent 0cbdaa6 commit 71285f7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ import queueWrapper from './queue-wrapper.js';
/**
* This is the main function
* @param {Request} request the request object (see fetch api)
* @param {UniversalContext} context the context of the universal serverless function
* @param {UniversalContext} context the context of the universal serverle ss function
* @returns {Response} a response
*/
async function run(request, context) {
const db = DB({
region: context.env.REGION,
});
const { message } = JSON.parse(context.invocation.event.Records[0].body);
const message = JSON.parse(context.invocation.event.Records[0].body);
console.error('###body', context.invocation.event.Records[0].body);
console.error('###message', message);

const psiClient = PSIClient({
apiKey: context.env.PAGESPEED_API_KEY,
Expand All @@ -49,7 +51,7 @@ async function run(request, context) {

export const main = wrap(run)
.with(helixStatus)
.with(queueWrapper)
.with(logger.trace)
.with(logger)
.with(queueWrapper)
.with(secrets);
4 changes: 2 additions & 2 deletions src/queue-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import SqsQueue from './sqs-queue.js';

export default function queueWrapper(func) {
return async (request, context) => {
const region = context.env.AWS_REGION;
const queueUrl = context.env.AUDIT_RESULTS_QUEUE_URL;
const { region } = context.runtime;
const queueUrl = process.env.AUDIT_RESULTS_QUEUE_URL;
const { log } = context;

if (!queueUrl) {
Expand Down
32 changes: 21 additions & 11 deletions src/sqs-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@
*/
import { SendMessageCommand, SQSClient } from '@aws-sdk/client-sqs';

let sqsClient;
/**
* @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
*/
class SQSQueue {
constructor(region, queueUrl, log) {
if (!this.sqsClient) {
this.sqsClient = new SQSClient({ region });
log.info(`Creating SQS client in region ${region}`);
}

export default function SQSQueue(region, queueUrl, log) {
if (!sqsClient) {
sqsClient = new SQSClient({ region });
log.info(`Creating SQS client in region ${region}`);
this.queueUrl = queueUrl;
this.log = log;
}

async function sendAuditResult(message) {
async sendAuditResult(message) {
const body = {
message,
timestamp: new Date().toISOString(),
Expand All @@ -28,16 +37,17 @@ export default function SQSQueue(region, queueUrl, log) {
const params = {
DelaySeconds: 10,
MessageBody: JSON.stringify(body),
QueueUrl: queueUrl,
QueueUrl: this.queueUrl,
};

try {
const data = await sqsClient.send(new SendMessageCommand(params));
log.info('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) {
log.error('Error:', err);
this.log.error(`Error: ${err}`);
throw err;
}
}
return { sendAuditResult };
}

export default SQSQueue;

0 comments on commit 71285f7

Please sign in to comment.