restore node-cron + cron jobs
This commit is contained in:
@@ -11,7 +11,8 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^17.2.0",
|
"dotenv": "^17.2.0",
|
||||||
"express": "^5.1.0"
|
"express": "^5.1.0",
|
||||||
|
"node-cron": "^4.5.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/cors": "^2.8.19",
|
"@types/cors": "^2.8.19",
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'dotenv/config';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
|
import cron from 'node-cron';
|
||||||
import express, { type NextFunction, type Request, type Response } from 'express';
|
import express, { type NextFunction, type Request, type Response } from 'express';
|
||||||
import { buildApiPath, getConfig } from './config';
|
import { buildApiPath, getConfig } from './config';
|
||||||
import { TursoClient } from './lib/db';
|
import { TursoClient } from './lib/db';
|
||||||
@@ -22,6 +23,14 @@ const apiRoot = config.apiPrefix || '/';
|
|||||||
app.use(apiRoot, createHealthRouter(config));
|
app.use(apiRoot, createHealthRouter(config));
|
||||||
app.use(apiRoot, createTodosRouter(config, db));
|
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) => {
|
app.post('/_cron/tick', (req, res) => {
|
||||||
const token = req.headers.authorization?.replace(/^Bearer\s+/i, '') || '';
|
const token = req.headers.authorization?.replace(/^Bearer\s+/i, '') || '';
|
||||||
const expected = process.env.CRON_TOKEN || '';
|
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' });
|
res.status(401).json({ success: false, error: 'Invalid cron token' });
|
||||||
return;
|
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');
|
const publicDir = path.join(__dirname, '../public');
|
||||||
|
|||||||
Reference in New Issue
Block a user