public void AddConnection(BinanceConnection connection) { _queue.QueueWS(connection); }
public void QueueWS(BinanceConnection wsItem) { _wsItems.Enqueue(wsItem); _signal.Release(); }
private async Task BackgroundProcessing(CancellationToken stoppingToken) { using (var scope = _scopeFactory.CreateScope()) { while (!stoppingToken.IsCancellationRequested) { BinanceConnection wsConnection = null; try { wsConnection = await _connectionsQueue.DequeueAsync(stoppingToken); var scraper = scope.ServiceProvider.GetRequiredService <BinanceWSScraper>(); scraper.Stats = scope.ServiceProvider.GetRequiredService <StatsService>().GetStatsHelper(wsConnection.Symbol); if (wsConnection.Store) { var storage = scope.ServiceProvider.GetRequiredService <IStorage>(); storage.Init(wsConnection.Symbol); scraper.Storage = storage; } WSScraperData data = new WSScraperData() { wsScraper = scraper }; data.wsScraper.Init(wsConnection); _logger.LogInformation("Connecting to {0}.", wsConnection.Symbol); data.wsScraperTask = Task.Run(async() => { await data.wsScraper.ConnectAsync(data.tokenSource.Token) .ContinueWith((antecedent, wsConnection) => { var binConnection = wsConnection as BinanceConnection; _wsConnections.TryRemove(binConnection.Symbol, out var data); foreach (var ex in antecedent.Exception.InnerExceptions) { if (ex is TaskCanceledException) { _logger.LogInformation("WebSocket connetion to {0} was closed.", binConnection.Symbol); } else { _logger.LogError("WebSocket connetion to {0} has failed. Exception: {1}.", binConnection.Symbol, ex); } } }, wsConnection, TaskContinuationOptions.OnlyOnFaulted); }); _wsConnections.TryAdd(wsConnection.Symbol, data); _logger.LogInformation("Active connections count = {0}", _wsConnections.Count); } catch (OperationCanceledException) { } catch (Exception ex) { if (wsConnection != null) { _logger.LogError(ex, "Error occured connecting: {Address}", wsConnection.Address); } else { _logger.LogError(ex, "Websocket connection dequeuing error."); } } } _logger.LogInformation("BackgroundProcessing is finished."); ShutDown(); } }
public void Init(BinanceConnection connectionDetails) { this.connectionDetails = connectionDetails; }