const cron = require('node-cron'); const { refreshOpsBIncentiveSnapshots } = require('../services/opsBIncentiveBoardService'); const JOB_CRON = String(process.env.OPS_B_INCENTIVE_SNAPSHOT_CRON || '0 * * * *').trim(); const TIMEZONE = 'Asia/Shanghai'; async function runOpsBIncentiveSnapshotJob(source = 'scheduled') { try { const result = await refreshOpsBIncentiveSnapshots(source); console.log('[OpsBIncentiveSnapshot] refresh completed:', JSON.stringify(result)); return result; } catch (error) { console.error('[OpsBIncentiveSnapshot] refresh failed:', error.message || error); return []; } } function startOpsBIncentiveSnapshotSchedulers() { // 默认开启;仅当显式设置 DISABLE_OPS_B_INCENTIVE_SNAPSHOT=true 时关闭。 const disabled = String(process.env.DISABLE_OPS_B_INCENTIVE_SNAPSHOT || '').trim().toLowerCase() === 'true'; if (disabled) { console.log('[Scheduler] 运营中心B激励看板快照任务已被禁用'); return; } cron.schedule(JOB_CRON, () => { runOpsBIncentiveSnapshotJob('daily_schedule').catch(() => {}); }, { timezone: TIMEZONE }); console.log(`[Scheduler] 运营中心B激励看板快照任务已注册: ${JOB_CRON} (${TIMEZONE})`); } module.exports = { startOpsBIncentiveSnapshotSchedulers, runOpsBIncentiveSnapshotJob };