go_dreamfactory/modules/notify/notify_timer.go
2023-06-14 15:16:43 +08:00

74 lines
1.8 KiB
Go

package notify
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/sys/timewheel"
"go_dreamfactory/pb"
"time"
)
///定时通知组件
type notifyTimer struct {
cbase.ModuleCompBase
module *Notify
timers map[string]map[comm.NotifyType]*timernotify
}
//组件初始化接口
func (this *notifyTimer) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, opt)
this.timers = make(map[string]map[comm.NotifyType]*timernotify)
return
}
func (this *notifyTimer) addTimerNotify(uid string, ntype comm.NotifyType, delay time.Duration) (errdata *pb.ErrorData) {
var (
utimers map[comm.NotifyType]*timernotify
timer *timernotify
ok bool
)
if utimers, ok = this.timers[uid]; !ok {
this.timers[uid] = make(map[comm.NotifyType]*timernotify)
utimers = this.timers[uid]
}
if timer, ok = utimers[ntype]; !ok { //还没有定时任务
utimers[ntype] = &timernotify{}
utimers[ntype].rtask = timewheel.Add(delay, this.timerNotify, uid, ntype)
} else {
timewheel.Remove(utimers[ntype].rtask)
timer.rtask = timewheel.Add(delay, this.timerNotify, uid, ntype)
}
return
}
func (this *notifyTimer) timerNotify(task *timewheel.Task, args ...interface{}) {
if len(args) != 2 {
this.module.Error("异常定时通知消息", log.Field{Key: "args", Value: args})
return
}
var (
uid string
ntype comm.NotifyType
utimers map[comm.NotifyType]*timernotify
ok bool
)
uid = args[0].(string)
ntype = args[1].(comm.NotifyType)
if utimers, ok = this.timers[uid]; !ok {
return
}
if _, ok = utimers[ntype]; !ok { //还没有定时任务
return
}
delete(utimers, ntype)
switch ntype {
case comm.Notify1001:
break
}
}