From a47cc0e26f797d68e25dadaa09c1b3fdbd913aef Mon Sep 17 00:00:00 2001 From: Paribesh Nepal Date: Mon, 14 Oct 2024 23:46:39 +0530 Subject: [PATCH] feat: added logic to run cron job --- src/actions/corn.ts | 22 ++++++++++++++++------ src/app/page.tsx | 2 ++ 2 files changed, 18 insertions(+), 6 deletions(-) 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 (