Last active
December 13, 2022 12:57
-
-
Save leosantosw/b50771a287e342eee7547f392034059c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios') | |
const sqs = require('./sqs-client') | |
const VTEX_URL_QUEUE = process.env.VTEX_QUEUE_URL | |
const DLQ_URL = process.env.VTEX_DLQ_URL | |
const MAX_RETRY_REQUEST = 3 | |
const processRetry = async (retry, error, payload) => { | |
if (MAX_RETRY_REQUEST >= retry) { | |
return true | |
} | |
await sqs.sendMessage({ | |
queueURL: DLQ_URL, | |
message: { error, payload, retry } | |
}) | |
return false | |
} | |
exports.handler = handler(async event => { | |
const [ record ] = event.Records | |
const { | |
retry = 0, | |
error = '', | |
...user | |
} = JSON.parse(record.body) | |
console.info('Retry: ', retry) | |
const canRetry = await processRetry(retry, error, user) | |
if (!canRetry) { | |
return responses.unprocessableEntity({ message: 'Max retry reached' }) | |
} | |
try { | |
console.log('Try to call API...') | |
const { data } = await axios.get(`https://httpstat.us/${retry === 2 ? '200' : '500'}`) | |
console.log('Response URL: ', data) | |
} catch (error) { | |
console.log('has an error in the request...') | |
const message = { ...event, error, retry: retry + 1 } | |
await sqs.sendMessage({ | |
queueURL: VTEX_URL_QUEUE, | |
delaySeconds: 30, // reprocess message after 30 seconds | |
message, | |
}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment