restore node-cron + cron jobs

This commit is contained in:
sumit-plena
2026-07-07 17:08:50 +05:30
parent 6068b19fa5
commit 4d86eff038
2 changed files with 13 additions and 2 deletions

View File

@@ -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",

View File

@@ -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');