米家调度独立模块注册, 避免被容器中无法加载的旧 cron.js 连带失效
Docker Build and Push / build-image (push) Successful in 4m36s

This commit is contained in:
jif2.zhang
2026-09-21 11:52:24 +08:00
parent 8e2c6ffa35
commit 1d69915cf3
3 changed files with 41 additions and 36 deletions
+38
View File
@@ -0,0 +1,38 @@
// 米家环境监控定时调度
// 独立于 server/cron.js 注册: 容器镜像不含 scripts/, 旧 cron.js 顶层 import 脚本模块会整体加载失败,
// 故米家节拍不依赖它, 由 server/index.js 单独加载本模块, 确保采样/聚合在部署环境一定生效。
import cron from 'node-cron'
import * as mihome from './index.js'
export function registerMihomeScheduler() {
cron.schedule('5 * * * *', async () => {
console.log('[cron] 米家小时采样触发 @ 上海 xx:05')
try {
const r = await mihome.syncNow()
console.log('[cron] 米家采样完成: 家庭=' + r.homes + ' 设备=' + r.devices + ' 采样=' + r.sampled + ' 聚合=' + r.aggregated + ' (' + r.ms + 'ms)')
} catch (e) {
console.error('[cron] 米家小时采样失败:', e.message)
}
}, { timezone: 'Asia/Shanghai' })
console.log('[cron] 已注册: 每小时 xx:05 米家温湿度采样')
cron.schedule('5 0,3 * * *', async () => {
console.log('[cron] 米家每日维护触发 (00:05 / 03:05)')
try {
const r = await mihome.dailyMaintenance()
console.log('[cron] 米家聚合完成: 采样=' + r.sampled + ' 聚合=' + r.aggregated
+ ' vendor小时=' + r.vendorHours + ' vendor天=' + r.vendorDays + ' 缺口聚合=' + r.gapAggregated)
} catch (e) {
console.error('[cron] 米家每日维护失败:', e.message)
}
}, { timezone: 'Asia/Shanghai' })
console.log('[cron] 已注册: 每天 00:05/03:05 米家日聚合 + vendor 补齐')
// 启动追赶: 服务重启/停机恢复后立即补一次 (不打断 5 分钟内的整点采样)
setTimeout(() => {
mihome.dailyMaintenance().catch((e) => console.error('[cron] 米家启动追赶失败:', e.message))
}, 8000)
console.log('[cron] 已注册: 启动后 8s 执行米家追赶同步')
}
registerMihomeScheduler()