Пример #1
0
        private async Task OnChannelActivated()
        {
            try
            {
                //Step 1 - read nodes and stats from db
                AddPersistedNodes();

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

                    _logger.Warn("Could not communicate with any bootnodes.");

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

                    _logger.Warn("Could not communicate with any nodes.");
                    await Task.Delay(1000);
                }
                InitializeDiscoveryPersistanceTimer();
                InitializeDiscoveryTimer();

                await RunDiscoveryAsync();
            }
            catch (Exception e)
            {
                _logger.Error("Error during discovery initialization", e);
            }
        }
Пример #2
0
        private void RunDiscoveryCommit()
        {
            try
            {
                IReadOnlyCollection <INodeLifecycleManager> managers = _discoveryManager.GetNodeLifecycleManagers();
                //we need to update all notes to update reputation
                _discoveryStorage.UpdateNodes(managers.Select(x => new NetworkNode(x.ManagedNode.Id, x.ManagedNode.Host, x.ManagedNode.Port, x.NodeStats.NewPersistedNodeReputation)).ToArray());

                if (!_discoveryStorage.AnyPendingChange())
                {
                    if (_logger.IsTrace)
                    {
                        _logger.Trace("No changes in discovery storage, skipping commit.");
                    }
                    return;
                }

                _storageCommitTask = Task.Run(() =>
                {
                    _discoveryStorage.Commit();
                    _discoveryStorage.StartBatch();
                });

                Task task = _storageCommitTask.ContinueWith(x =>
                {
                    if (x.IsFaulted && _logger.IsError)
                    {
                        _logger.Error($"Error during discovery commit: {x.Exception}");
                    }
                });
                task.Wait();
                _storageCommitTask = null;
            }
            catch (Exception ex)
            {
                _logger.Error($"Error during discovery commit: {ex}");
            }
        }