Files
jx-print/services/misc/misc.go
邹宗楠 ecdb628231 打印机
2022-07-11 16:43:16 +08:00

59 lines
1.8 KiB
Go

package misc
import (
"git.rosy.net.cn/baseapi/utils"
putils "git.rosy.net.cn/jx-print/putils"
"git.rosy.net.cn/jx-print/services/print_server"
"git.rosy.net.cn/jx-print/globals"
"time"
)
func Init() {
globals.SugarLogger.Debugf("init misc ...")
ScheduleTimerFunc("SimFlowDaySettle", func() {
print_server.SimFlowMonthSettle()
print_server.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)
})
}