-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworker.js
More file actions
33 lines (29 loc) · 1.03 KB
/
Copy pathworker.js
File metadata and controls
33 lines (29 loc) · 1.03 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
import { fetch as routerFetch } from "./routes/router.js";
export default {
async fetch(request, env) {
return routerFetch(request, env);
},
// Cron Trigger 处理函数 - 用于预热 Worker 防止冷启动
async scheduled(event, env, ctx) {
const warmupBaseUrl = env.WARMUP_BASE_URL?.trim();
if (!warmupBaseUrl) {
console.log("Warm-up skipped: WARMUP_BASE_URL is not configured");
return;
}
let url;
try {
url = new URL("/health", warmupBaseUrl).toString();
} catch (error) {
console.error(`Warm-up skipped: invalid WARMUP_BASE_URL: ${error.message}`);
return;
}
// 发送一个简单的健康检查请求到自己,保持 Worker 预热状态
// 这不会消耗任何 AI 额度,只是唤醒 Worker
try {
const response = await fetch(url, { method: "GET" });
console.log(`Warm-up ping completed at ${new Date().toISOString()}, status: ${response.status}`);
} catch (error) {
console.error(`Warm-up ping failed: ${error.message}`);
}
}
};