From 85dd9091d1ca0976380b70fb5b5a3d9f776322d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E9=B8=BF=E9=A3=9E?= Date: Wed, 15 Nov 2023 15:51:39 +0800 Subject: [PATCH] fix(region): fix db_dispatcher duplicate notify --- 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 | 14 ++++++++++- 4 files changed, 13 insertions(+), 57 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..96708630b10 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -3382,7 +3382,6 @@ func (hh *SHost) PostCreate( query jsonutils.JSONObject, data jsonutils.JSONObject, ) { - hh.SEnabledStatusInfrasResourceBase.PostCreate(ctx, userCred, ownerId, query, data) input := api.HostCreateInput{} err := data.Unmarshal(&input) if err != nil { @@ -3431,6 +3430,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 +3852,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)