Exemplo n.º 1
0
        private async Task OnChannelActivated(CancellationToken cancellationToken)
        {
            try
            {
                //Step 1 - read nodes and stats from db
                AddPersistedNodes(cancellationToken);

                //Step 2 - initialize bootnodes
                if (_logger.IsDebug)
                {
                    _logger.Debug("Initializing bootnodes.");
                }
                while (true)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }

                    if (await InitializeBootnodes(cancellationToken))
                    {
                        break;
                    }

                    //Check if we were able to communicate with any trusted nodes or persisted nodes
                    //if so no need to replay bootstrapping, we can start discovery process
                    if (_discoveryManager.GetOrAddNodeLifecycleManagers(x => x.State == NodeLifecycleState.Active).Any())
                    {
                        break;
                    }

                    _logger.Warn("Could not communicate with any nodes (bootnodes, trusted nodes, persisted nodes).");
                    await Task.Delay(1000, cancellationToken);
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                InitializeDiscoveryPersistenceTimer();
                InitializeDiscoveryTimer();

                await RunDiscoveryAsync(cancellationToken).ContinueWith(
                    t =>
                {
                    if (t.IsFaulted)
                    {
                        _logger.Error("Discovery error", t.Exception);
                    }
                });
            }
            catch (Exception e)
            {
                if (_logger.IsDebug)
                {
                    _logger.Error("DEBUG/ERROR Error during discovery initialization", e);
                }
            }
        }