Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
chore(sync): make initial delay configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbes committed Aug 28, 2024
1 parent 54253b5 commit 9c1c83f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/nada-backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func main() {
MetabaseCollectionsFrequency,
zlog.With().Str("subsystem", "metabase_collections_syncer").Logger(),
)
go collectionSyncer.Run(ctx)
go collectionSyncer.Run(ctx, 60)

go access_ensurer.NewEnsurer(
googleGroups,
Expand Down
23 changes: 18 additions & 5 deletions pkg/syncers/metabase_collections/metabase_collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ type Syncer struct {
syncInterval time.Duration
}

func (s *Syncer) Run(ctx context.Context) {
ticker := time.NewTicker(s.syncInterval)

func (s *Syncer) Run(ctx context.Context, initialDelaySec int) {
isLeader, err := leaderelection.IsLeader()
if err != nil {
s.log.Error().Err(err).Msg("checking leader status")
return
}

if isLeader {
// Delay a little before starting
time.Sleep(60 * time.Second)
time.Sleep(time.Duration(initialDelaySec) * time.Second)

// Do an initial sync
s.log.Info().Msg("running initial metabase collections syncer")
Expand All @@ -45,6 +44,8 @@ func (s *Syncer) Run(ctx context.Context) {
s.log.Info().Msg("not leader, skipping metabase collections sync")
}

ticker := time.NewTicker(s.syncInterval)

defer ticker.Stop()
for {
select {
Expand Down Expand Up @@ -177,17 +178,29 @@ func (s *Syncer) AddRestrictedTagToCollections(ctx context.Context) error {
collectionByID[collection.ID] = collection
}

s.log.Info().Msgf("collections: %v", collections)

for _, meta := range metas {
s.log.Debug().Msgf("meta: %v", meta)

if meta.SyncCompleted != nil && *meta.CollectionID != 0 {
collection, ok := collectionByID[*meta.CollectionID]
if !ok {
continue
}

if !strings.Contains(collection.Name, service.MetabaseRestrictedCollectionTag) {
newName := fmt.Sprintf("%s %s", collection.Name, service.MetabaseRestrictedCollectionTag)

s.log.Info().Fields(map[string]interface{}{
"collection_id": collection.ID,
"existing_name": collection.Name,
"new_name": newName,
}).Msg("adding_restricted_tag")

err := s.api.UpdateCollection(ctx, &service.MetabaseCollection{
ID: collection.ID,
Name: fmt.Sprintf("%s %s", collection.Name, service.MetabaseRestrictedCollectionTag),
Name: newName,
})
if err != nil {
return errs.E(op, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func TestSyncer_Run(t *testing.T) {
tc.setupAPI(api)
tc.setupStorage(storage)

go syncer.Run(ctx)
go syncer.Run(ctx, 0)
time.Sleep(2 * time.Second)

// Check logs for expected messages
Expand Down
2 changes: 1 addition & 1 deletion test/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func NewMetabaseConfig() *MetabaseConfig {
func (c *containers) RunMetabase(cfg *MetabaseConfig) *MetabaseConfig {
metabaseVersion := os.Getenv("METABASE_VERSION")
if metabaseVersion == "" {
metabaseVersion = "v1.50.20"
metabaseVersion = "v1.50.21"
}

resource, err := c.pool.RunWithOptions(&dockertest.RunOptions{
Expand Down
3 changes: 2 additions & 1 deletion test/integration/metabase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestMetabase(t *testing.T) {
defer cancel()

log := zerolog.New(zerolog.NewConsoleWriter())
log.Level(zerolog.DebugLevel)

c := NewContainers(t, log)
defer c.Cleanup()
Expand Down Expand Up @@ -247,7 +248,7 @@ func TestMetabase(t *testing.T) {
require.NoError(t, err)

collectionSyncer := metabase_collections.New(mbapi, stores.MetaBaseStorage, 1, log)
go collectionSyncer.Run(ctx)
go collectionSyncer.Run(ctx, 0)
time.Sleep(5 * time.Second)

collections, err := mbapi.GetCollections(ctx)
Expand Down

0 comments on commit 9c1c83f

Please sign in to comment.