aa
This commit is contained in:
57
services/misc/misc.go
Normal file
57
services/misc/misc.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package misc
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-print/services"
|
||||
putils "git.rosy.net.cn/jx-print/utils"
|
||||
|
||||
"git.rosy.net.cn/jx-print/globals"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
ScheduleTimerFunc("SimFlowDaySettle", func() {
|
||||
services.SimFlowMonthSettle()
|
||||
services.SimFlowDaySettle()
|
||||
}, []string{
|
||||
"00:05:00",
|
||||
})
|
||||
}
|
||||
|
||||
// 按时间序列循环
|
||||
func ScheduleTimerFunc(name string, handler func(), timeList []string) {
|
||||
now := time.Now()
|
||||
nextTime := putils.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 := putils.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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user