public async Task <ExchangeHistory> RunAsync(ExchangeHistory exchangeHistory) { if (IsRunning()) { throw new InvalidOperationException(); } Stopwatch.Restart(); DocumentSession.Advanced.MaxNumberOfRequestsPerSession = int.MaxValue; if (!string.IsNullOrEmpty(exchangeHistory.Id)) { exchangeHistory = DocumentSession.Load <ExchangeHistory>(exchangeHistory.Id); } exchangeHistory.CompletedOnDateTimeUtc = null; if (PoeNinjaDataService == null) { PoeNinjaDataService = new PoeNinjaDataService(); } FetchCurrencyQueue = new ConcurrentQueue <PoeNinjaDataService.CurrencyType>(); FetchItemQueue = new ConcurrentQueue <PoeNinjaDataService.ItemType>(); FetchCurrencyHistoryQueue = new ConcurrentQueue <IdCurrencyTypePair>(); FetchItemHistoryQueue = new ConcurrentQueue <IdItemTypePair>(); FetchCurrencyQueue.Enqueue(PoeNinjaDataService.CurrencyType.Currency); foreach (var itemType in (PoeNinjaDataService.ItemType[])Enum.GetValues(typeof(PoeNinjaDataService.ItemType))) { FetchItemQueue.Enqueue(itemType); } var tasks = new Task [NumberOfThreads]; for (var i = 0; i < NumberOfThreads; i++) { tasks[i] = FillAndEmptyFetchQueuesAsync(exchangeHistory); } await Task.WhenAll(tasks); exchangeHistory.CompletedOnDateTimeUtc = DateTime.UtcNow; if (!string.IsNullOrEmpty(exchangeHistory.Id)) { DocumentSession.SaveChanges(); } return(exchangeHistory); }
public TradeRateService() { RemoteDataService = new PoeNinjaDataService(); RateDataTimeoutInTicks = new TimeSpan(1, 0, 0).Ticks; }
private async Task FillAndEmptyFetchQueuesAsync(ExchangeHistory exchangeHistory) { PoeNinjaDataService.CurrencyType currencyType; PoeNinjaDataService.ItemType itemType; IdCurrencyTypePair idCurrencyTypePair; IdItemTypePair idItemTypePair; while (FetchCurrencyQueue.TryDequeue(out currencyType)) { Debug.WriteLine("Thread: " + Thread.CurrentThread.ManagedThreadId + " fetched currency type: " + currencyType); var currencyJsonResponse = await PoeNinjaDataService.FetchCurrencyJsonResponseAsync(currencyType); foreach (var details in currencyJsonResponse.CurrencyDetails) { FetchCurrencyHistoryQueue.Enqueue(new IdCurrencyTypePair { Id = details.Id, Name = details.Name, CurrencyType = currencyType }); } Thread.Sleep(10); } while (FetchItemQueue.TryDequeue(out itemType)) { Debug.WriteLine("Thread: " + Thread.CurrentThread.ManagedThreadId + " fetched item type: " + itemType); var itemJsonResponse = await PoeNinjaDataService.FetchItemJsonResponseAsync(itemType); foreach (var line in itemJsonResponse.Lines) { FetchItemHistoryQueue.Enqueue(new IdItemTypePair { Id = line.Id, Name = line.Name, ItemType = itemType }); } Thread.Sleep(10); } while (FetchCurrencyHistoryQueue.TryDequeue(out idCurrencyTypePair)) { Debug.WriteLine("Thread: " + Thread.CurrentThread.ManagedThreadId + " fetched currency id: " + idCurrencyTypePair.Id); var currencyHistoryJsonResponse = await PoeNinjaDataService.FetchCurrencyHistoryJsonResponseAsync(idCurrencyTypePair.CurrencyType, idCurrencyTypePair.Id); // can be null for certain currency items like Chaos Orb if (currencyHistoryJsonResponse != null) { var item = new ExchangeHistory.ItemData { Type = idCurrencyTypePair.CurrencyType.Name(), Name = idCurrencyTypePair.Name, PoeNinjaId = idCurrencyTypePair.Id, }; item.ChaosValuePerDay.AddRange(currencyHistoryJsonResponse.ReceiveCurrencyGraphData.Select(o => o.Value)); AddAndSaveExchangeHistory(exchangeHistory, item); } Thread.Sleep(10); } while (FetchItemHistoryQueue.TryDequeue(out idItemTypePair)) { Debug.WriteLine("Thread: " + Thread.CurrentThread.ManagedThreadId + " fetched item id: " + idItemTypePair.Id); var itemHistoryJsonResponse = await PoeNinjaDataService.FetchItemHistoryJsonResponseAsync(idItemTypePair.ItemType, idItemTypePair.Id); // can be null for certain currency items like Chaos Orb if (itemHistoryJsonResponse != null) { var item = new ExchangeHistory.ItemData { Type = idItemTypePair.ItemType.Name(), Name = idItemTypePair.Name, PoeNinjaId = idItemTypePair.Id, }; item.ChaosValuePerDay.AddRange(itemHistoryJsonResponse.Select(o => o.Value)); AddAndSaveExchangeHistory(exchangeHistory, item); } Thread.Sleep(10); } }