Move custom var sync to a new method

This commit is contained in:
Eric Lippmann 2021-10-14 15:05:24 +02:00
parent 5b18a7dd93
commit e433aa7ec3
2 changed files with 38 additions and 30 deletions

View file

@ -4,7 +4,6 @@ import (
"context"
"github.com/go-redis/redis/v8"
"github.com/icinga/icingadb/internal/command"
"github.com/icinga/icingadb/pkg/com"
"github.com/icinga/icingadb/pkg/common"
"github.com/icinga/icingadb/pkg/icingadb"
"github.com/icinga/icingadb/pkg/icingadb/history"
@ -205,35 +204,7 @@ func run() int {
return synctx.Err()
}
logger.Info("Syncing customvar")
logger.Info("Syncing customvar_flat")
cv := common.NewSyncSubject(v1.NewCustomvar)
cvs, errs := rc.YieldAll(synctx, cv)
com.ErrgroupReceive(g, errs)
desiredCvs, desiredFlatCvs, errs := v1.ExpandCustomvars(synctx, cvs)
com.ErrgroupReceive(g, errs)
actualCvs, errs := db.YieldAll(
synctx, cv.Factory(), db.BuildSelectStmt(cv.Entity(), cv.Entity().Fingerprint()))
com.ErrgroupReceive(g, errs)
g.Go(func() error {
return s.ApplyDelta(synctx, icingadb.NewDelta(synctx, actualCvs, desiredCvs, cv, logs.GetChildLogger("config-sync")))
})
flatCv := common.NewSyncSubject(v1.NewCustomvarFlat)
actualFlatCvs, errs := db.YieldAll(
synctx, flatCv.Factory(), db.BuildSelectStmt(flatCv.Entity(), flatCv.Entity().Fingerprint()))
com.ErrgroupReceive(g, errs)
g.Go(func() error {
return s.ApplyDelta(synctx, icingadb.NewDelta(synctx, actualFlatCvs, desiredFlatCvs, flatCv, logs.GetChildLogger("config-sync")))
})
return nil
return s.SyncCustomvars(synctx)
})
g.Go(func() error {

View file

@ -6,6 +6,7 @@ import (
"github.com/icinga/icingadb/pkg/com"
"github.com/icinga/icingadb/pkg/common"
"github.com/icinga/icingadb/pkg/contracts"
v1 "github.com/icinga/icingadb/pkg/icingadb/v1"
"github.com/icinga/icingadb/pkg/icingaredis"
"github.com/icinga/icingadb/pkg/utils"
"github.com/pkg/errors"
@ -157,3 +158,39 @@ func (s Sync) ApplyDelta(ctx context.Context, delta *Delta) error {
return g.Wait()
}
// SyncCustomvars synchronizes customvar and customvar_flat.
func (s Sync) SyncCustomvars(ctx context.Context) error {
s.logger.Info("Syncing customvar")
s.logger.Info("Syncing customvar_flat")
g, ctx := errgroup.WithContext(ctx)
cv := common.NewSyncSubject(v1.NewCustomvar)
cvs, errs := s.redis.YieldAll(ctx, cv)
com.ErrgroupReceive(g, errs)
desiredCvs, desiredFlatCvs, errs := v1.ExpandCustomvars(ctx, cvs)
com.ErrgroupReceive(g, errs)
actualCvs, errs := s.db.YieldAll(
ctx, cv.Factory(), s.db.BuildSelectStmt(cv.Entity(), cv.Entity().Fingerprint()))
com.ErrgroupReceive(g, errs)
g.Go(func() error {
return s.ApplyDelta(ctx, NewDelta(ctx, actualCvs, desiredCvs, cv, s.logger))
})
flatCv := common.NewSyncSubject(v1.NewCustomvarFlat)
actualFlatCvs, errs := s.db.YieldAll(
ctx, flatCv.Factory(), s.db.BuildSelectStmt(flatCv.Entity(), flatCv.Entity().Fingerprint()))
com.ErrgroupReceive(g, errs)
g.Go(func() error {
return s.ApplyDelta(ctx, NewDelta(ctx, actualFlatCvs, desiredFlatCvs, flatCv, s.logger))
})
return g.Wait()
}