From 0dbb094cd9c720b6dfd71a3a5f928d513bf15fb6 Mon Sep 17 00:00:00 2001 From: gouqi11 <66834753+gouqi11@users.noreply.github.com> Date: Wed, 15 Nov 2023 17:02:39 +0800 Subject: [PATCH] fix(region): fix db_dispatcher duplicate notify (#18698) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 马鸿飞 --- pkg/cloudcommon/db/db_dispatcher.go | 2 -- pkg/cloudcommon/db/notify_hook.go | 32 -------------------------- pkg/cloudcommon/notifyclient/notify.go | 22 ------------------ pkg/compute/models/hosts.go | 13 +++++++++++ 4 files changed, 13 insertions(+), 56 deletions(-) diff --git a/pkg/cloudcommon/db/db_dispatcher.go b/pkg/cloudcommon/db/db_dispatcher.go index 4d0e6de043e..9864548d3a5 100644 --- a/pkg/cloudcommon/db/db_dispatcher.go +++ b/pkg/cloudcommon/db/db_dispatcher.go @@ -1427,7 +1427,6 @@ func (dispatcher *DBModelDispatcher) Create(ctx context.Context, query jsonutils notes := model.GetShortDesc(ctx) OpsLog.LogEvent(model, ACT_CREATE, notes, userCred) logclient.AddActionLogWithContext(ctx, model, logclient.ACT_CREATE, notes, userCred, true) - CallCreateNotifyHook(ctx, userCred, model) } manager.OnCreateComplete(ctx, []IModel{model}, userCred, ownerId, query, []jsonutils.JSONObject{data}) return getItemDetails(manager, model, ctx, userCred, query) @@ -1878,7 +1877,6 @@ func DeleteModel(ctx context.Context, userCred mcclient.TokenCredential, item IM if userCred != nil { OpsLog.LogEvent(item, ACT_DELETE, item.GetShortDesc(ctx), userCred) logclient.AddSimpleActionLog(item, logclient.ACT_DELETE, item.GetShortDesc(ctx), userCred, true) - CallDeleteNotifyHook(ctx, userCred, item) } if _, ok := item.(IStandaloneModel); ok && len(item.GetId()) > 0 { err := Metadata.RemoveAll(ctx, item, userCred) diff --git a/pkg/cloudcommon/db/notify_hook.go b/pkg/cloudcommon/db/notify_hook.go index bcce5e27be1..a31eadece14 100644 --- a/pkg/cloudcommon/db/notify_hook.go +++ b/pkg/cloudcommon/db/notify_hook.go @@ -22,13 +22,9 @@ import ( var ( updateNotifyHook updateNotifyHookFunc - createNotifyHook createNotifyHookFunc - deleteNotifyHook deleteNotifyHookFunc ) type updateNotifyHookFunc func(ctx context.Context, userCred mcclient.TokenCredential, obj IModel) -type createNotifyHookFunc func(ctx context.Context, userCred mcclient.TokenCredential, obj IModel) -type deleteNotifyHookFunc func(ctx context.Context, userCred mcclient.TokenCredential, obj IModel) func SetUpdateNotifyHook(f updateNotifyHookFunc) { if updateNotifyHook != nil { @@ -37,37 +33,9 @@ func SetUpdateNotifyHook(f updateNotifyHookFunc) { updateNotifyHook = f } -func SetCreateNotifyHook(f createNotifyHookFunc) { - if createNotifyHook != nil { - panic("createNotifyHook already set") - } - createNotifyHook = f -} - -func SetDeleteNotifyHook(f deleteNotifyHookFunc) { - if deleteNotifyHook != nil { - panic("deleteNotifyHook already set") - } - deleteNotifyHook = f -} - func CallUpdateNotifyHook(ctx context.Context, userCred mcclient.TokenCredential, obj IModel) { if updateNotifyHook == nil { return } updateNotifyHook(ctx, userCred, obj) } - -func CallCreateNotifyHook(ctx context.Context, userCred mcclient.TokenCredential, obj IModel) { - if createNotifyHook == nil { - return - } - createNotifyHook(ctx, userCred, obj) -} - -func CallDeleteNotifyHook(ctx context.Context, userCred mcclient.TokenCredential, obj IModel) { - if deleteNotifyHook == nil { - return - } - deleteNotifyHook(ctx, userCred, obj) -} diff --git a/pkg/cloudcommon/notifyclient/notify.go b/pkg/cloudcommon/notifyclient/notify.go index 9e7c5849cc0..b0841940a1a 100644 --- a/pkg/cloudcommon/notifyclient/notify.go +++ b/pkg/cloudcommon/notifyclient/notify.go @@ -55,28 +55,6 @@ func init() { Action: ActionUpdate, }) }) - - db.SetCreateNotifyHook(func(ctx context.Context, userCred mcclient.TokenCredential, obj db.IModel) { - _, ok := notifyDBHookResources.Load(obj.KeywordPlural()) - if !ok { - return - } - EventNotify(ctx, userCred, SEventNotifyParam{ - Obj: obj, - Action: ActionCreate, - }) - }) - - db.SetDeleteNotifyHook(func(ctx context.Context, userCred mcclient.TokenCredential, obj db.IModel) { - _, ok := notifyDBHookResources.Load(obj.KeywordPlural()) - if !ok { - return - } - EventNotify(ctx, userCred, SEventNotifyParam{ - Obj: obj, - Action: ActionDelete, - }) - }) } func AddNotifyDBHookResources(keywordPlurals ...string) { diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 1bedf779698..65da6c3ba8e 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -3431,6 +3431,11 @@ func (hh *SHost) PostCreate( if err != nil { log.Errorf("CancelPendingUsage fail %s", err) } + hh.SEnabledStatusInfrasResourceBase.PostCreate(ctx, userCred, ownerId, query, data) + notifyclient.EventNotify(ctx, userCred, notifyclient.SEventNotifyParam{ + Obj: hh, + Action: notifyclient.ActionCreate, + }) } func (hh *SHost) StartBaremetalCreateTask(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, parentTaskId string) error { @@ -3848,6 +3853,14 @@ func (hh *SHost) PostUpdate(ctx context.Context, userCred mcclient.TokenCredenti } } +func (hh *SHost) PostDelete(ctx context.Context, userCred mcclient.TokenCredential) { + hh.SEnabledStatusInfrasResourceBase.PostDelete(ctx, userCred) + notifyclient.EventNotify(ctx, userCred, notifyclient.SEventNotifyParam{ + Obj: hh, + Action: notifyclient.ActionDelete, + }) +} + func (hh *SHost) UpdateDnsRecords(isAdd bool) { for _, netif := range hh.GetHostNetInterfaces() { hh.UpdateDnsRecord(&netif, isAdd)