public async Task StartAsync()
        {
            await _log.WriteInfoAsync(nameof(StartAsync), "", "Deserializing persistence queue async...");

            var tasks = new List <Task>
            {
                _snapshotSerializer.DeserializeAsync(_persistenceQueue, _persistenceQueueSnapshotRepository)
            };

            await _log.WriteInfoAsync(nameof(StartAsync), "", "Initializing cache from the history async...");

            tasks.Add(_cacheInitalizationService.InitializeCacheAsync());

            await _log.WriteInfoAsync(nameof(StartAsync), "", "Waiting for async tasks...");

            await Task.WhenAll(tasks);

            await _log.WriteInfoAsync(nameof(StartAsync), "", "Starting persistence queue...");

            _persistenceQueue.Start();

            await _log.WriteInfoAsync(nameof(StartAsync), "", "Starting persistence manager...");

            _persistenceManager.Start();

            await _log.WriteInfoAsync(nameof(StartAsync), "", "Starting candles subscriber...");

            _candlesSubscriber.Start();

            await _log.WriteInfoAsync(nameof(StartAsync), "", "Started up");
        }
Ejemplo n.º 2
0
 private async Task ReloadCacheIfNeededAsync()
 {
     if (!await _redisCacheService.CheckCacheValidityAsync())
     {
         await _cacheInitalizationService.InitializeCacheAsync();
     }
 }
Ejemplo n.º 3
0
        public async Task Initialization_caches_each_asset_pairs_in_each_stored_interval_and_in_each_stored_price_type_from_persistent_storage()
        {
            // Arrange
            var now = new DateTime(2017, 06, 23, 15, 35, 20, DateTimeKind.Utc);

            _dateTimeProviderMock.SetupGet(p => p.UtcNow).Returns(now);
            _historyRepositoryMock
            .Setup(r => r.GetCandlesAsync(It.IsAny <string>(), It.IsAny <CandleTimeInterval>(), It.IsAny <CandlePriceType>(), It.IsAny <DateTime>(), It.IsAny <DateTime>()))
            .ReturnsAsync((string a, CandleTimeInterval i, CandlePriceType p, DateTime f, DateTime t) => new[] { new TestCandle(), new TestCandle() });

            // Act
            await _service.InitializeCacheAsync();

            // Assert
            foreach (var interval in StoredIntervals)
            {
                foreach (var priceType in StoredPriceTypes)
                {
                    foreach (var assetPairId in new[] { "EURUSD", "USDCHF" })
                    {
                        _historyRepositoryMock.Verify(r =>
                                                      r.GetCandlesAsync(
                                                          It.Is <string>(a => a == assetPairId),
                                                          It.Is <CandleTimeInterval>(i => i == interval),
                                                          It.Is <CandlePriceType>(p => p == priceType),
                                                          It.Is <DateTime>(d => d == now.TruncateTo(interval).AddIntervalTicks(-AmountOfCandlesToStore, interval)),
                                                          It.Is <DateTime>(d => d == now.TruncateTo(interval))),
                                                      Times.Once);

                        _cacheServiceMock.Verify(s =>
                                                 s.InitializeAsync(
                                                     It.Is <string>(a => a == assetPairId),
                                                     It.Is <CandlePriceType>(p => p == priceType),
                                                     It.Is <CandleTimeInterval>(i => i == interval),
                                                     It.Is <IReadOnlyCollection <ICandle> >(c => c.Count() == 2)),
                                                 Times.Once);
                    }
                }
            }

            _historyRepositoryMock.Verify(r =>
                                          r.GetCandlesAsync(
                                              It.Is <string>(a => !new[] { "EURUSD", "USDCHF" }.Contains(a)),
                                              It.IsAny <CandleTimeInterval>(),
                                              It.IsAny <CandlePriceType>(),
                                              It.IsAny <DateTime>(),
                                              It.IsAny <DateTime>()),
                                          Times.Never);

            _cacheServiceMock.Verify(s =>
                                     s.InitializeAsync(
                                         It.Is <string>(a => !new[] { "EURUSD", "USDCHF" }.Contains(a)),
                                         It.IsAny <CandlePriceType>(),
                                         It.IsAny <CandleTimeInterval>(),
                                         It.IsAny <IReadOnlyCollection <ICandle> >()),
                                     Times.Never);
        }
        public async Task StartAsync()
        {
            await _log.WriteInfoAsync(nameof(StartAsync), "", "Deserializing persistence queue async...");

            var tasks = new List <Task>
            {
                _snapshotSerializer.DeserializeAsync(_persistenceQueue, _persistenceQueueSnapshotRepository)
            };

            if (!_migrationEnabled)
            {
                await _log.WriteInfoAsync(nameof(StartAsync), "", "Initializing cache from the history async...");

                tasks.Add(_cacheInitalizationService.InitializeCacheAsync());
            }

            await _log.WriteInfoAsync(nameof(StartAsync), "", "Waiting for async tasks...");

            await Task.WhenAll(tasks);

            await _log.WriteInfoAsync(nameof(StartAsync), "", "Starting persistence queue...");

            _persistenceQueue.Start();

            await _log.WriteInfoAsync(nameof(StartAsync), "", "Starting persistence manager...");

            _persistenceManager.Start();

            // We can not combine it with the previous if(!_migration...) due to launch order importance.
            if (!_migrationEnabled)
            {
                await _log.WriteInfoAsync(nameof(StartAsync), "", "Starting candles subscriber...");

                _candlesSubscriber.Start();
            }

            await _log.WriteInfoAsync(nameof(StartAsync), "", "Starting cqrs engine subscribers...");

            _cqrsEngine.StartSubscribers();

            await _log.WriteInfoAsync(nameof(StartAsync), "", "Started up");
        }
