private void AddOrUpdateNodeException(NeoMonitorContext scopedCtx, Node dbNode, long latency) { int nodeId = dbNode.Id; var nodeEx = scopedCtx.NodeExceptionList.AsNoTracking().FirstOrDefault(e => e.Url == dbNode.Url && e.ExceptionHeight == dbNode.Height); if (nodeEx is null) { int interval = (int)Math.Round((DateTime.Now - dbNode.LastUpdateTime).TotalSeconds, 0); string url = dbNode.Url; int height = dbNode.Height.Value; _contextActions.Add((ctx) => { ctx.NodeExceptionList.Add(new NodeException { Url = url, ExceptionHeight = height, GenTime = DateTime.Now, Intervals = interval }); }); AddOrUpdateAction(_nodeActionDict, nodeId, n => { n.Latency = latency; n.ExceptionCount++; }); } else { AddOrUpdateAction(_nodeActionDict, nodeId, n => { n.Latency = latency; }); int nodeExId = nodeEx.Id; int intervals = (int)Math.Round((DateTime.Now - nodeEx.GenTime).TotalSeconds, 0); AddOrUpdateAction(_nodeExceptionActionDict, nodeExId, n => n.Intervals = intervals); } }
private async Task GetNodeHeightAsync(NeoMonitorContext scopedCtx, Node dbNode) { var sw = Stopwatch.StartNew(); int?height = await _rpcService.GetBlockCountAsync(dbNode.Url); sw.Stop(); long latency = sw.ElapsedMilliseconds; int nodeId = dbNode.Id; if (height.HasValue) { if (!dbNode.Height.HasValue || height > dbNode.Height) { AddOrUpdateAction(_nodeActionDict, nodeId, n => { n.Latency = latency; n.Height = height; }); } else if (height == dbNode.Height) { AddOrUpdateNodeException(scopedCtx, dbNode, latency); } } else { AddOrUpdateAction(_nodeActionDict, nodeId, n => { n.Latency = -1; n.LastUpdateTime = DateTime.Now; }); } }
private void ExecuteActionsAsync(NeoMonitorContext ctx) { foreach (var item in _nodeActionDict) { var node = ctx.Nodes.FirstOrDefault(p => p.Id == item.Key); if (node != null) { item.Value.Invoke(node); } } foreach (var item in _nodeExceptionActionDict) { var node = ctx.NodeExceptionList.FirstOrDefault(p => p.Id == item.Key); if (node != null) { item.Value.Invoke(node); } } foreach (var item in _contextActions) { item.Invoke(ctx); } }