Skip to content

Commit

Permalink
prevent to close internal database on close gitcoin provider, close t…
Browse files Browse the repository at this point in the history
…he provider if the data is updated
  • Loading branch information
lucasmenendez committed Apr 17, 2024
1 parent 03475bd commit e037e63
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
7 changes: 5 additions & 2 deletions scanner/providers/farcaster/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ func (p *FarcasterProvider) Init(globalCtx context.Context, iconf any) error {
}
p.contracts.idRegistrySynced.Store(false)
p.contracts.keyRegistrySynced.Store(false)
// start the internal scanner
p.scannerCtx, p.cancelScanner = context.WithCancel(globalCtx)
// to prevent that the scanner stops when the context is canceled, we create
// a new context and a cancel function to stop the scanner when the context
// is canceled
p.scannerCtx, p.cancelScanner = context.WithCancel(context.Background())
if !downloading.Load() {
// if no scanner is running, start this one
go p.initInternalScanner()
}
return nil
Expand Down
23 changes: 15 additions & 8 deletions scanner/providers/gitcoin/gitcoin_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (g *GitcoinPassport) HoldersBalances(ctx context.Context, stamp []byte, _ u
defer cancel()
// get the current scores from the db, handle the case when the stamp is
// empty and when it is not to get the scores from the db
synced := g.isSynced(true)
synced := g.isSynced(ctx, true)
totalSupply := big.NewInt(0)
currentScores := make(map[common.Address]*big.Int)
if len(stamp) > 0 {
Expand Down Expand Up @@ -193,7 +193,7 @@ func (g *GitcoinPassport) Close() error {
g.cancel()
g.waiter.Wait()
close(g.scoresChan)
return g.db.Close()
return nil
}

// IsExternal returns true because Gitcoin Passport is an external provider.
Expand All @@ -203,7 +203,10 @@ func (g *GitcoinPassport) IsExternal() bool {

// IsSynced returns true if the balances are not empty.
func (g *GitcoinPassport) IsSynced(_ []byte) bool {
return g.isSynced(false)
ctx, cancel := context.WithTimeout(context.Background(), metadataTimeout)
defer cancel()
synced := g.isSynced(ctx, false)
return synced
}

// Address returns the address of the Gitcoin Passport contract.
Expand Down Expand Up @@ -406,11 +409,11 @@ func (g *GitcoinPassport) updateLastSync(ctx context.Context) error {
return nil
}

func (g *GitcoinPassport) isSynced(update bool) bool {
func (g *GitcoinPassport) isSynced(ctx context.Context, update bool) bool {
if !update {
return g.synced.Load()
}
lastSync, err := g.loadLastSync(g.ctx)
lastSync, err := g.loadLastSync(ctx)
if err != nil {
log.Warnw("error loading last sync time", "err", err)
return g.synced.Load()
Expand All @@ -423,20 +426,24 @@ func (g *GitcoinPassport) isSynced(update bool) bool {
}

func (g *GitcoinPassport) startScoreUpdates() {
log.Debug("starting Gitcoin Passport score updates")
g.waiter.Add(1)
go func() {
defer g.waiter.Done()
defer func() {
if err := g.Close(); err != nil {
log.Warnw("error closing Gitcoin Passport", "err", err)
}
}()
for {
select {
case <-g.ctx.Done():
return
default:
lastSync := time.Unix(g.lastSyncedTime.Load(), 0)
if time.Since(lastSync).Abs() < g.cooldown {
time.Sleep(30 * time.Second)
continue
return
}
log.Debug("starting Gitcoin Passport score updates")
if err := g.updateScores(); err != nil {
log.Warnw("error updating Gitcoin Passport scores", "err", err)
}
Expand Down

0 comments on commit e037e63

Please sign in to comment.