add retry logic for send batch event
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import { randomUUID } from 'crypto';
|
import { randomUUID } from 'crypto';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import os from 'os';
|
||||||
|
import path from 'path';
|
||||||
import readline from 'readline';
|
import readline from 'readline';
|
||||||
import zlib from 'zlib';
|
import zlib from 'zlib';
|
||||||
import Progress from 'progress';
|
import Progress from 'progress';
|
||||||
@@ -323,7 +325,7 @@ async function sendBatchToAPI(
|
|||||||
clientSecret: string;
|
clientSecret: string;
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
try {
|
async function request() {
|
||||||
const res = await fetch(`${apiUrl}/import/events`, {
|
const res = await fetch(`${apiUrl}/import/events`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@@ -335,13 +337,28 @@ async function sendBatchToAPI(
|
|||||||
body: zlib.gzipSync(JSON.stringify(batch)),
|
body: zlib.gzipSync(JSON.stringify(batch)),
|
||||||
});
|
});
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
console.log('Failed to send batch to API');
|
throw new Error(`Failed to send batch: ${await res.text()}`);
|
||||||
console.log(await res.text());
|
|
||||||
}
|
}
|
||||||
await new Promise((resolve) => setTimeout(resolve, SLEEP_TIME));
|
await new Promise((resolve) => setTimeout(resolve, SLEEP_TIME));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await request();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('sendBatchToAPI failed');
|
console.log('Error sending batch, retrying...');
|
||||||
throw e;
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||||
|
try {
|
||||||
|
await request();
|
||||||
|
} catch (e) {
|
||||||
|
console.log('Error sending batch, skipping...');
|
||||||
|
fs.writeFileSync(
|
||||||
|
path.join(
|
||||||
|
os.tmpdir(),
|
||||||
|
`openpanel/failed-import-batch-${batch[0]?.created_at ? new Date(batch[0]?.created_at).toISOString() : Date.now()}.json`
|
||||||
|
),
|
||||||
|
JSON.stringify(batch, null, 2)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user