fix(event handling): use internal pubsub for view update (#1118)

* start sub

* start implement subsciptions

* start subscription

* implementation for member done

* admin done

* fix: tests

* extend handlers

* prepary notification

* no errors in adminapi

* changed current sequence in all packages

* ignore mocks

* works

* subscriptions as singleton

* tests

* refactor: rename function scope var
This commit is contained in:
Silvan
2020-12-18 16:47:45 +01:00
committed by GitHub
parent e15fc0b92b
commit dd5e4acd24
160 changed files with 4010 additions and 1596 deletions

View File

@@ -1,8 +1,11 @@
package query
import (
"context"
"time"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/eventstore/models"
)
@@ -14,4 +17,42 @@ type Handler interface {
OnSuccess() error
MinimumCycleDuration() time.Duration
QueryLimit() uint64
AggregateTypes() []models.AggregateType
CurrentSequence(*models.Event) (uint64, error)
Eventstore() eventstore.Eventstore
}
func ReduceEvent(handler Handler, event *models.Event) {
currentSequence, err := handler.CurrentSequence(event)
if err != nil {
logging.Log("HANDL-BmpkC").WithError(err).Warn("unable to get current sequence")
return
}
if event.PreviousSequence > currentSequence {
searchQuery := models.NewSearchQuery().
AggregateTypeFilter(handler.AggregateTypes()...).
SequenceBetween(currentSequence, event.PreviousSequence)
events, err := handler.Eventstore().FilterEvents(context.Background(), searchQuery)
if err != nil {
logging.LogWithFields("HANDL-L6YH1", "seq", event.Sequence).Warn("filter failed")
return
}
for _, previousEvent := range events {
//if other process already updated view
//TODO: correct?
if event.PreviousSequence > previousEvent.Sequence {
continue
}
err = handler.Reduce(previousEvent)
logging.LogWithFields("HANDL-V42TI", "seq", previousEvent.Sequence).OnError(err).Warn("reduce failed")
return
}
} else if event.PreviousSequence > 0 && event.PreviousSequence < currentSequence {
logging.LogWithFields("HANDL-w9Bdy", "previousSeq", event.PreviousSequence, "currentSeq", currentSequence).Debug("already processed")
return
}
err = handler.Reduce(event)
logging.LogWithFields("HANDL-wQDL2", "seq", event.Sequence).OnError(err).Warn("reduce failed")
}