From cc34cce1153ff0ebd986214e17d30ae3c556e000 Mon Sep 17 00:00:00 2001 From: gouqi11 <66834753+gouqi11@users.noreply.github.com> Date: Tue, 2 Apr 2024 18:40:13 +0800 Subject: [PATCH] fix(notify): clean notificationGroup after send (#19858) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 马鸿飞 --- pkg/apis/notify/config.go | 1 + pkg/notify/models/config.go | 7 +++++- pkg/notify/models/notification_group.go | 24 +++++++++++++++------ pkg/notify/tasks/notifications_send_task.go | 8 +++---- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/pkg/apis/notify/config.go b/pkg/apis/notify/config.go index d49989a979..63aaadbc8e 100644 --- a/pkg/apis/notify/config.go +++ b/pkg/apis/notify/config.go @@ -168,6 +168,7 @@ type SendParams struct { // minutes GroupTimes uint ReceiverId string + SendTime time.Time } type SRemoteTemplateParam struct { diff --git a/pkg/notify/models/config.go b/pkg/notify/models/config.go index fb52726b13..cbafde0242 100644 --- a/pkg/notify/models/config.go +++ b/pkg/notify/models/config.go @@ -192,7 +192,11 @@ func (c *SConfig) PostUpdate(ctx context.Context, userCred mcclient.TokenCredent func (c *SConfig) PreDelete(ctx context.Context, userCred mcclient.TokenCredential) { c.SStandaloneResourceBase.PreDelete(ctx, userCred) - delete(ConfigMap, fmt.Sprintf("%s-%s", c.Type, c.DomainId)) + key := fmt.Sprintf("%s-%s", c.Type, c.DomainId) + if c.Type == api.MOBILE || c.Type == api.EMAIL { + key = c.Type + } + delete(ConfigMap, key) } func (c *SConfig) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error { @@ -392,6 +396,7 @@ func (confManager *SConfigManager) InitializeData() error { logclient.AddSimpleActionLog(&config, logclient.ACT_INIT_NOTIFY_CONFIGMAP, err, session.GetToken(), false) } } + log.Infoln("init ConfigMap:", jsonutils.Marshal(ConfigMap)) return nil } diff --git a/pkg/notify/models/notification_group.go b/pkg/notify/models/notification_group.go index 0507cbf8ae..b8c13e656b 100644 --- a/pkg/notify/models/notification_group.go +++ b/pkg/notify/models/notification_group.go @@ -17,31 +17,31 @@ package models // import "yunion.io/x/onecloud/pkg/notify/models" import ( "context" "fmt" + "strings" "time" "yunion.io/x/jsonutils" + "yunion.io/x/log" "yunion.io/x/pkg/errors" + "yunion.io/x/sqlchemy" apis "yunion.io/x/onecloud/pkg/apis/notify" - "yunion.io/x/onecloud/pkg/cloudcommon/consts" "yunion.io/x/onecloud/pkg/cloudcommon/db" ) type SNotificationGroupManager struct { - db.SLogBaseManager + db.SModelBaseManager } var NotificationGroupManager *SNotificationGroupManager func init() { NotificationGroupManager = &SNotificationGroupManager{ - SLogBaseManager: db.NewLogBaseManager( + SModelBaseManager: db.NewModelBaseManager( SNotificationGroup{}, "notification_group_tbl", "notification_group", "notification_groups", - "created_at", - consts.OpsLogWithClickhouse, ), } NotificationGroupManager.SetVirtualObject(NotificationGroupManager) @@ -49,8 +49,9 @@ func init() { // 站内信 type SNotificationGroup struct { - db.SLogBase + db.SModelBase + Id string `width:"128" nullable:"false" create:"required" list:"user" index:"true" get:"user"` GroupKey string `width:"128" nullable:"false" create:"required" list:"user" get:"user"` Title string // swagger:ignore @@ -70,6 +71,7 @@ func (ng *SNotificationGroupManager) TaskCreate(ctx context.Context, contactType return nil } insertNotificationGroup := SNotificationGroup{ + Id: db.DefaultUUIDGenerator(), ContactType: contactType, Body: args.Body, Header: args.Header, @@ -121,9 +123,19 @@ func (ng *SNotificationGroupManager) TaskSend(ctx context.Context, input apis.SN if input.ContactType == apis.EMAIL { joinStr = "
" } + deleteIds := []string{} for _, ng := range ngs { msg += fmt.Sprintf("%s %s", ng.Message, joinStr) + deleteIds = append(deleteIds, fmt.Sprintf("'%s'", ng.Id)) } + + defer func() { + _, err = sqlchemy.Exec(fmt.Sprintf("delete from %s where id in (%s) ", ng.TableSpec().Name(), strings.Join(deleteIds, ","))) + if err != nil { + log.Errorln("clean notification_groups err:", err) + } + }() + sendParams.Message = msg if input.ContactType == apis.EMAIL { sendParams.EmailMsg = apis.SEmailMessage{ diff --git a/pkg/notify/tasks/notifications_send_task.go b/pkg/notify/tasks/notifications_send_task.go index 120687262c..41ad5a6eab 100644 --- a/pkg/notify/tasks/notifications_send_task.go +++ b/pkg/notify/tasks/notifications_send_task.go @@ -288,6 +288,7 @@ func (notificationSendTask *NotificationSendTask) batchSend(ctx context.Context, params.Receivers.Contact = mobile } params.ReceiverId = receiver.Id + params.SendTime = time.Now().Truncate(time.Second) if len(params.GroupKey) > 0 && params.GroupTimes > 0 { notificationGroupLock.Lock() if _, ok := notificationSendMap.Load(params.GroupKey + receiver.Id + notification.ContactType); ok { @@ -328,8 +329,8 @@ func createTimeTicker(ctx context.Context, driver models.ISenderDriver, params a GroupKey: params.GroupKey, ReceiverId: receiverId, ContactType: contactType, - StartTime: time.Now(), - EndTime: time.Now().Add(time.Duration(params.GroupTimes) * time.Minute), + StartTime: params.SendTime, + EndTime: params.SendTime.Add(time.Duration(params.GroupTimes) * time.Minute), }) // 启动一个goroutine来处理计时器触发的事件 @@ -349,8 +350,7 @@ func createTimeTicker(ctx context.Context, driver models.ISenderDriver, params a log.Errorln("TaskSend err:", err) return } - driverT := models.GetDriver(contactType) - driverT.Send(ctx, *sendParams) + driver.Send(ctx, *sendParams) notificationSendMap.Delete(params.GroupKey + receiverId + contactType) } }()