mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 18:17:35 +00:00
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:
@@ -1,11 +1,13 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/view/model"
|
||||
"github.com/jinzhu/gorm"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
int_model "github.com/caos/zitadel/internal/model"
|
||||
"github.com/caos/zitadel/internal/view/model"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
type CurrentSequence struct {
|
||||
@@ -13,6 +15,18 @@ type CurrentSequence struct {
|
||||
CurrentSequence uint64 `gorm:"column:current_sequence"`
|
||||
EventTimestamp time.Time `gorm:"column:event_timestamp"`
|
||||
LastSuccessfulSpoolerRun time.Time `gorm:"column:last_successful_spooler_run"`
|
||||
AggregateType string `gorm:"column:aggregate_type;primary_key"`
|
||||
}
|
||||
|
||||
type currentSequenceViewWithSequence struct {
|
||||
ViewName string `gorm:"column:view_name;primary_key"`
|
||||
CurrentSequence uint64 `gorm:"column:current_sequence"`
|
||||
LastSuccessfulSpoolerRun time.Time `gorm:"column:last_successful_spooler_run"`
|
||||
}
|
||||
|
||||
type currentSequenceView struct {
|
||||
ViewName string `gorm:"column:view_name;primary_key"`
|
||||
LastSuccessfulSpoolerRun time.Time `gorm:"column:last_successful_spooler_run"`
|
||||
}
|
||||
|
||||
type SequenceSearchKey int32
|
||||
@@ -20,6 +34,7 @@ type SequenceSearchKey int32
|
||||
const (
|
||||
SequenceSearchKeyUndefined SequenceSearchKey = iota
|
||||
SequenceSearchKeyViewName
|
||||
SequenceSearchKeyAggregateType
|
||||
)
|
||||
|
||||
type sequenceSearchKey SequenceSearchKey
|
||||
@@ -28,11 +43,30 @@ func (key sequenceSearchKey) ToColumnName() string {
|
||||
switch SequenceSearchKey(key) {
|
||||
case SequenceSearchKeyViewName:
|
||||
return "view_name"
|
||||
case SequenceSearchKeyAggregateType:
|
||||
return "aggregate_type"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
type sequenceSearchQuery struct {
|
||||
key sequenceSearchKey
|
||||
value string
|
||||
}
|
||||
|
||||
func (q *sequenceSearchQuery) GetKey() ColumnKey {
|
||||
return q.key
|
||||
}
|
||||
|
||||
func (q *sequenceSearchQuery) GetMethod() int_model.SearchMethod {
|
||||
return int_model.SearchMethodEquals
|
||||
}
|
||||
|
||||
func (q *sequenceSearchQuery) GetValue() interface{} {
|
||||
return q.value
|
||||
}
|
||||
|
||||
func CurrentSequenceToModel(sequence *CurrentSequence) *model.View {
|
||||
dbView := strings.Split(sequence.ViewName, ".")
|
||||
return &model.View{
|
||||
@@ -41,26 +75,44 @@ func CurrentSequenceToModel(sequence *CurrentSequence) *model.View {
|
||||
CurrentSequence: sequence.CurrentSequence,
|
||||
EventTimestamp: sequence.EventTimestamp,
|
||||
LastSuccessfulSpoolerRun: sequence.LastSuccessfulSpoolerRun,
|
||||
AggregateType: sequence.AggregateType,
|
||||
}
|
||||
}
|
||||
|
||||
func SaveCurrentSequence(db *gorm.DB, table, viewName string, sequence uint64, eventTimestamp time.Time) error {
|
||||
return UpdateCurrentSequence(db, table, &CurrentSequence{viewName, sequence, eventTimestamp, time.Now()})
|
||||
func SaveCurrentSequence(db *gorm.DB, table, viewName, aggregateType string, sequence uint64, eventTimestamp time.Time) error {
|
||||
return UpdateCurrentSequence(db, table, &CurrentSequence{viewName, sequence, eventTimestamp, time.Now(), aggregateType})
|
||||
}
|
||||
|
||||
func UpdateCurrentSequence(db *gorm.DB, table string, currentSequence *CurrentSequence) error {
|
||||
save := PrepareSave(table)
|
||||
err := save(db, currentSequence)
|
||||
func UpdateCurrentSequence(db *gorm.DB, table string, currentSequence *CurrentSequence) (err error) {
|
||||
var seq interface{} = currentSequence
|
||||
if currentSequence.AggregateType == "" && currentSequence.CurrentSequence > 0 {
|
||||
//spooler run
|
||||
seq = ¤tSequenceView{ViewName: currentSequence.ViewName, LastSuccessfulSpoolerRun: currentSequence.LastSuccessfulSpoolerRun}
|
||||
} else if currentSequence.AggregateType == "" {
|
||||
//reset current sequence on view
|
||||
seq = ¤tSequenceViewWithSequence{ViewName: currentSequence.ViewName, LastSuccessfulSpoolerRun: currentSequence.LastSuccessfulSpoolerRun, CurrentSequence: currentSequence.CurrentSequence}
|
||||
}
|
||||
|
||||
save := PrepareSave(table)
|
||||
err = save(db, seq)
|
||||
if err != nil {
|
||||
return caos_errs.ThrowInternal(err, "VIEW-5kOhP", "unable to updated processed sequence")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func LatestSequence(db *gorm.DB, table, viewName string) (*CurrentSequence, error) {
|
||||
func LatestSequence(db *gorm.DB, table, viewName, aggregateType string) (*CurrentSequence, error) {
|
||||
searchQueries := make([]SearchQuery, 0, 2)
|
||||
searchQueries = append(searchQueries, &sequenceSearchQuery{key: sequenceSearchKey(SequenceSearchKeyViewName), value: viewName})
|
||||
if aggregateType != "" {
|
||||
searchQueries = append(searchQueries, &sequenceSearchQuery{key: sequenceSearchKey(SequenceSearchKeyAggregateType), value: aggregateType})
|
||||
} else {
|
||||
// ensure highest sequence of view
|
||||
db = db.Order("current_sequence DESC")
|
||||
}
|
||||
|
||||
query := PrepareGetByQuery(table, searchQueries...)
|
||||
sequence := new(CurrentSequence)
|
||||
query := PrepareGetByKey(table, sequenceSearchKey(SequenceSearchKeyViewName), viewName)
|
||||
err := query(db, sequence)
|
||||
|
||||
if err == nil {
|
||||
@@ -89,5 +141,5 @@ func ClearView(db *gorm.DB, truncateView, sequenceTable string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return SaveCurrentSequence(db, sequenceTable, truncateView, 0, time.Now())
|
||||
return SaveCurrentSequence(db, sequenceTable, truncateView, "", 0, time.Now())
|
||||
}
|
||||
|
Reference in New Issue
Block a user