OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
add retry logic for send batch event
Carl-Gerhard Lindesvärd1405acc4f675
diff --git a/packages/cli/src/importer/importer.ts b/packages/cli/src/importer/importer.tsindex b83629653..ae3c150bb 100644--- a/packages/cli/src/importer/importer.ts+++ b/packages/cli/src/importer/importer.ts@@ -1,5 +1,7 @@import { randomUUID } from 'crypto';import fs from 'fs';+import os from 'os';+import path from 'path';import readline from 'readline';import zlib from 'zlib';import Progress from 'progress';@@ -323,7 +325,7 @@ async function sendBatchToAPI(clientSecret: string;}) {- try {+ async function request() {const res = await fetch(`${apiUrl}/import/events`, {method: 'POST',headers: {@@ -335,13 +337,28 @@ async function sendBatchToAPI(body: zlib.gzipSync(JSON.stringify(batch)),});if (!res.ok) {- console.log('Failed to send batch to API');- console.log(await res.text());+ throw new Error(`Failed to send batch: ${await res.text()}`);}await new Promise((resolve) => setTimeout(resolve, SLEEP_TIME));+ }++ try {+ await request();} catch (e) {- console.log('sendBatchToAPI failed');- throw e;+ console.log('Error sending batch, retrying...');+ 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)+ );+ }}}