Files
jx-callback/business/jxstore/misc/misc.go
2020-10-16 15:16:26 +08:00

69 lines
1.9 KiB
Go

package misc
import (
"time"
"git.rosy.net.cn/jx-callback/business/jxstore/cms"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/globals"
)
var (
dailyWorkTimeList = []string{
"00:00:30",
}
)
func Init() {
if globals.IsProductEnv() {
ScheduleTimerFunc("doDailyWork", doDailyWork, dailyWorkTimeList)
}
}
func doDailyWork() {
globals.SugarLogger.Debug("doDailyWork")
//刷新任务过期状态
cms.RefreshUserMemberStatus(jxcontext.AdminCtx)
}
// 按时间序列循环
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)
})
}