Skip to content

Commit

Permalink
don't halt on first price fetch failure.
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Dec 16, 2023
1 parent e8ca874 commit f36a063
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ananthakumaran/paisa/internal/scraper"
"github.com/ananthakumaran/paisa/internal/scraper/india"
"github.com/ananthakumaran/paisa/internal/scraper/mutualfund"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -67,7 +68,9 @@ func SyncJournal(db *gorm.DB) (string, error) {
func SyncCommodities(db *gorm.DB) error {
AutoMigrate(db)
log.Info("Fetching commodities price history")
commodities := commodity.All()
commodities := lo.Shuffle(commodity.All())

var errors []error
for _, commodity := range commodities {
name := commodity.Name
log.Info("Fetching commodity ", name)
Expand All @@ -80,11 +83,20 @@ func SyncCommodities(db *gorm.DB) error {

if err != nil {
log.Error(err)
return fmt.Errorf("Failed to fetch price for %s: %w", name, err)
errors = append(errors, fmt.Errorf("Failed to fetch price for %s: %w", name, err))
continue
}

price.UpsertAllByTypeNameAndID(db, commodity.Type, name, code, prices)
}

if len(errors) > 0 {
var message string
for _, error := range errors {
message += error.Error() + "\n"
}
return fmt.Errorf(strings.Trim(message, "\n"))
}
return nil
}

Expand Down

0 comments on commit f36a063

Please sign in to comment.