2020-05-18 10:06:36 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-03-28 08:05:09 +00:00
|
|
|
"context"
|
2020-07-28 07:42:21 +00:00
|
|
|
"time"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/auth/repository/eventsourcing/view"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2024-01-25 16:28:20 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2023-10-19 10:19:10 +00:00
|
|
|
handler2 "github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-04-26 23:01:45 +00:00
|
|
|
query2 "github.com/zitadel/zitadel/internal/query"
|
2020-05-18 10:06:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2023-10-19 10:19:10 +00:00
|
|
|
Client *database.DB
|
|
|
|
Eventstore *eventstore.Eventstore
|
2020-05-18 10:06:36 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
BulkLimit uint64
|
|
|
|
FailureCountUntilSkip uint64
|
|
|
|
HandleActiveInstances time.Duration
|
|
|
|
TransactionDuration time.Duration
|
|
|
|
Handlers map[string]*ConfigOverwrites
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
type ConfigOverwrites struct {
|
|
|
|
MinimumCycleDuration time.Duration
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
2020-07-28 07:42:21 +00:00
|
|
|
|
2024-01-25 16:28:20 +00:00
|
|
|
var projections []*handler.Handler
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func Register(ctx context.Context, configs Config, view *view.View, queries *query2.Queries) {
|
2024-01-25 16:28:20 +00:00
|
|
|
projections = append(projections, newUser(ctx,
|
2023-10-19 10:19:10 +00:00
|
|
|
configs.overwrite("User"),
|
|
|
|
view,
|
|
|
|
queries,
|
2024-01-25 16:28:20 +00:00
|
|
|
))
|
2020-07-28 07:42:21 +00:00
|
|
|
|
2024-01-25 16:28:20 +00:00
|
|
|
projections = append(projections, newUserSession(ctx,
|
2023-10-19 10:19:10 +00:00
|
|
|
configs.overwrite("UserSession"),
|
|
|
|
view,
|
|
|
|
queries,
|
2024-01-25 16:28:20 +00:00
|
|
|
))
|
2020-12-22 11:27:55 +00:00
|
|
|
|
2024-01-25 16:28:20 +00:00
|
|
|
projections = append(projections, newToken(ctx,
|
2023-10-19 10:19:10 +00:00
|
|
|
configs.overwrite("Token"),
|
|
|
|
view,
|
2024-01-25 16:28:20 +00:00
|
|
|
))
|
2022-03-28 08:05:09 +00:00
|
|
|
|
2024-01-25 16:28:20 +00:00
|
|
|
projections = append(projections, newRefreshToken(ctx,
|
2023-10-19 10:19:10 +00:00
|
|
|
configs.overwrite("RefreshToken"),
|
|
|
|
view,
|
2024-01-25 16:28:20 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Start(ctx context.Context) {
|
|
|
|
for _, projection := range projections {
|
|
|
|
projection.Start(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Projections() []*handler2.Handler {
|
|
|
|
return projections
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
2022-07-22 10:08:39 +00:00
|
|
|
|
2024-05-30 09:35:30 +00:00
|
|
|
func ProjectInstance(ctx context.Context) error {
|
|
|
|
for _, projection := range projections {
|
|
|
|
_, err := projection.Trigger(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (config Config) overwrite(viewModel string) handler2.Config {
|
|
|
|
c := handler2.Config{
|
|
|
|
Client: config.Client,
|
|
|
|
Eventstore: config.Eventstore,
|
|
|
|
BulkLimit: uint16(config.BulkLimit),
|
|
|
|
RequeueEvery: 3 * time.Minute,
|
|
|
|
HandleActiveInstances: config.HandleActiveInstances,
|
|
|
|
MaxFailureCount: uint8(config.FailureCountUntilSkip),
|
|
|
|
TransactionDuration: config.TransactionDuration,
|
|
|
|
}
|
|
|
|
overwrite, ok := config.Handlers[viewModel]
|
|
|
|
if !ok {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
if overwrite.MinimumCycleDuration > 0 {
|
|
|
|
c.RequeueEvery = overwrite.MinimumCycleDuration
|
2022-07-22 10:08:39 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return c
|
2022-07-22 10:08:39 +00:00
|
|
|
}
|