Skip to content

Commit 4996708

Browse files
authored
Persist last known exchange rates even if rate provider is down (#163)
1 parent cc751cd commit 4996708

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

ocp/worker/currency/exchangerate/runtime.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ import (
1313

1414
currency_lib "github.com/code-payments/ocp-server/currency"
1515
ocp_data "github.com/code-payments/ocp-server/ocp/data"
16+
"github.com/code-payments/ocp-server/ocp/data/currency"
1617
"github.com/code-payments/ocp-server/ocp/worker"
1718
)
1819

1920
type exchangeRateRuntime struct {
2021
log *zap.Logger
2122
data ocp_data.Provider
23+
24+
lastRates *currency.MultiRateRecord
2225
}
2326

2427
func New(log *zap.Logger, data ocp_data.Provider) worker.Runtime {
@@ -70,7 +73,21 @@ func (p *exchangeRateRuntime) Start(runtimeCtx context.Context, interval time.Du
7073
func (p *exchangeRateRuntime) GetCurrentExchangeRates(ctx context.Context) error {
7174
data, err := p.data.GetCurrentExchangeRatesFromExternalProviders(ctx)
7275
if err != nil {
73-
return errors.Wrap(err, "failed to get current rate data")
76+
p.log.With(zap.Error(err)).Warn("failed to get current rate data, persisting last known rates")
77+
78+
lastRates := p.lastRates
79+
if lastRates == nil {
80+
lastRates, _ = p.data.GetAllExchangeRates(ctx, time.Now())
81+
}
82+
83+
if lastRates == nil {
84+
return errors.Wrap(err, "failed to get current rate data")
85+
}
86+
87+
data = &currency.MultiRateRecord{
88+
Time: time.Now(),
89+
Rates: lastRates.Rates,
90+
}
7491
}
7592

7693
delete(data.Rates, string(currency_lib.BTC))
@@ -97,5 +114,7 @@ func (p *exchangeRateRuntime) GetCurrentExchangeRates(ctx context.Context) error
97114
return errors.Wrap(err, "failed to store rate data")
98115
}
99116

117+
p.lastRates = data
118+
100119
return nil
101120
}

0 commit comments

Comments
 (0)