restore node-cron + cron jobs
This commit is contained in:
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user