All checks were successful
Docker Build and Push / build-image (push) Successful in 2m53s
79 lines
2.5 KiB
JavaScript
79 lines
2.5 KiB
JavaScript
import cron from 'node-cron';
|
|
import { trigger as triggerUSDCNYRate } from '../scraper/usd-cny-rate.scraper.js';
|
|
import { trigger as triggerSH300Data } from '../scraper/sh300.scraper.js';
|
|
import { insertCronAlert } from '../lib/cron-alert.js';
|
|
|
|
console.log('='.repeat(70));
|
|
console.log('🚀 启动定时任务调度器');
|
|
console.log('='.repeat(70));
|
|
|
|
// 注册定时任务 - USD-CNY 汇率数据 (每天 6:20, 出错生成报警)
|
|
const jobUSDCNY = cron.schedule('20 6 * * *', async () => {
|
|
console.log('\n' + '='.repeat(70));
|
|
console.log('⏰ 执行定时任务: USD-CNY 汇率数据更新');
|
|
console.log('📅 执行时间:', new Date().toISOString());
|
|
console.log('='.repeat(70));
|
|
|
|
try {
|
|
await triggerUSDCNYRate();
|
|
console.log('\n✅ 任务执行成功!');
|
|
} catch (error) {
|
|
console.error('\n❌ 任务执行失败:', error.message);
|
|
await insertCronAlert('USD-CNY 汇率更新', error.message);
|
|
} finally {
|
|
console.log('='.repeat(70));
|
|
}
|
|
}, {
|
|
scheduled: true,
|
|
timezone: 'Asia/Shanghai'
|
|
});
|
|
|
|
// 注册定时任务 - 沪深300指数数据 (每天 6:30, 出错生成报警)
|
|
const jobSH300 = cron.schedule('30 6 * * *', async () => {
|
|
console.log('\n' + '='.repeat(70));
|
|
console.log('⏰ 执行定时任务: 沪深300指数数据更新');
|
|
console.log('📅 执行时间:', new Date().toISOString());
|
|
console.log('='.repeat(70));
|
|
|
|
try {
|
|
await triggerSH300Data();
|
|
console.log('\n✅ 任务执行成功!');
|
|
} catch (error) {
|
|
console.error('\n❌ 任务执行失败:', error.message);
|
|
await insertCronAlert('沪深300指数更新', error.message);
|
|
} finally {
|
|
console.log('='.repeat(70));
|
|
}
|
|
}, {
|
|
scheduled: true,
|
|
timezone: 'Asia/Shanghai'
|
|
});
|
|
|
|
console.log('✅ 定时任务已启动: USD-CNY 汇率数据更新');
|
|
console.log('📅 执行表达式: 20 6 * * * (每天 6:20)');
|
|
console.log('📅 下次执行时间: 明天 06:20');
|
|
console.log('');
|
|
console.log('✅ 定时任务已启动: 沪深300指数数据更新');
|
|
console.log('📅 执行表达式: 30 6 * * * (每天 6:30)');
|
|
console.log('📅 下次执行时间: 明天 06:30');
|
|
console.log('');
|
|
console.log('按 Ctrl+C 停止...');
|
|
console.log('='.repeat(70));
|
|
|
|
// 优雅处理退出
|
|
process.on('SIGINT', () => {
|
|
console.log('\n' + '='.repeat(70));
|
|
console.log('🛑 停止定时任务调度器');
|
|
console.log('='.repeat(70));
|
|
|
|
jobUSDCNY.stop();
|
|
jobSH300.stop();
|
|
console.log('✅ 定时任务已停止');
|
|
|
|
console.log('\n' + '='.repeat(70));
|
|
console.log('📊 定时任务调度器已停止');
|
|
console.log('='.repeat(70));
|
|
|
|
process.exit(0);
|
|
});
|