MCPcopy Index your code
hub / github.com/FloatTech/ZeroBot-Plugin / RegisterTimer

Method RegisterTimer

plugin/manager/timer/timer.go:50–118  ·  view source on GitHub ↗

RegisterTimer 注册计时器

(ts *Timer, save, isinit bool)

Source from the content-addressed store, hash-verified

48
49// RegisterTimer 注册计时器
50func (c *Clock) RegisterTimer(ts *Timer, save, isinit bool) bool {
51 var key uint32
52 if save {
53 key = ts.GetTimerID()
54 ts.ID = key
55 } else {
56 key = ts.ID
57 }
58 t, ok := c.GetTimer(key)
59 if t != ts && ok { // 避免重复注册定时器
60 t.SetEn(false)
61 }
62 logrus.Infoln("[群管]注册计时器", key)
63 if ts.Cron != "" {
64 var ctx *zero.Ctx
65 if isinit {
66 process.GlobalInitMutex.Lock()
67 }
68 if ts.SelfID != 0 {
69 ctx = zero.GetBot(ts.SelfID)
70 } else {
71 zero.RangeBot(func(id int64, c *zero.Ctx) bool {
72 ctx = c
73 ts.SelfID = id
74 return false
75 })
76 }
77 if isinit {
78 process.GlobalInitMutex.Unlock()
79 }
80 eid, err := c.cron.AddFunc(ts.Cron, func() { ts.sendmsg(ts.GrpID, ctx) })
81 if err == nil {
82 c.entmu.Lock()
83 c.entries[key] = eid
84 c.entmu.Unlock()
85 if save {
86 err = c.AddTimerIntoDB(ts)
87 }
88 if err == nil {
89 err = c.AddTimerIntoMap(ts)
90 }
91 return err == nil
92 }
93 ts.Alert = err.Error()
94 } else {
95 if save {
96 _ = c.AddTimerIntoDB(ts)
97 }
98 _ = c.AddTimerIntoMap(ts)
99 for ts.En() {
100 nextdate := ts.nextWakeTime()
101 sleepsec := time.Until(nextdate)
102 logrus.Printf("[群管]计时器%08x将睡眠%ds", key, sleepsec/time.Second)
103 time.Sleep(sleepsec)
104 if ts.En() {
105 if ts.Month() < 0 || ts.Month() == time.Now().Month() {
106 if ts.Day() < 0 || ts.Day() == time.Now().Day() {
107 ts.judgeHM()

Callers 2

loadTimersMethod · 0.95
initFunction · 0.80

Calls 12

GetTimerMethod · 0.95
AddTimerIntoDBMethod · 0.95
AddTimerIntoMapMethod · 0.95
GetTimerIDMethod · 0.80
SetEnMethod · 0.80
sendmsgMethod · 0.80
EnMethod · 0.80
nextWakeTimeMethod · 0.80
MonthMethod · 0.80
DayMethod · 0.80
judgeHMMethod · 0.80
WeekMethod · 0.80

Tested by

no test coverage detected