From 0c4892bed08bfa912501b4680b60dee2feaa8877 Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Tue, 28 Jan 2025 18:54:42 +0800 Subject: [PATCH] fix: notify empty receipients (#22035) Co-authored-by: Qiu Jian --- pkg/cloudcommon/notifyclient/notify.go | 2 +- .../notifyclient/notify_internal.go | 34 ++++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/pkg/cloudcommon/notifyclient/notify.go b/pkg/cloudcommon/notifyclient/notify.go index fa4bad923e..bf620e6f30 100644 --- a/pkg/cloudcommon/notifyclient/notify.go +++ b/pkg/cloudcommon/notifyclient/notify.go @@ -42,7 +42,7 @@ var ( ) func init() { - notifyClientWorkerMan = appsrv.NewWorkerManager("NotifyClientWorkerManager", 1, 50, false) + notifyClientWorkerMan = appsrv.NewWorkerManager("NotifyClientWorkerManager", 1, 1024, false) // set db notify hook db.SetUpdateNotifyHook(func(ctx context.Context, userCred mcclient.TokenCredential, obj db.IModel) { diff --git a/pkg/cloudcommon/notifyclient/notify_internal.go b/pkg/cloudcommon/notifyclient/notify_internal.go index d66249f73f..516d0c381f 100644 --- a/pkg/cloudcommon/notifyclient/notify_internal.go +++ b/pkg/cloudcommon/notifyclient/notify_internal.go @@ -182,6 +182,16 @@ func lang(ctx context.Context, contactType npk.TNotifyChannel, reIds []string, c return langMap, nil } +func isEmptyRecipients(recipientId []string) bool { + var recvs []string + for _, c := range recipientId { + if len(c) > 0 { + recvs = append(recvs, c) + } + } + return len(recvs) == 0 +} + func genMsgViaLang(ctx context.Context, p sNotifyParams) ([]npk.SNotifyMessage, error) { reIds := make([]string, 0) s, err := AdminSessionGenerator(context.Background(), consts.GetRegion()) @@ -206,6 +216,10 @@ func genMsgViaLang(ctx context.Context, p sNotifyParams) ([]npk.SNotifyMessage, reIds = p.recipientId } + if isEmptyRecipients(p.recipientId) { + return nil, errors.Wrap(errors.ErrEmpty, "empty receipients") + } + if !hasTemplateOfTopic(p.event) { msg := npk.SNotifyMessage{} msg.Uid = reIds @@ -225,6 +239,9 @@ func genMsgViaLang(ctx context.Context, p sNotifyParams) ([]npk.SNotifyMessage, if p.channel == npk.NotifyByRobot { langMap, err = langRobot(ctx, p.robots) + if err != nil { + return nil, err + } } else { langMap, err = lang(ctx, p.channel, reIds, p.contacts) if err != nil { @@ -332,13 +349,10 @@ func notifyNormal(ctx context.Context, recipientId []string, isGroup bool, event } func notifyWithChannel(ctx context.Context, p sNotifyParams, channels ...npk.TNotifyChannel) { - reps := p.recipientId for _, c := range channels { - p.recipientId = []string{} - p.contacts = []string{} - p.channel = c - p.recipientId = reps - rawNotify(ctx, p) + np := p + np.channel = c + rawNotify(ctx, np) } } @@ -367,7 +381,7 @@ func (t *notifyTask) Run() { break } if !t.createReceiver { - log.Errorf("unable to send notification: %v", err) + log.Errorf("unable to send notification to contacts %s uid %s gid %s @%s: %s", strings.Join(t.msg.Contacts, ","), strings.Join(t.msg.Uid, ","), strings.Join(t.msg.Gid, ","), t.msg.ContactType, jsonutils.Marshal(err)) break } jerr, ok := err.(*httputils.JSONClientError) @@ -394,10 +408,12 @@ func (t *notifyTask) Run() { } log.Infof("create receiver %q successfully", receiverId) } - return } func intelliNotify(ctx context.Context, p sNotifyParams) { + if isEmptyRecipients(p.recipientId) { + return + } log.Infof("recipientId: %v, contacts: %v, event %s priority %s", p.recipientId, p.contacts, p.event, p.priority) msgs, err := genMsgViaLang(ctx, p) if err != nil { @@ -405,7 +421,7 @@ func intelliNotify(ctx context.Context, p sNotifyParams) { } for i := range msgs { t := notifyTask{ - ctx: context.Background(), + ctx: ctx, createReceiver: p.createReceiver, msg: msgs[i], }