mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-22 22:25:28 +01:00
Better handle errors on BackgroundFetcherRateProvider
This commit is contained in:
parent
058ccf56d0
commit
929a0c37bd
1 changed files with 14 additions and 16 deletions
|
@ -13,15 +13,15 @@ namespace BTCPayServer.Services.Rates
|
|||
public class LatestFetch
|
||||
{
|
||||
public ExchangeRates Latest;
|
||||
public DateTimeOffset Timestamp;
|
||||
public DateTimeOffset NextRefresh;
|
||||
public DateTimeOffset Expiration;
|
||||
public Exception Exception;
|
||||
|
||||
internal ExchangeRates GetResult()
|
||||
{
|
||||
if(Expiration < DateTimeOffset.UtcNow)
|
||||
if (Expiration <= DateTimeOffset.UtcNow)
|
||||
{
|
||||
if(Exception != null)
|
||||
if (Exception != null)
|
||||
{
|
||||
ExceptionDispatchInfo.Capture(Exception).Throw();
|
||||
}
|
||||
|
@ -32,14 +32,6 @@ namespace BTCPayServer.Services.Rates
|
|||
}
|
||||
return Latest;
|
||||
}
|
||||
|
||||
internal void CopyFrom(LatestFetch previous)
|
||||
{
|
||||
Latest = previous.Latest;
|
||||
Timestamp = previous.Timestamp;
|
||||
Expiration = previous.Expiration;
|
||||
Exception = previous.Exception;
|
||||
}
|
||||
}
|
||||
|
||||
IRateProvider _Inner;
|
||||
|
@ -58,9 +50,9 @@ namespace BTCPayServer.Services.Rates
|
|||
get
|
||||
{
|
||||
var latest = _Latest;
|
||||
if (latest == null || latest.Exception != null)
|
||||
if (latest == null)
|
||||
return DateTimeOffset.UtcNow;
|
||||
return latest.Timestamp + RefreshRate;
|
||||
return latest.NextRefresh;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -93,16 +85,22 @@ namespace BTCPayServer.Services.Rates
|
|||
var rates = await _Inner.GetRatesAsync();
|
||||
fetch.Latest = rates;
|
||||
fetch.Expiration = DateTimeOffset.UtcNow + ValidatyTime;
|
||||
fetch.NextRefresh = DateTimeOffset.UtcNow + RefreshRate;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if(previous != null)
|
||||
if (previous != null)
|
||||
{
|
||||
fetch.CopyFrom(previous);
|
||||
fetch.Latest = previous.Latest;
|
||||
fetch.Expiration = previous.Expiration;
|
||||
}
|
||||
else
|
||||
{
|
||||
fetch.Expiration = DateTimeOffset.UtcNow;
|
||||
}
|
||||
fetch.NextRefresh = DateTimeOffset.UtcNow;
|
||||
fetch.Exception = ex;
|
||||
}
|
||||
fetch.Timestamp = DateTimeOffset.UtcNow;
|
||||
_Latest = fetch;
|
||||
fetch.GetResult(); // Will throw if not valid
|
||||
return fetch;
|
||||
|
|
Loading…
Add table
Reference in a new issue