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() { //ScheduleTimerFunc("SimFlowDaySettle", func() { // cms.SimFlowDaySettle(jxcontext.AdminCtx) // cms.SimFlowMonthSettle(jxcontext.AdminCtx) //}, []string{ // "00:05:00", //}) } // 按时间序列循环 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) }) }