diff --git a/src/actions/corn.ts b/src/actions/corn.ts index 0b7c395e..15b7b2d5 100644 --- a/src/actions/corn.ts +++ b/src/actions/corn.ts @@ -1,10 +1,20 @@ +// lib/cron.ts import cron from 'node-cron'; import { updateExpiredJobs } from './job.action'; -cron.schedule('0 * * * *', async () => { - try { - await updateExpiredJobs(); - } catch (error) { - console.error('Error updating expired jobs:', error); +let cronJobInitialized = false; + +export const startCronJob = () => { + if (!cronJobInitialized) { + cronJobInitialized = true; + + // Schedule the job to run at midnight (12:00 AM) every day + cron.schedule('0 0 * * *', async () => { + try { + await updateExpiredJobs(); + } catch (error) { + console.error('Error updating expired jobs:', error); + } + }); } -}); +}; diff --git a/src/app/page.tsx b/src/app/page.tsx index bd78e16e..c0a3335e 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,9 +1,11 @@ +import { startCronJob } from '@/actions/corn'; import Faqs from '@/components/Faqs'; import HeroSection from '@/components/hero-section'; import { JobLanding } from '@/components/job-landing'; import Testimonials from '@/components/Testimonials'; const HomePage = async () => { + startCronJob(); return (