Spaces:
Running
Running
File size: 3,316 Bytes
0b89e2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
import { dataService } from './dataService.js'
/**
* 自动刷新服务
* 每天美东时间早上5点自动从数据库刷新数据
*/
class AutoRefreshService {
constructor() {
this.timerId = null
this.targetHour = 5 // 美东早上5点
this.isRunning = false
}
/**
* 计算下一次美东早上5点的时间
*/
getNextRefreshTime() {
const now = new Date()
// 获取美东时区的当前时间
const etOptions = { timeZone: 'America/New_York', hour12: false }
const etTimeString = now.toLocaleString('en-US', etOptions)
const etDate = new Date(etTimeString)
// 设置为美东早上5点
const nextRefresh = new Date(etDate)
nextRefresh.setHours(this.targetHour, 0, 0, 0)
// 如果已经过了今天的5点,设置为明天
if (etDate.getTime() >= nextRefresh.getTime()) {
nextRefresh.setDate(nextRefresh.getDate() + 1)
}
// 转换回本地时区
const etTimestamp = nextRefresh.getTime()
const etOffset = this.getETOffset(nextRefresh)
const localOffset = now.getTimezoneOffset() * 60 * 1000
const localTimestamp = etTimestamp - etOffset + localOffset
return new Date(localTimestamp)
}
/**
* 获取美东时区的偏移量(毫秒)
*/
getETOffset(date) {
const etString = date.toLocaleString('en-US', {
timeZone: 'America/New_York',
timeZoneName: 'short'
})
// EST is UTC-5, EDT is UTC-4
const isDST = etString.includes('EDT')
return isDST ? -4 * 60 * 60 * 1000 : -5 * 60 * 60 * 1000
}
/**
* 执行刷新
*/
async refresh() {
console.log('[AutoRefresh] Refreshing data at', new Date().toLocaleString())
try {
await dataService.load(true)
console.log('[AutoRefresh] Data refreshed successfully')
} catch (error) {
console.error('[AutoRefresh] Failed to refresh data:', error)
}
}
/**
* 调度下一次刷新
*/
scheduleNext() {
// 清除现有的定时器
if (this.timerId) {
clearTimeout(this.timerId)
this.timerId = null
}
const nextRefresh = this.getNextRefreshTime()
const now = new Date()
const delay = nextRefresh.getTime() - now.getTime()
console.log('[AutoRefresh] Next refresh scheduled at:', nextRefresh.toLocaleString())
console.log('[AutoRefresh] Time until next refresh:', Math.round(delay / 1000 / 60), 'minutes')
this.timerId = setTimeout(async () => {
await this.refresh()
this.scheduleNext() // 刷新后调度下一次
}, delay)
}
/**
* 启动自动刷新
*/
start() {
if (this.isRunning) {
console.log('[AutoRefresh] Already running')
return
}
console.log('[AutoRefresh] Starting auto-refresh service')
this.isRunning = true
this.scheduleNext()
}
/**
* 停止自动刷新
*/
stop() {
console.log('[AutoRefresh] Stopping auto-refresh service')
if (this.timerId) {
clearTimeout(this.timerId)
this.timerId = null
}
this.isRunning = false
}
/**
* 手动触发刷新(不影响定时调度)
*/
async triggerManualRefresh() {
console.log('[AutoRefresh] Manual refresh triggered')
await this.refresh()
}
}
// 创建单例
export const autoRefreshService = new AutoRefreshService()
|