/// <summary> /// Loop every so often until a condition is met, then execute the action and finish. /// </summary> public static Task RunUntil(string name, CancellationToken nodeCancellationToken, Func <bool> condition, Action action, Action <Exception> onException, TimeSpan repeatEvery) { var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(nodeCancellationToken); return(AsyncLoop.Run(name, token => { try { // loop until the condition is met, then execute the action and finish. if (condition()) { action(); linkedTokenSource.Cancel(); } } catch (Exception e) { onException(e); linkedTokenSource.Cancel(); } return Task.CompletedTask; }, linkedTokenSource.Token, repeatEvery: repeatEvery)); }
private void StartPeriodicLog() { AsyncLoop.Run("PeriodicLog", (cancellation) => { // TODO: move stats to each of its components StringBuilder benchLogs = new StringBuilder(); benchLogs.AppendLine("======Consensus====== " + DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)); benchLogs.AppendLine("Headers.Height: ".PadRight(Logs.ColumnLength + 3) + this.Chain.Tip.Height.ToString().PadRight(8) + " Headers.Hash: ".PadRight(Logs.ColumnLength + 3) + this.Chain.Tip.HashBlock); benchLogs.AppendLine("Consensus.Height: ".PadRight(Logs.ColumnLength + 3) + this._ChainBehaviorState.HighestValidatedPoW.Height.ToString().PadRight(8) + " Consensus.Hash: ".PadRight(Logs.ColumnLength + 3) + this._ChainBehaviorState.HighestValidatedPoW.HashBlock); benchLogs.AppendLine("Store.Height: ".PadRight(Logs.ColumnLength + 3) + this._ChainBehaviorState.HighestPersistedBlock.Height.ToString().PadRight(8) + " Store.Hash: ".PadRight(Logs.ColumnLength + 3) + this._ChainBehaviorState.HighestPersistedBlock.HashBlock); benchLogs.AppendLine(); benchLogs.AppendLine("======Mempool======"); benchLogs.AppendLine(this.MempoolManager.PerformanceCounter.ToString()); benchLogs.AppendLine("======Connection======"); benchLogs.AppendLine(this.ConnectionManager.GetNodeStats()); Logs.Bench.LogInformation(benchLogs.ToString()); return(Task.CompletedTask); }, this.GlobalCancellation.Cancellation.Token, repeatEvery: TimeSpans.FiveSeconds, startAfter: TimeSpans.FiveSeconds); }
private void StartPeriodicLog() { AsyncLoop.Run("PeriodicLog", (cancellation) => { // TODO: move stats to each of its components StringBuilder benchLogs = new StringBuilder(); benchLogs.AppendLine("======Node stats====== " + DateTime.UtcNow.ToString(CultureInfo.InvariantCulture) + " agent " + this.ConnectionManager.Parameters.UserAgent); benchLogs.AppendLine("Headers.Height: ".PadRight(Logs.ColumnLength + 3) + this.Chain.Tip.Height.ToString().PadRight(8) + " Headers.Hash: ".PadRight(Logs.ColumnLength + 3) + this.Chain.Tip.HashBlock); if (this.ConsensusLoop != null) { benchLogs.AppendLine("Consensus.Height: ".PadRight(Logs.ColumnLength + 3) + this.ChainBehaviorState.HighestValidatedPoW.Height.ToString().PadRight(8) + " Consensus.Hash: ".PadRight(Logs.ColumnLength + 3) + this.ChainBehaviorState.HighestValidatedPoW.HashBlock); } if (this.ChainBehaviorState.HighestPersistedBlock != null) { benchLogs.AppendLine("Store.Height: ".PadRight(Logs.ColumnLength + 3) + this.ChainBehaviorState.HighestPersistedBlock.Height.ToString().PadRight(8) + " Store.Hash: ".PadRight(Logs.ColumnLength + 3) + this.ChainBehaviorState.HighestPersistedBlock.HashBlock); } if (this.WalletManager != null) { var height = this.WalletManager.LastBlockHeight(); var block = this.Chain.GetBlock(height); var hashBlock = block == null ? uint256.Zero : block.HashBlock; benchLogs.AppendLine("Wallet.Height: ".PadRight(Logs.ColumnLength + 3) + height.ToString().PadRight(8) + " Wallet.Hash: ".PadRight(Logs.ColumnLength + 3) + hashBlock); } benchLogs.AppendLine(); if (this.MempoolManager != null) { benchLogs.AppendLine("======Mempool======"); benchLogs.AppendLine(this.MempoolManager.PerformanceCounter.ToString()); } benchLogs.AppendLine("======Connection======"); benchLogs.AppendLine(this.ConnectionManager.GetNodeStats()); Logs.Bench.LogInformation(benchLogs.ToString()); return(Task.CompletedTask); }, this.nodeLifetime.ApplicationStopping, repeatEvery: TimeSpans.FiveSeconds, startAfter: TimeSpans.FiveSeconds); }