sync providers concurrently instead of in series
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Steven Polley 2024-09-01 07:27:49 -06:00
parent 39f3b27a8b
commit a7d0005423

33
main.go
View File

@ -86,22 +86,29 @@ func refreshData() {
} }
lastRefresh = time.Now() lastRefresh = time.Now()
var wg *sync.WaitGroup
// Loop through each configured account provider and attempt to get the account balances, and update YNAB // Loop through each configured account provider and attempt to get the account balances, and update YNAB
for _, p := range configuredProviders { for _, p := range configuredProviders {
balances, accountIDs, err := p.GetBalances() wg.Add(1)
if err != nil { go func() {
log.Printf("failed to get balances with provider '%s': %v", p.Name(), err) defer wg.Done()
continue balances, accountIDs, err := p.GetBalances()
}
if len(balances) != len(accountIDs) {
log.Printf("'%s' provider data validation error: mismatched balance and accountID slice lengths - expected the same: balances length = %d, accountIDs length = %d", p.Name(), len(balances), len(accountIDs))
continue
}
for i := range balances {
err = ynabClient.SetAccountBalance(accountIDs[i], balances[i])
if err != nil { if err != nil {
log.Printf("failed to update ynab account '%s' balance: %v", accountIDs[i], err) log.Printf("failed to get balances with provider '%s': %v", p.Name(), err)
return
} }
} if len(balances) != len(accountIDs) {
log.Printf("'%s' provider data validation error: mismatched balance and accountID slice lengths - expected the same: balances length = %d, accountIDs length = %d", p.Name(), len(balances), len(accountIDs))
return
}
for i := range balances {
err = ynabClient.SetAccountBalance(accountIDs[i], balances[i])
if err != nil {
log.Printf("failed to update ynab account '%s' balance: %v", accountIDs[i], err)
}
}
}()
} }
wg.Wait()
} }