v1.0
This commit is contained in:
@@ -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 * * *'
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user