Skip to content

Commit eb683af

Browse files
committed
Crons fixed
1 parent 4043ed0 commit eb683af

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

utils/cronJobs.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
import cron from 'node-cron'
22
import axios from 'axios'
33

4+
function getInternalBaseUrl(): string {
5+
if (process.env.PLANETARY_CLOUD_URL) {
6+
return (process.env.NEXTAUTH_URL || "http://localhost:3000").replace(/\/$/, "");
7+
}
8+
9+
if (process.env.NEXTAUTH_URL) {
10+
return process.env.NEXTAUTH_URL.replace(/\/$/, "");
11+
}
12+
13+
return "http://localhost:3000";
14+
}
15+
416
export async function initCronJobs() {
17+
const baseUrl = getInternalBaseUrl();
18+
519
try {
620
cron.schedule('* * * * *', async () => {
7-
await axios.post(`${process.env.NEXTAUTH_URL}/api/cron/update-sessions`)
21+
await axios.post(`${baseUrl}/api/cron/update-sessions`);
822
});
9-
1023
cron.schedule('0 * * * *', async () => {
11-
await axios.post(`${process.env.NEXTAUTH_URL}/api/cron/update-roles`)
24+
await axios.post(`${baseUrl}/api/cron/update-roles`);
1225
});
13-
1426
cron.schedule('0 0 * * *', async () => {
15-
await axios.post(`${process.env.NEXTAUTH_URL}/api/cron/birthday`)
27+
await axios.post(`${baseUrl}/api/cron/birthday`);
1628
});
17-
1829
cron.schedule('0 6 * * *', async () => {
19-
await axios.post(`${process.env.NEXTAUTH_URL}/api/cron/reset-activity`)
30+
await axios.post(`${baseUrl}/api/cron/reset-activity`);
2031
});
21-
2232
cron.schedule('* * * * *', async () => {
23-
await axios.post(`${process.env.NEXTAUTH_URL}/api/cron/milestone`)
33+
await axios.post(`${baseUrl}/api/cron/milestone`);
2434
});
2535
} catch (err) {
26-
console.log(`[CRON JOBS]: An error occured while running a cron job: ${err}`)
36+
console.log(`[CRON JOBS]: An error occured while running a cron job: ${err}`);
2737
}
2838

29-
console.log("[STARTUP] All crons scheduled.")
39+
console.log("[STARTUP] All crons scheduled.");
3040
}

0 commit comments

Comments
 (0)