|
1 | 1 | import cron from 'node-cron' |
2 | 2 | import axios from 'axios' |
3 | 3 |
|
| 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 | + |
4 | 16 | export async function initCronJobs() { |
| 17 | + const baseUrl = getInternalBaseUrl(); |
| 18 | + |
5 | 19 | try { |
6 | 20 | cron.schedule('* * * * *', async () => { |
7 | | - await axios.post(`${process.env.NEXTAUTH_URL}/api/cron/update-sessions`) |
| 21 | + await axios.post(`${baseUrl}/api/cron/update-sessions`); |
8 | 22 | }); |
9 | | - |
10 | 23 | 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`); |
12 | 25 | }); |
13 | | - |
14 | 26 | cron.schedule('0 0 * * *', async () => { |
15 | | - await axios.post(`${process.env.NEXTAUTH_URL}/api/cron/birthday`) |
| 27 | + await axios.post(`${baseUrl}/api/cron/birthday`); |
16 | 28 | }); |
17 | | - |
18 | 29 | 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`); |
20 | 31 | }); |
21 | | - |
22 | 32 | cron.schedule('* * * * *', async () => { |
23 | | - await axios.post(`${process.env.NEXTAUTH_URL}/api/cron/milestone`) |
| 33 | + await axios.post(`${baseUrl}/api/cron/milestone`); |
24 | 34 | }); |
25 | 35 | } 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}`); |
27 | 37 | } |
28 | 38 |
|
29 | | - console.log("[STARTUP] All crons scheduled.") |
| 39 | + console.log("[STARTUP] All crons scheduled."); |
30 | 40 | } |
0 commit comments