fix(network): set up retries to fix network failure issues

This commit is contained in:
ryan 2025-04-17 02:12:56 +03:00
parent b01a40a1fd
commit 17c110934f
3 changed files with 25 additions and 5 deletions

View file

@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node src/server.js"
"start": "NODE_OPTIONS='--dns-result-order=ipv4first' node src/server.js"
},
"author": "ryan",
"license": "ISC",

View file

@ -25,16 +25,31 @@ export async function submitToListmonk(req, res) {
console.log('Sending request to Listmonk with payload:', payload);
async function fetchWithRetry(url, options, retries = 3) {
for (let i = 0; i < retries; i++) {
console.log(`Attempt ${i + 1} sending request to ${url}`);
try {
const res = await fetch(url, options);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res;
} catch (err) {
console.log(`Attempt ${i + 1} failed: ${err}`);
if (i === retries - 1) throw err;
await new Promise(resolve => setTimeout(resolve, 500));
}
}
}
try {
// Add subscriber
const response = await fetch(listmonkUrl, {
const response = await fetchWithRetry(listmonkUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${auth}`
},
body: JSON.stringify(payload),
});
},3);
if (!response.ok) {
const errorBody = await response.text();
@ -64,13 +79,13 @@ export async function submitToListmonk(req, res) {
},
body: JSON.stringify(statusPayload),
});
if (!statusResponse.ok) {
const errorBody = await statusResponse.text();
console.error('Response from Listmonk:', errorBody);
throw new Error(`Listmonk API error (Update Campaign Status): ${statusResponse.statusText}. Response body: ${errorBody}`);
}
const statusResult = await statusResponse.json();
console.log('Campaign status updated successfully:', statusResult);
} catch (error) {

5
testDns.js Normal file
View file

@ -0,0 +1,5 @@
import dns from 'dns';
dns.lookup('mailer.melonion.me', { all: true }, (err, addresses) => {
console.log(addresses);
});