-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-worker.js
More file actions
41 lines (35 loc) · 1.15 KB
/
custom-worker.js
File metadata and controls
41 lines (35 loc) · 1.15 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
import nextWorker from "./.open-next/worker.js";
const CRON_PATHS = {
"0 */12 * * *": "/api/cron/snapshots",
"0 */4 * * *": "/api/cron/assets/sync",
"0 1/4 * * *": "/api/cron/investments/settle",
"0 2 * * *": "/api/cron/investments/expiry-reminders",
"0 14 * * *": "/api/cron/investments/expiry-reminders"
};
export default {
fetch: nextWorker.fetch,
async scheduled(controller, env, ctx) {
const path = CRON_PATHS[controller.cron];
if (!path || !env.CRON_SECRET) {
console.warn("Skipped scheduled job because Cloudflare cron config is incomplete.");
return;
}
const request = new Request(`https://earn-compass.local${path}`, {
headers: {
Authorization: `Bearer ${env.CRON_SECRET}`
}
});
ctx.waitUntil((async () => {
try {
const response = await nextWorker.fetch(request, env, ctx);
if (!response.ok) {
console.error(
`Scheduled job ${controller.cron} failed with ${response.status}: ${await response.text()}`
);
}
} catch (error) {
console.error(`Scheduled job ${controller.cron} failed.`, error);
}
})());
}
};