Cron Jobs
This document explains how scheduled tasks (cron jobs) are managed and initiated within the application.
Initiation
Cron job scheduling is primarily initiated within apps/web/instrumentation.ts during server startup (process.env.NEXT_RUNTIME === "nodejs"). This does not run on serverless-only hosts like Vercel without a Node server process.
- Enable Flag:
NEXT_PUBLIC_ENABLE_CRONinpackages/env/src/schemas/general.tsmust betrue. - Main Cron Function:
startMainCronfromapps/web/src/server/instrumentation/cron/scripts/main-cron.ts. - Conditional Start: Within
instrumentation.ts, the call tostartMainCron()is executed only ifNEXT_PUBLIC_ENABLE_CRONistrue, ensuring that the defined scheduled tasks begin running when the server starts.
Implementation
- Job Definitions: Individual jobs live under
apps/web/src/server/instrumentation/cron/, starting withmain-cron.ts. - Purpose: Cron jobs are typically used for recurring tasks like database cleanup, sending scheduled reports, syncing data, etc.
Refer to apps/web/src/server/instrumentation/cron/ for implementations and schedules.