diff --git a/controller/db/api_session_store.go b/controller/db/api_session_store.go index 525754e41..c05bdfaaf 100644 --- a/controller/db/api_session_store.go +++ b/controller/db/api_session_store.go @@ -88,6 +88,7 @@ func newApiSessionStore(stores *stores) *apiSessionStoreImpl { store.baseStore = newBaseStore[*ApiSession](stores, store) stores.EventualEventer.AddEventualListener(EventualEventApiSessionDelete, store.onEventualDelete) store.InitImpl(store) + store.AddEntityConstraint(store) return store } @@ -141,6 +142,7 @@ func (store *apiSessionStoreImpl) GetEventsEmitter() events.EventEmmiter { func (store *apiSessionStoreImpl) onEventualDelete(db boltz.Db, name string, apiSessionId []byte) { idCollector := &sessionIdCollector{} indexPath := []string{RootBucket, boltz.IndexesBucket, EntityTypeApiSessions, EntityTypeSessions} + err := db.View(func(tx *bbolt.Tx) error { path := append(indexPath, string(apiSessionId)) if bucket := boltz.Path(tx, path...); bucket != nil { @@ -224,24 +226,33 @@ func (store *apiSessionStoreImpl) Update(ctx boltz.MutateContext, entity *ApiSes return err } -func (store *apiSessionStoreImpl) DeleteById(ctx boltz.MutateContext, id string) error { +func (store *apiSessionStoreImpl) ProcessPreCommit(state *boltz.EntityChangeState[*ApiSession]) error { + if state.ChangeType == boltz.EntityDeleted { + return store.handleDeleteCleanup(state.Ctx, state.EntityId) + } + return nil +} + +func (store *apiSessionStoreImpl) ProcessPostCommit(_ *boltz.EntityChangeState[*ApiSession]) { + /* does nothing */ +} + +func (store *apiSessionStoreImpl) handleDeleteCleanup(ctx boltz.MutateContext, id string) error { for _, apiSessionCertId := range store.GetRelatedEntitiesIdList(ctx.Tx(), id, EntityTypeApiSessionCertificates) { if err := store.stores.apiSessionCertificate.DeleteById(ctx, apiSessionCertId); err != nil { return err } } - err := store.baseStore.DeleteById(ctx, id) - - if err == nil { - if bboltEventualEventer, ok := store.baseStore.stores.EventualEventer.(*EventualEventerBbolt); ok { - bboltEventualEventer.AddEventualEventWithCtx(ctx, EventualEventApiSessionDelete, []byte(id)) - } else { - store.baseStore.stores.EventualEventer.AddEventualEvent(EventualEventApiSessionDelete, []byte(id)) + if bboltEventualEventer, ok := store.baseStore.stores.EventualEventer.(*EventualEventerBbolt); ok { + if err := bboltEventualEventer.AddEventualEventWithCtx(ctx, EventualEventApiSessionDelete, []byte(id)); err != nil { + return err } + } else { + store.baseStore.stores.EventualEventer.AddEventualEvent(EventualEventApiSessionDelete, []byte(id)) } - return err + return nil } func (store *apiSessionStoreImpl) GetTokenIndex() boltz.ReadIndex { diff --git a/controller/db/eventual_eventer.go b/controller/db/eventual_eventer.go index ba19cd545..0f9414629 100644 --- a/controller/db/eventual_eventer.go +++ b/controller/db/eventual_eventer.go @@ -328,13 +328,8 @@ func (a *EventualEventerBbolt) initOutstandingEventCount() { } } -func (a *EventualEventerBbolt) AddEventualEventWithCtx(ctx boltz.MutateContext, eventType string, data []byte) { +func (a *EventualEventerBbolt) AddEventualEventWithCtx(ctx boltz.MutateContext, eventType string, data []byte) error { newId := cuid.New() - total := atomic.AddInt64(a.outstandingEvents, 1) - a.Emit(EventualEventAddedName, &EventualEventAdded{ - Id: newId, - Total: total, - }) event := &EventualEvent{ BaseExtEntity: boltz.BaseExtEntity{ @@ -357,18 +352,21 @@ func (a *EventualEventerBbolt) AddEventualEventWithCtx(ctx boltz.MutateContext, } if err != nil { - total := atomic.AddInt64(a.outstandingEvents, -1) - a.Emit(EventualEventRemovedName, &EventualEventRemoved{ - Id: newId, - Total: total, - }) pfxlog.Logger().WithError(err).Error("error adding event for EventualEventerBbolt") - return + return err } + + total := atomic.AddInt64(a.outstandingEvents, 1) + a.Emit(EventualEventAddedName, &EventualEventAdded{ + Id: newId, + Total: total, + }) + + return nil } func (a *EventualEventerBbolt) AddEventualEvent(eventType string, data []byte) { - a.AddEventualEventWithCtx(nil, eventType, data) + _ = a.AddEventualEventWithCtx(nil, eventType, data) } func (a *EventualEventerBbolt) AddEventualListener(eventType string, listener EventListenerFunc) {