zitadel/internal/management/repository/eventsourcing/handler/machine_keys.go
Silvan dd5e4acd24
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
2020-12-18 16:47:45 +01:00

111 lines
2.9 KiB
Go

package handler
import (
"time"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/eventstore/models"
es_models "github.com/caos/zitadel/internal/eventstore/models"
"github.com/caos/zitadel/internal/eventstore/query"
"github.com/caos/zitadel/internal/eventstore/spooler"
"github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
usr_model "github.com/caos/zitadel/internal/user/repository/view/model"
)
const (
machineKeysTable = "management.machine_keys"
)
type MachineKeys struct {
handler
subscription *eventstore.Subscription
}
func newMachineKeys(handler handler) *MachineKeys {
h := &MachineKeys{
handler: handler,
}
h.subscribe()
return h
}
func (m *MachineKeys) subscribe() {
m.subscription = m.es.Subscribe(m.AggregateTypes()...)
go func() {
for event := range m.subscription.Events {
query.ReduceEvent(m, event)
}
}()
}
func (d *MachineKeys) ViewModel() string {
return machineKeysTable
}
func (_ *MachineKeys) AggregateTypes() []es_models.AggregateType {
return []es_models.AggregateType{model.UserAggregate}
}
func (k *MachineKeys) CurrentSequence(event *models.Event) (uint64, error) {
sequence, err := k.view.GetLatestMachineKeySequence(string(event.AggregateType))
if err != nil {
return 0, err
}
return sequence.CurrentSequence, nil
}
func (d *MachineKeys) EventQuery() (*models.SearchQuery, error) {
sequence, err := d.view.GetLatestMachineKeySequence("")
if err != nil {
return nil, err
}
return es_models.NewSearchQuery().
AggregateTypeFilter(d.AggregateTypes()...).
LatestSequenceFilter(sequence.CurrentSequence), nil
}
func (d *MachineKeys) Reduce(event *models.Event) (err error) {
switch event.AggregateType {
case model.UserAggregate:
err = d.processMachineKeys(event)
}
return err
}
func (d *MachineKeys) processMachineKeys(event *models.Event) (err error) {
key := new(usr_model.MachineKeyView)
switch event.Type {
case model.MachineKeyAdded:
err = key.AppendEvent(event)
if key.ExpirationDate.Before(time.Now()) {
return d.view.ProcessedMachineKeySequence(event)
}
case model.MachineKeyRemoved:
err = key.SetData(event)
if err != nil {
return err
}
return d.view.DeleteMachineKey(key.ID, event)
case model.UserRemoved:
return d.view.DeleteMachineKeysByUserID(event.AggregateID, event)
default:
return d.view.ProcessedMachineKeySequence(event)
}
if err != nil {
return err
}
return d.view.PutMachineKey(key, event)
}
func (d *MachineKeys) OnError(event *models.Event, err error) error {
logging.LogWithFields("SPOOL-S9fe", "id", event.AggregateID).WithError(err).Warn("something went wrong in machine key handler")
return spooler.HandleError(event, err, d.view.GetLatestMachineKeyFailedEvent, d.view.ProcessedMachineKeyFailedEvent, d.view.ProcessedMachineKeySequence, d.errorCountUntilSkip)
}
func (d *MachineKeys) OnSuccess() error {
return spooler.HandleSuccess(d.view.UpdateMachineKeySpoolerRunTimestamp)
}