fix(notify): clean notificationGroup after send (#19858)

Co-authored-by: 马鸿飞 <mahongfei@yunion.cn>
This commit is contained in:
gouqi11
2024-04-02 18:40:13 +08:00
committed by GitHub
co-authored by 马鸿飞
parent 546d0112b1
commit cc34cce115
4 changed files with 29 additions and 11 deletions
+1
View File
@@ -168,6 +168,7 @@ type SendParams struct {
// minutes
GroupTimes uint
ReceiverId string
SendTime time.Time
}
type SRemoteTemplateParam struct {
+6 -1
View File
@@ -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
}
+18 -6
View File
@@ -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 = " <br>"
}
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{
+4 -4
View File
@@ -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)
}
}()