Ejemplo n.º 5
0
        public async Task StartAsync()
        {
            _log.Info(nameof(StartAsync), "Deserializing persistence queue async...");

            var tasks = new List <Task>
            {
                _snapshotSerializer.DeserializeAsync(_persistenceQueue, _persistenceQueueSnapshotRepository)
            };

            if (!_migrationEnabled)
            {
                _log.Info(nameof(StartAsync), "Initializing cache from the history async...");

                tasks.Add(_cacheInitalizationService.InitializeCacheAsync());
            }

            _log.Info(nameof(StartAsync), "Waiting for async tasks...");

            await Task.WhenAll(tasks);

            _log.Info(nameof(StartAsync), "Starting persistence queue...");

            _persistenceQueue.Start();

            _log.Info(nameof(StartAsync), "Starting persistence manager...");

            _persistenceManager.Start();

            // We can not combine it with the previous if(!_migration...) due to launch order importance.
            if (!_migrationEnabled)
            {
                _log.Info(nameof(StartAsync), "Starting candles subscriber...");

                _candlesSubscriber.Start();

                _cacheCaretaker.Start();  // Should go after cache initialization has finished working && if no migration
            }

            _log.Info(nameof(StartAsync), "Started up");
        }
Ejemplo n.º 6
0
        public async Task Initialization_caches_each_asset_pairs_in_each_stored_interval_and_in_each_stored_price_type_from_persistent_storage()
        {
            // Arrange
            var now = new DateTime(2017, 06, 23, 15, 35, 20, DateTimeKind.Utc);

            _dateTimeProviderMock.SetupGet(p => p.UtcNow).Returns(now);
            _historyRepositoryMock
            .Setup(r => r.GetCandlesAsync(
                       It.IsAny <string>(),
                       It.IsAny <CandleTimeInterval>(),
                       It.IsAny <CandlePriceType>(),
                       It.IsAny <DateTime>(),
                       It.IsAny <DateTime>()))
            .ReturnsAsync((string a, CandleTimeInterval i, CandlePriceType p, DateTime f, DateTime t) =>
                          new[]
            {
                new TestCandle(),
                new TestCandle()
            });
            _historyRepositoryMock
            .Setup(r => r.CanStoreAssetPair(It.Is <string>(a => new[] { "EURUSD", "USDCHF" }.Contains(a))))
            .Returns <string>(a => true);

            _cacheServiceMock
            .Setup(c => c.GetRedisCacheIntervals(It.IsAny <CandleTimeInterval>()))
            .Returns((CandleTimeInterval interval) =>
            {
                switch (interval)
                {
                case CandleTimeInterval.Minute:
                    return(new[] { CandleTimeInterval.Min5, CandleTimeInterval.Min15, CandleTimeInterval.Min30 });

                case CandleTimeInterval.Hour:
                    return(new[]
                    {
                        CandleTimeInterval.Hour, CandleTimeInterval.Hour4, CandleTimeInterval.Hour6,
                        CandleTimeInterval.Hour12
                    });

                case CandleTimeInterval.Day:
                    return(new[] { CandleTimeInterval.Day });

                case CandleTimeInterval.Week:
                    return(new[] { CandleTimeInterval.Week });

                case CandleTimeInterval.Month:
                    return(new[] { CandleTimeInterval.Month });

                default:
                    throw new ArgumentOutOfRangeException(nameof(interval), interval,
                                                          "This interval is not stored in the db");
                }
            });

            // Act
            await _service.InitializeCacheAsync();

            // Assert
            foreach (var interval in StoredIntervals)
            {
                foreach (var priceType in StoredPriceTypes)
                {
                    foreach (var assetPairId in new[] { "EURUSD", "USDCHF" })
                    {
                        _historyRepositoryMock.Verify(r =>
                                                      r.GetCandlesAsync(
                                                          It.Is <string>(a => a == assetPairId),
                                                          It.Is <CandleTimeInterval>(i => i == interval),
                                                          It.Is <CandlePriceType>(p => p == priceType),
                                                          It.Is <DateTime>(d => d == now.TruncateTo(interval).AddIntervalTicks(-_amountOfCandlesToStore[interval], interval)),
                                                          It.Is <DateTime>(d => d == now.TruncateTo(interval).AddIntervalTicks(1, interval))),
                                                      Times.Once);

                        if (interval == CandleTimeInterval.Minute)
                        {
                            continue;
                        }

                        _cacheServiceMock.Verify(s =>
                                                 s.InitializeAsync(
                                                     It.Is <string>(a => a == assetPairId),
                                                     It.Is <CandlePriceType>(p => p == priceType),
                                                     It.Is <CandleTimeInterval>(i => i == interval),
                                                     It.Is <IReadOnlyCollection <ICandle> >(c => c.Count == 2),
                                                     It.IsAny <SlotType>()),
                                                 Times.Once);
                    }
                }
            }

            _historyRepositoryMock.Verify(r =>
                                          r.GetCandlesAsync(
                                              It.Is <string>(a => !new[] { "EURUSD", "USDCHF" }.Contains(a)),
                                              It.IsAny <CandleTimeInterval>(),
                                              It.IsAny <CandlePriceType>(),
                                              It.IsAny <DateTime>(),
                                              It.IsAny <DateTime>()),
                                          Times.Never);

            _cacheServiceMock.Verify(s =>
                                     s.InitializeAsync(
                                         It.Is <string>(a => !new[] { "EURUSD", "USDCHF" }.Contains(a)),
                                         It.IsAny <CandlePriceType>(),
                                         It.IsAny <CandleTimeInterval>(),
                                         It.IsAny <IReadOnlyCollection <ICandle> >(),
                                         It.IsAny <SlotType>()),
                                     Times.Never);
        }