From 9f335b0d1e8b4a37ce5b3feff458e4fe68cc0442 Mon Sep 17 00:00:00 2001 From: Gregor Podlogar Date: Mon, 2 Dec 2024 12:41:53 +0100 Subject: [PATCH] waiting for db from main --- client/collector/collector.go | 8 +++----- main/main.go | 9 +++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/client/collector/collector.go b/client/collector/collector.go index 0441f10..8dadbcf 100644 --- a/client/collector/collector.go +++ b/client/collector/collector.go @@ -123,8 +123,6 @@ func New(user *config.UserRaw, system *config.System, sharedDataPipes *shared.Da // Run starts SigningPolicyInitializedListener, BitVoteListener, and AttestationRequestListener in go routines. func (c *Collector) Run(ctx context.Context) { - WaitForDBToSync(ctx, c.DB) - go SigningPolicyInitializedListener(ctx, c.DB, c.RelayContractAddress, c.VoterRegistryContractAddress, c.SigningPolicies) go AttestationRequestListener(ctx, c.DB, c.FdcContractAddress, requestListenerInterval, c.Requests) @@ -134,13 +132,13 @@ func (c *Collector) Run(ctx context.Context) { } // WaitForDBToSync waits for db to sync. After many unsuccessful attempts it panics. -func WaitForDBToSync(ctx context.Context, db *gorm.DB) { +func (c *Collector) WaitForDBToSync(ctx context.Context) { k := 0 for k < syncRetry { if k > 0 { logger.Debugf("Checking database for %v/%v time", k, syncRetry) } - state, err := database.FetchState(ctx, db, nil) + state, err := database.FetchState(ctx, c.DB, nil) if err != nil { logger.Panic("database error:", err) } @@ -162,7 +160,7 @@ func WaitForDBToSync(ctx context.Context, db *gorm.DB) { } logger.Warnf("Checking database for the final time") - state, err := database.FetchState(ctx, db, nil) + state, err := database.FetchState(ctx, c.DB, nil) if err != nil { logger.Panic("database error:", err) } diff --git a/main/main.go b/main/main.go index 3336f40..11b26b5 100644 --- a/main/main.go +++ b/main/main.go @@ -47,15 +47,16 @@ func main() { sharedDataPipes := shared.NewDataPipes() // Start attestation client collector - collector := collector.New(userConfigRaw, systemConfig, sharedDataPipes) - collector.Run(ctx) + col := collector.New(userConfigRaw, systemConfig, sharedDataPipes) + col.WaitForDBToSync(ctx) + go col.Run(ctx) // Start attestation client manager - manager, err := manager.New(userConfigRaw, attestationTypeConfig, sharedDataPipes) + mngr, err := manager.New(userConfigRaw, attestationTypeConfig, sharedDataPipes) if err != nil { logger.Panicf("failed to create the manager: %s", err) } - go manager.Run(ctx) + go mngr.Run(ctx) // Run attestation client server srv := server.New(&sharedDataPipes.Rounds, userConfigRaw.ProtocolID, userConfigRaw.RestServer)