Files
phaseflow/src/instrumentation.ts
Petru Paler 1a9a327a30
Some checks failed
Deploy / deploy (push) Has been cancelled
Increase Garmin sync frequency to every 3 hours
Change cron schedule from 4x daily (0,6,12,18 UTC) to 8x daily
(every 3 hours) to keep body battery and other Garmin data fresher.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-22 12:57:59 +00:00

63 lines
2.0 KiB
TypeScript

// ABOUTME: Next.js instrumentation file for server startup initialization.
// ABOUTME: Schedules cron jobs for notifications and Garmin sync using node-cron.
export async function register() {
// Only run on the server side
if (process.env.NEXT_RUNTIME === "nodejs") {
const cron = await import("node-cron");
const APP_URL = process.env.APP_URL || "http://localhost:3000";
const CRON_SECRET = process.env.CRON_SECRET;
// Log startup
console.log("[cron] Scheduler starting...");
if (!CRON_SECRET) {
console.warn(
"[cron] CRON_SECRET not set - cron jobs will fail authentication",
);
}
// Helper to call cron endpoints
async function triggerCronEndpoint(endpoint: string, name: string) {
try {
const response = await fetch(`${APP_URL}/api/cron/${endpoint}`, {
method: "POST",
headers: {
Authorization: `Bearer ${CRON_SECRET}`,
"Content-Type": "application/json",
},
});
if (!response.ok) {
const errorText = await response.text();
console.error(
`[cron] ${name} failed: ${response.status} ${errorText}`,
);
} else {
const result = await response.json();
console.log(`[cron] ${name} completed:`, result);
}
} catch (error) {
console.error(`[cron] ${name} error:`, error);
}
}
// Schedule notifications every 15 minutes for finer-grained delivery times
cron.default.schedule("*/15 * * * *", () => {
console.log("[cron] Triggering notifications...");
triggerCronEndpoint("notifications", "Notifications");
});
// Schedule Garmin sync 8 times daily (every 3 hours) to keep data fresh
cron.default.schedule("0 */3 * * *", () => {
console.log("[cron] Triggering Garmin sync...");
triggerCronEndpoint("garmin-sync", "Garmin sync");
});
console.log(
"[cron] Scheduler started - notifications every 15 min, Garmin sync every 3 hours",
);
}
}