-
Notifications
You must be signed in to change notification settings - Fork 1
/
cron.mjs
43 lines (35 loc) · 970 Bytes
/
cron.mjs
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
import axios from "axios";
import dotenv from "dotenv";
import cron from "node-cron";
import z from "zod";
dotenv.config();
const envSchema = z.object({
BASE_URL: z.string().url(),
GENERIC_SCHEDULER_AUTH_SECRET: z.string(),
});
const _env = envSchema.safeParse(process.env);
if (!_env.success) {
console.error("❌ Invalid environment variables:\n", _env.error.format());
throw new Error("Invalid environment variables");
}
const env = _env.data;
const EVERY_MINUTE = "* * * * *";
cron.schedule(EVERY_MINUTE, async () => {
console.log("running");
const url = `${env.BASE_URL}/api/cron/generic`;
console.log(`Executing ${url}`);
try {
const response = await axios.post(
url,
{},
{
headers: {
"cron-shared-secret": env.GENERIC_SCHEDULER_AUTH_SECRET,
},
}
);
console.log(`response.status: ${response.status}`);
} catch (error) {
console.error("An error occurred:", error);
}
});