diff --git a/plugin/manager/manager.go b/plugin/manager/manager.go index 6e5d401e..48b69a1a 100644 --- a/plugin/manager/manager.go +++ b/plugin/manager/manager.go @@ -281,7 +281,7 @@ func init() { // 插件主体 dateStrs := ctx.State["regex_matched"].([]string) ts := timer.GetFilledTimer(dateStrs, ctx.Event.SelfID, ctx.Event.GroupID, false) if ts.En() { - go clock.RegisterTimer(ts, true) + go clock.RegisterTimer(ts, true, false) ctx.SendChain(message.Text("记住了~")) } else { ctx.SendChain(message.Text("参数非法:" + ts.Alert)) @@ -304,7 +304,7 @@ func init() { // 插件主体 } logrus.Debugln("[manager] cron:", dateStrs[1]) ts := timer.GetFilledCronTimer(dateStrs[1], alert, url, ctx.Event.SelfID, ctx.Event.GroupID) - if clock.RegisterTimer(ts, true) { + if clock.RegisterTimer(ts, true, false) { ctx.SendChain(message.Text("记住了~")) } else { ctx.SendChain(message.Text("参数非法:" + ts.Alert)) diff --git a/plugin/manager/timer/timer.go b/plugin/manager/timer/timer.go index 1f2833ef..880d260d 100644 --- a/plugin/manager/timer/timer.go +++ b/plugin/manager/timer/timer.go @@ -8,6 +8,7 @@ import ( "time" sql "github.com/FloatTech/sqlite" + "github.com/FloatTech/zbputils/process" "github.com/fumiama/cron" "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" @@ -47,7 +48,7 @@ func NewClock(db *sql.Sqlite) (c Clock) { } // RegisterTimer 注册计时器 -func (c *Clock) RegisterTimer(ts *Timer, save bool) bool { +func (c *Clock) RegisterTimer(ts *Timer, save, isinit bool) bool { var key uint32 if save { key = ts.GetTimerID() @@ -62,6 +63,9 @@ func (c *Clock) RegisterTimer(ts *Timer, save bool) bool { logrus.Println("[群管]注册计时器", key) if ts.Cron != "" { var ctx *zero.Ctx + if isinit { + process.GlobalInitMutex.Lock() + } if ts.SelfID != 0 { ctx = zero.GetBot(ts.SelfID) } else { @@ -71,6 +75,9 @@ func (c *Clock) RegisterTimer(ts *Timer, save bool) bool { return false }) } + if isinit { + process.GlobalInitMutex.Unlock() + } eid, err := c.cron.AddFunc(ts.Cron, func() { ts.sendmsg(ts.GrpID, ctx) }) if err == nil { c.entmu.Lock() @@ -187,7 +194,7 @@ func (c *Clock) loadTimers(db *sql.Sqlite) { var t Timer _ = c.db.FindFor("timer", &t, "", func() error { tescape := t - go c.RegisterTimer(&tescape, false) + go c.RegisterTimer(&tescape, false, true) return nil }) }