// Copyright Abridged, Inc. 2021,2024. All Rights Reserved. // Node module: @collabland/aws // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT import {Message} from '@aws-sdk/client-sqs'; import readline from 'readline'; import {inspect} from 'util'; import {getSQSQueueUrl} from '../aws-clients/index.js'; import {loadSecrets, SQSServiceClient} from '../index.js'; import {isMain} from '@collabland/common'; function buildPrompt() { const question = (query?: string) => { return new Promise((resolve, reject) => { const msg = query ?? 'Please press Enter to continue:'; process.stdout.write(msg + ' '); const tty = readline.createInterface(process.stdin); tty.question(msg, answer => { resolve(answer); tty.close(); }); }); }; return question; } const JOB_ERROR_QUEUE = getSQSQueueUrl('collabland-job-error-prod'); function printMessage( msg: Message, queue: string, errors: Record = {}, log = true, ) { if (queue !== JOB_ERROR_QUEUE) { console.log(inspect(msg, false, null, true)); return; } const res = JSON.parse(msg.Body!); /* const jobRes = res.body; if (jobRes?.request != null) { delete jobRes.request.headers; } */ if (res.body?.errors) { // eslint-disable-next-line @typescript-eslint/no-explicit-any res.body.errors.forEach((err: any) => { const key = err.details?.message; if (key != null) { errors[key] = errors[key] ?? 0; errors[key] += 1; } }); } if (log) { console.log(inspect(res, false, null, true)); } } async function main() { await loadSecrets(); const sqs = new SQSServiceClient(); let queue = process.argv[2] ?? JOB_ERROR_QUEUE; let print = true; let total = Infinity; if (!isNaN(parseInt(queue))) { total = parseInt(queue); queue = JOB_ERROR_QUEUE; print = false; } console.log('Fetching %s messages from %s', total, queue); const prompt = buildPrompt(); const errors: Record = {}; let count = 0; for await (const msg of sqs.fetchMessages(queue)) { count++; printMessage(msg, queue, errors, print); if (count >= total) break; if (total === Infinity && count % 10 === 0) { const answer = await prompt( 'Press Enter to read 10 more messages (0 to exit):', ); if (answer === '0') break; } } console.log(errors); } if (isMain(import.meta.url)) { await main(); }