From 4d86eff038aed87b7181f3cbf4909995ccfcf08b Mon Sep 17 00:00:00 2001 From: sumit-plena Date: Tue, 7 Jul 2026 17:08:50 +0530 Subject: [PATCH] restore node-cron + cron jobs --- backend/package.json | 3 ++- backend/src/index.ts | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/backend/package.json b/backend/package.json index ff65b52..bd49e07 100644 --- a/backend/package.json +++ b/backend/package.json @@ -11,7 +11,8 @@ "dependencies": { "cors": "^2.8.5", "dotenv": "^17.2.0", - "express": "^5.1.0" + "express": "^5.1.0", + "node-cron": "^4.5.0" }, "devDependencies": { "@types/cors": "^2.8.19", diff --git a/backend/src/index.ts b/backend/src/index.ts index cb2e6cc..4c514d7 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -2,6 +2,7 @@ import 'dotenv/config'; import path from 'path'; import { fileURLToPath } from 'url'; import cors from 'cors'; +import cron from 'node-cron'; import express, { type NextFunction, type Request, type Response } from 'express'; import { buildApiPath, getConfig } from './config'; import { TursoClient } from './lib/db'; @@ -22,6 +23,14 @@ const apiRoot = config.apiPrefix || '/'; app.use(apiRoot, createHealthRouter(config)); app.use(apiRoot, createTodosRouter(config, db)); +cron.schedule('*/3 * * * *', () => { + console.log('[Cron A] Heartbeat executing every 3 minutes: ' + new Date().toISOString()); +}); + +cron.schedule('*/10 * * * *', () => { + console.log('[Cron B] Heartbeat executing every 10 minutes: ' + new Date().toISOString()); +}); + app.post('/_cron/tick', (req, res) => { const token = req.headers.authorization?.replace(/^Bearer\s+/i, '') || ''; const expected = process.env.CRON_TOKEN || ''; @@ -29,7 +38,8 @@ app.post('/_cron/tick', (req, res) => { res.status(401).json({ success: false, error: 'Invalid cron token' }); return; } - res.status(200).json({ success: true, ticked: new Date().toISOString() }); + console.log('[Tick] Heartbeat received. Waking up event loop.'); + res.status(200).json({ success: true }); }); const publicDir = path.join(__dirname, '../public');