Files
suyl 12c1f633cc aa
2021-07-20 16:54:43 +08:00

52 lines
1.5 KiB
Go

package misc
import (
"time"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/globals"
)
func Init() {
}
// 按时间序列循环
func ScheduleTimerFunc(name string, handler func(), timeList []string) {
now := time.Now()
nextTime := jxutils.GetNextTimeFromList(now, timeList)
duration := nextTime.Sub(now) + 1*time.Second
globals.SugarLogger.Debugf("ScheduleTimerFunc, func:%s, duration:%v", name, duration)
utils.AfterFuncWithRecover(duration, func() {
globals.SugarLogger.Debugf("ScheduleTimerFunc func:%s", name)
handler()
ScheduleTimerFunc(name, handler, timeList)
})
}
// 按时间调度一次
func ScheduleTimerFuncOnce(name string, handler func(interface{}), timeStr string, param interface{}) {
now := time.Now()
nextTime := jxutils.GetNextTimeFromList(now, []string{timeStr})
duration := nextTime.Sub(now) + 1*time.Second
globals.SugarLogger.Debugf("ScheduleTimerFuncOnce, func:%s, duration:%v", name, duration)
utils.AfterFuncWithRecover(duration, func() {
globals.SugarLogger.Debugf("ScheduleTimerFuncOnce func:%s", name)
handler(param)
})
}
func ScheduleTimerFuncByInterval(handler func(), delay, inerval time.Duration) {
globals.SugarLogger.Debugf("ScheduleTimerFuncByInterval, delay:%v, inerval:%v", delay, inerval)
utils.AfterFuncWithRecover(delay, func() {
beginTime := time.Now()
handler()
delay = inerval - time.Now().Sub(beginTime)
if delay < time.Second {
delay = time.Second
}
ScheduleTimerFuncByInterval(handler, delay, inerval)
})
}