This commit is contained in:
lejay
2026-02-13 00:21:53 +01:00
parent 81d2412835
commit 6923c319a1
85 changed files with 17836 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
/**
* Scheduler service using node-cron
* Handles scheduled tasks like Formex sync
*/
import cron from 'node-cron';
import { sync as formexSync } from './formexCrawler.js';
let formexJob = null;
/**
* Initialize the scheduler with configured jobs
*/
export function initScheduler() {
const cronExpression = process.env.FORMEX_SYNC_CRON || '0 3 * * *'; // Default: 3:00 AM daily
// Validate cron expression
if (!cron.validate(cronExpression)) {
console.error(`Invalid FORMEX_SYNC_CRON expression: ${cronExpression}`);
return;
}
// Schedule Formex sync
formexJob = cron.schedule(cronExpression, async () => {
console.log(`[${new Date().toISOString()}] Running scheduled Formex sync...`);
try {
const result = await formexSync();
console.log(`[${new Date().toISOString()}] Scheduled sync complete:`, result);
} catch (err) {
console.error(`[${new Date().toISOString()}] Scheduled sync failed:`, err.message);
}
}, {
scheduled: true,
timezone: 'Europe/Zurich' // Swiss timezone
});
console.log(`Formex sync scheduled: ${cronExpression} (Europe/Zurich)`);
}
/**
* Stop all scheduled jobs
*/
export function stopScheduler() {
if (formexJob) {
formexJob.stop();
formexJob = null;
console.log('Scheduler stopped');
}
}
/**
* Get scheduler status
*/
export function getSchedulerStatus() {
return {
formexSync: {
running: formexJob !== null,
schedule: process.env.FORMEX_SYNC_CRON || '0 3 * * *'
}
};
}