-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: create wrapper for SQSClient #9
Changes from 3 commits
9d0971b
83e34ce
2f90385
0cbdaa6
71285f7
a05ac6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ logs | |
test-results.xml | ||
.env | ||
.idea/ | ||
.npmrc |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,25 +9,23 @@ | |||||||||
* OF ANY KIND, either express or implied. See the License for the specific language | ||||||||||
* governing permissions and limitations under the License. | ||||||||||
*/ | ||||||||||
const log = (level, message, ...args) => { | ||||||||||
const timestamp = new Date().toISOString(); | ||||||||||
|
||||||||||
switch (level) { | ||||||||||
case 'info': | ||||||||||
console.info(`[${timestamp}] INFO: ${message}`, ...args); | ||||||||||
break; | ||||||||||
case 'error': | ||||||||||
console.error(`[${timestamp}] ERROR: ${message}`, ...args); | ||||||||||
break; | ||||||||||
case 'warn': | ||||||||||
console.warn(`[${timestamp}] WARN: ${message}`, ...args); | ||||||||||
break; | ||||||||||
default: | ||||||||||
console.log(`[${timestamp}] ${message}`, ...args); | ||||||||||
break; | ||||||||||
} | ||||||||||
}; | ||||||||||
'use strict'; | ||||||||||
|
||||||||||
export { | ||||||||||
log, | ||||||||||
}; | ||||||||||
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 { log } = context; | ||||||||||
|
||||||||||
if (!queueUrl) { | ||||||||||
throw new Error('AUDIT_RESULTS_QUEUE_URL env variable is empty/not provided'); | ||||||||||
} | ||||||||||
|
||||||||||
context.queue = SqsQueue(region, queueUrl, log); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpicking: I would check if
Suggested change
see the suggestion below as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please note: that we store all extra (cached) objects in
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tripodsan thank you! @dzehnder let's stick to the same convention There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tripodsan could you give me access to helix-admin-support repo I don t seem to have it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We took a look and we will add the queue and storage to the context, but I think we shouldn t add it to the attributes, because I see the storage which is similar to db and queue is added directly to the context. Wdyt @tripodsan ? |
||||||||||
|
||||||||||
return func(request, context); | ||||||||||
}; | ||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,47 +9,35 @@ | |||||||||||||||||||||
* 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 | ||||||||||||||||||||||
let sqsClient; | ||||||||||||||||||||||
|
||||||||||||||||||||||
// 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'; | ||||||||||||||||||||||
export default function SQSQueue(region, queueUrl, log) { | ||||||||||||||||||||||
if (!sqsClient) { | ||||||||||||||||||||||
sqsClient = new SQSClient({ region }); | ||||||||||||||||||||||
log.info(`Creating SQS client in region ${region}`); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continues from the suggestion above
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
function SQSQueue() { | ||||||||||||||||||||||
async function sendMessage(message) { | ||||||||||||||||||||||
async function 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: 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); | ||||||||||||||||||||||
} 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 { sendAuditResult }; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
export default SQSQueue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
secrets
wrapper should be in outer level thenqueueWrapper
otherwisequeueWrapper
won't have access to theenv
variables