Skip to content

Commit f667a75

Browse files
test: analytics update test
1 parent 9b85313 commit f667a75

File tree

1 file changed

+50
-16
lines changed

1 file changed

+50
-16
lines changed

create-db/index.js

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,44 @@ const CREATE_DB_WORKER_URL =
1717
const CLAIM_DB_WORKER_URL =
1818
process.env.CLAIM_DB_WORKER_URL || "https://create-db.prisma.io";
1919

20+
// Track pending analytics promises to ensure they complete before exit
21+
const pendingAnalytics = [];
22+
2023
async function sendAnalyticsToWorker(eventName, properties) {
2124
const controller = new AbortController();
22-
const timer = setTimeout(() => controller.abort(), 2000);
23-
try {
24-
const payload = {
25-
eventName,
26-
properties: { distinct_id: CLI_RUN_ID, ...(properties || {}) },
27-
};
28-
await fetch(`${CREATE_DB_WORKER_URL}/analytics`, {
29-
method: "POST",
30-
headers: { "Content-Type": "application/json" },
31-
body: JSON.stringify(payload),
32-
signal: controller.signal,
33-
});
34-
} catch (error) {
35-
} finally {
36-
clearTimeout(timer);
37-
}
25+
const timer = setTimeout(() => controller.abort(), 5000);
26+
27+
const analyticsPromise = (async () => {
28+
try {
29+
const payload = {
30+
eventName,
31+
properties: { distinct_id: CLI_RUN_ID, ...(properties || {}) },
32+
};
33+
await fetch(`${CREATE_DB_WORKER_URL}/analytics`, {
34+
method: "POST",
35+
headers: { "Content-Type": "application/json" },
36+
body: JSON.stringify(payload),
37+
signal: controller.signal,
38+
});
39+
} catch (error) {
40+
// Silently fail - analytics shouldn't block CLI
41+
} finally {
42+
clearTimeout(timer);
43+
}
44+
})();
45+
46+
pendingAnalytics.push(analyticsPromise);
47+
return analyticsPromise;
48+
}
49+
50+
// Wait for all pending analytics with a timeout
51+
async function flushAnalytics(maxWaitMs = 500) {
52+
if (pendingAnalytics.length === 0) return;
53+
54+
const timeout = new Promise((resolve) => setTimeout(resolve, maxWaitMs));
55+
const allAnalytics = Promise.all(pendingAnalytics);
56+
57+
await Promise.race([allAnalytics, timeout]);
3858
}
3959

4060
async function detectUserLocation() {
@@ -135,6 +155,7 @@ async function isOffline() {
135155
`Check your internet connection or visit ${chalk.green("https://www.prisma-status.com/\n")}`
136156
)
137157
);
158+
await flushAnalytics();
138159
process.exit(1);
139160
}
140161
}
@@ -205,6 +226,7 @@ Examples:
205226
${chalk.gray(`npx ${CLI_NAME} --env --region us-east-1`)}
206227
${chalk.gray(`npx ${CLI_NAME} --env >> .env`)}
207228
`);
229+
await flushAnalytics();
208230
process.exit(0);
209231
}
210232

@@ -405,6 +427,7 @@ async function promptForRegion(defaultRegion, userAgent) {
405427

406428
if (region === null) {
407429
cancel(chalk.red("Operation cancelled."));
430+
await flushAnalytics();
408431
process.exit(0);
409432
}
410433

@@ -460,6 +483,7 @@ async function createDatabase(name, region, userAgent, silent = false) {
460483
"user-agent": userAgent,
461484
});
462485

486+
await flushAnalytics();
463487
process.exit(1);
464488
}
465489

@@ -489,6 +513,7 @@ async function createDatabase(name, region, userAgent, silent = false) {
489513
"user-agent": userAgent,
490514
});
491515

516+
await flushAnalytics();
492517
process.exit(1);
493518
}
494519

@@ -560,6 +585,7 @@ async function createDatabase(name, region, userAgent, silent = false) {
560585
"user-agent": userAgent,
561586
});
562587

588+
await flushAnalytics();
563589
process.exit(1);
564590
}
565591

@@ -667,6 +693,7 @@ export async function main() {
667693

668694
if (flags["list-regions"]) {
669695
await listRegions();
696+
await flushAnalytics();
670697
process.exit(0);
671698
}
672699

@@ -694,6 +721,7 @@ export async function main() {
694721
}
695722
const result = await createDatabase(name, region, userAgent, true);
696723
console.log(JSON.stringify(result, null, 2));
724+
await flushAnalytics();
697725
process.exit(0);
698726
} catch (e) {
699727
console.log(
@@ -703,6 +731,7 @@ export async function main() {
703731
2
704732
)
705733
);
734+
await flushAnalytics();
706735
process.exit(1);
707736
}
708737
}
@@ -717,13 +746,16 @@ export async function main() {
717746
const result = await createDatabase(name, region, userAgent, true);
718747
if (result.error) {
719748
console.error(result.message || "Unknown error");
749+
await flushAnalytics();
720750
process.exit(1);
721751
}
722752
console.log(`DATABASE_URL="${result.directConnectionString}"`);
723753
console.error("\n# Claim your database at: " + result.claimUrl);
754+
await flushAnalytics();
724755
process.exit(0);
725756
} catch (e) {
726757
console.error(e?.message || String(e));
758+
await flushAnalytics();
727759
process.exit(1);
728760
}
729761
}
@@ -746,8 +778,10 @@ export async function main() {
746778
await createDatabase(name, region, userAgent);
747779

748780
outro("");
781+
await flushAnalytics();
749782
} catch (error) {
750783
console.error("Error:", error.message);
784+
await flushAnalytics();
751785
process.exit(1);
752786
}
753787
}

0 commit comments

Comments
 (0)