private void SetExpirationTime(ICollection <CurrencyRate> rates)
 {
     foreach (var rate in rates)
     {
         rate.ExpirationTime = timetable.GetNextMoment(rate.TimeOfReceipt) + lagToExpiration;
     }
 }
예제 #2
0
        private async Task WorkCycle()
        {
            var watch         = new Stopwatch();
            var currentMoment = UtcNow;

            while (true)
            {
                watch.Restart();
                try
                {
                    cts.Token.ThrowIfCancellationRequested();

                    logger.LogDebug("Updating rates...");
                    await ratesUpdater.UpdateRates(cts.Token);

                    watch.Stop();
                    logger.LogDebug($"Updating takes {watch.ElapsedMilliseconds} ms.");

                    var nextMoment = timetable.GetNextMoment(currentMoment);
                    var now        = UtcNow;

                    // should already be started
                    if (nextMoment <= now)
                    {
                        currentMoment = now;
                        continue;
                    }
                    // need to wait next moment
                    logger.LogDebug($"Lets sleep {nextMoment - now}");
                    await Task.Delay(nextMoment - now, cts.Token);

                    currentMoment = nextMoment;
                }
                catch (OperationCanceledException)
                {
                    break;
                }
                catch (Exception e)
                {
                    logger.LogError(e, e.Message);
                }
            }

            stopped.Set();
        }