diff --git a/src/index.js b/src/index.js index d1b2db7c..dfe3e508 100644 --- a/src/index.js +++ b/src/index.js @@ -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, @@ -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); diff --git a/src/queue-wrapper.js b/src/queue-wrapper.js index 82b44973..ccff8302 100644 --- a/src/queue-wrapper.js +++ b/src/queue-wrapper.js @@ -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) { diff --git a/src/sqs-queue.js b/src/sqs-queue.js index 31a87520..2a3ff0d0 100644 --- a/src/sqs-queue.js +++ b/src/sqs-queue.js @@ -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(), @@ -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;