Delay download of historical tickers by one hour

If the download is initiated too early, the provider does not yet
have the historical tokens ready.
This commit is contained in:
Martin Boehm 2022-09-12 16:19:43 +02:00 committed by Martin
parent f24c83b33a
commit 3932d19707
2 changed files with 7 additions and 5 deletions

View File

@ -105,7 +105,7 @@ func (cg *Coingecko) makeReq(url string) ([]byte, error) {
return nil, err
}
// if there is a throttling error, wait 60 seconds and retry
glog.Errorf("Coingecko makeReq %v error %v, will retry in 60 seconds", url, err)
glog.Warningf("Coingecko makeReq %v error %v, will retry in 60 seconds", url, err)
time.Sleep(60 * time.Second)
}
}

View File

@ -95,7 +95,9 @@ func (rd *RatesDownloader) Run() error {
rd.callbackOnNewTicker(tickers)
}
}
if time.Now().UTC().YearDay() != lastHistoricalTickers.YearDay() || time.Now().UTC().Year() != lastHistoricalTickers.Year() {
now := time.Now().UTC()
// once a day, 1 hour after UTC midnight (to let the provider prepare historical rates) update historical tickers
if (now.YearDay() != lastHistoricalTickers.YearDay() || now.Year() != lastHistoricalTickers.Year()) && now.Hour() > 0 {
err = rd.downloader.UpdateHistoricalTickers()
if err != nil {
glog.Error("FiatRatesDownloader: UpdateHistoricalTickers error ", err)
@ -127,10 +129,10 @@ func (rd *RatesDownloader) Run() error {
}
}
// wait for the next run with a slight random value to avoid too many request at the same time
now := time.Now().Unix()
next := now + rd.periodSeconds
unix := time.Now().Unix()
next := unix + rd.periodSeconds
next -= next % rd.periodSeconds
next += int64(rand.Intn(12))
time.Sleep(time.Duration(next-now) * time.Second)
time.Sleep(time.Duration(next-unix) * time.Second)
}
}