guanghulab/scheduler/cron-jobs.js
Guanghu Domestic Migration d1e47f4565
Some checks are pending
自动更新代码和重启 / update-and-restart (push) Waiting to run
CI检查 + 自动部署 / check (push) Waiting to run
CI检查 + 自动部署 / deploy (push) Blocked by required conditions
重启聊天服务 / restart (push) Waiting to run
chore: import sanitized domestic snapshot for REPO-002
Source snapshot: ca48d3ddf926d79aa138306164169baf764bb829
2026-07-17 15:54:41 +08:00

98 lines
2.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 定时任务调度器·cron-jobs.js
// HoloLake·M-DINGTALK Phase 3
// DEV-004 之之×秋秋
//
// 统一管理所有定时任务
//
const reminder = require('./reminder');
const syncEngine = require('../sync/sync-engine');
// 所有定时任务
const jobs = [];
let isRunning = false;
/**
* 注册并启动所有定时任务
*/
function startAll() {
if (isRunning) {
console.log('[CronJobs] 已在运行,跳过');
return;
}
console.log('[CronJobs] ==========================');
console.log('[CronJobs] 启动所有定时任务...');
// 任务1超时提醒每小时
reminder.start();
jobs.push({ name: '超时提醒', interval: '每小时', module: 'reminder' });
// 任务2三节点全量同步每30分钟
const syncTimer = setInterval(async () => {
console.log('[CronJobs] 触发定时同步...');
await syncEngine.syncAll();
}, 30 * 60 * 1000);
jobs.push({
name: '三节点同步',
interval: '每30分钟',
module: 'sync-engine',
timer: syncTimer
});
isRunning = true;
console.log(`[CronJobs] 已启动 ${jobs.length} 个定时任务`);
console.log('[CronJobs] ==========================');
}
/**
* 停止所有定时任务
*/
function stopAll() {
if (!isRunning) {
console.log('[CronJobs] 没有运行中的任务');
return;
}
console.log('[CronJobs] 停止所有定时任务...');
// 停止超时提醒
reminder.stop();
// 停止同步定时器
jobs.forEach(job => {
if (job.timer) {
clearInterval(job.timer);
}
});
// 清空任务列表
jobs.length = 0;
isRunning = false;
console.log('[CronJobs] 所有定时任务已停止');
}
/**
* 获取任务状态
*/
function getStatus() {
return {
isRunning,
jobCount: jobs.length,
jobs: jobs.map(j => ({
name: j.name,
interval: j.interval,
module: j.module
})),
reminder: reminder.getStatus()
};
}
module.exports = {
startAll,
stopAll,
getStatus
};