-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarmup-functions.js
More file actions
executable file
·155 lines (137 loc) · 4.28 KB
/
Copy pathwarmup-functions.js
File metadata and controls
executable file
·155 lines (137 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node
/**
* Edge Function Warmup Script
*
* This script "warms up" Supabase Edge Functions by making multiple requests
* before the event starts. This keeps the functions in memory and reduces
* cold start latency when real users access the system.
*
* Usage:
* node warmup-functions.js <supabase-url> <anon-key>
*
* Example:
* node warmup-functions.js https://xxx.supabase.co your-anon-key
*
* Run this 5-10 minutes before the event starts, and optionally
* keep it running in the background during the event.
*/
const SUPABASE_URL = process.argv[2];
const ANON_KEY = process.argv[3];
if (!SUPABASE_URL || !ANON_KEY) {
console.error("Usage: node warmup-functions.js <supabase-url> <anon-key>");
console.error("");
console.error("Example:");
console.error(
" node warmup-functions.js https://xxx.supabase.co your-anon-key"
);
process.exit(1);
}
const ENDPOINTS = [
{
name: "entry_pass (resolve)",
url: `${SUPABASE_URL}/functions/v1/entry_pass`,
body: { action: "resolve", token: "warmup-token-will-fail-gracefully" },
},
{
name: "health_check",
url: `${SUPABASE_URL}/functions/v1/health_check`,
body: {},
},
];
// Statistics
let totalRequests = 0;
let successfulWarmups = 0;
let isRunning = true;
async function warmupEndpoint(endpoint) {
try {
const startTime = Date.now();
const response = await fetch(endpoint.url, {
method: "POST",
headers: {
"Content-Type": "application/json",
apikey: ANON_KEY,
Authorization: `Bearer ${ANON_KEY}`,
},
body: JSON.stringify(endpoint.body),
});
const duration = Date.now() - startTime;
totalRequests++;
// For warmup purposes, we don't care if the request "succeeds"
// We just want to trigger the function to keep it warm
// Even 400/403 errors mean the function is running
if (response.status < 500) {
successfulWarmups++;
console.log(
`✅ ${endpoint.name}: Warmed (${duration}ms, HTTP ${response.status})`
);
} else {
console.log(
`⚠️ ${endpoint.name}: Server error (${duration}ms, HTTP ${response.status})`
);
}
return { success: true, duration };
} catch (error) {
totalRequests++;
console.log(`❌ ${endpoint.name}: Error - ${error.message}`);
return { success: false, error: error.message };
}
}
async function warmupCycle() {
console.log(
"\n" + new Date().toLocaleTimeString() + " - Starting warmup cycle..."
);
// Warm up all endpoints concurrently
await Promise.all(ENDPOINTS.map((endpoint) => warmupEndpoint(endpoint)));
console.log(
`Cycle complete. Total: ${totalRequests} requests, ${successfulWarmups} successful\n`
);
}
async function continuousWarmup() {
console.log("=".repeat(70));
console.log("EDGE FUNCTION WARMUP");
console.log("=".repeat(70));
console.log(`Target: ${SUPABASE_URL}`);
console.log(`Functions: ${ENDPOINTS.map((e) => e.name).join(", ")}`);
console.log("");
console.log(
"This script will make periodic requests to keep Edge Functions warm."
);
console.log("Press Ctrl+C to stop.");
console.log("=".repeat(70));
console.log("");
// Initial burst: 5 warmup cycles with 1 second delay
console.log("Initial warmup burst (5 cycles)...");
for (let i = 0; i < 5; i++) {
await warmupCycle();
if (i < 4) await new Promise((resolve) => setTimeout(resolve, 1000));
}
console.log("✅ Initial warmup complete!");
console.log("");
console.log("Entering maintenance mode (warmup every 30 seconds)...");
console.log(
"Keep this script running during the event for best performance."
);
console.log("");
// Continuous warmup: every 30 seconds
while (isRunning) {
await new Promise((resolve) => setTimeout(resolve, 30000)); // 30 seconds
if (isRunning) {
await warmupCycle();
}
}
}
// Handle Ctrl+C gracefully
process.on("SIGINT", () => {
console.log("\n\nStopping warmup script...");
console.log(`Total requests sent: ${totalRequests}`);
console.log(`Successful warmups: ${successfulWarmups}`);
console.log("");
console.log("Edge Functions should stay warm for a few more minutes.");
isRunning = false;
process.exit(0);
});
// Run the warmup
continuousWarmup().catch((error) => {
console.error("Fatal error:", error);
process.exit(1);
});