Skip to content

Commit 2bb4d65

Browse files
hiccup
1 parent 2986660 commit 2bb4d65

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

website/scripts/deploy-bunny.mjs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,45 @@ async function uploadFile(localPath, remotePath) {
2727

2828
async function purgeCache() {
2929
const purgePath = DEPLOY_PATH ? `${PULLZONE_URL}/${DEPLOY_PATH}/*` : `${PULLZONE_URL}/*`;
30-
const res = await fetch(`https://api.bunny.net/purge?url=${encodeURIComponent(purgePath)}`, {
31-
method: 'POST',
32-
headers: { AccessKey: API_KEY },
33-
});
34-
if (!res.ok) {
35-
const text = await res.text();
36-
throw new Error(`Purge failed: ${res.status} - ${text}`);
30+
const url = `https://api.bunny.net/purge?url=${encodeURIComponent(purgePath)}`;
31+
const maxAttempts = 5;
32+
33+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
34+
try {
35+
const controller = new AbortController();
36+
const timeout = setTimeout(() => controller.abort(), 30_000);
37+
const res = await fetch(url, {
38+
method: 'POST',
39+
headers: { AccessKey: API_KEY },
40+
signal: controller.signal,
41+
});
42+
clearTimeout(timeout);
43+
44+
if (res.ok) {
45+
console.log('✓ Cache purged');
46+
return;
47+
}
48+
49+
// Retry on 5xx and 429; fail fast on other 4xx (auth, bad URL, etc.)
50+
if (res.status < 500 && res.status !== 429) {
51+
const text = await res.text();
52+
throw new Error(`Purge failed: ${res.status} - ${text.slice(0, 200)}`);
53+
}
54+
console.warn(`Purge attempt ${attempt}/${maxAttempts} failed: ${res.status}`);
55+
} catch (err) {
56+
if (err.message?.startsWith('Purge failed:')) throw err;
57+
console.warn(`Purge attempt ${attempt}/${maxAttempts} error: ${err.message}`);
58+
}
59+
60+
if (attempt < maxAttempts) {
61+
const delay = Math.min(2000 * 2 ** (attempt - 1), 30_000);
62+
await new Promise((r) => setTimeout(r, delay));
63+
}
3764
}
38-
console.log('✓ Cache purged');
65+
66+
// Upload already succeeded; a failed purge only delays cache eviction.
67+
// Don't fail the deploy — warn loudly so the job stays green.
68+
console.warn('::warning::Cache purge failed after retries — new content will propagate as edge TTLs expire.');
3969
}
4070

4171
async function deploy() {

0 commit comments

Comments
 (0)