/// <summary> /// Function for sending data to ingress restful end point /// </summary> /// <param name="rtt">Reference of RealTimeTelemetry instance</param> private void SendDataToIngress(RealTimeTelemetry rtt) { try { //random pause of 55ms so we dnt have spike on ingress Thread.Sleep(new Random().Next(1, 500)); //push data to ingress real time telemetry endpoint string jsonPayload = JsonConvert.SerializeObject(rtt); bool sendSuccess = _tti.SendRequest(jsonPayload).Result; if (!sendSuccess) { // unable to send real time telemetry to ingress - send by second channel Console.WriteLine("ERROR: Unable to send to real time telemetry ingress. Sending data on second channel."); string fileName = $"{_nodeId}-{DateTime.UtcNow:yyyy-MM-dd_HH-mm-ss}.json"; try { if (!_ftpMgr.TransferData(jsonPayload, fileName)) { Console.WriteLine("ERROR: Unable to send real time telemetry on second channel. Data File {0}", fileName); } } catch (Exception ex) { Console.WriteLine("ERROR: Unable to send real time telemetry on second channel. Error Details {0}", ex); } } else { Console.WriteLine("Real Time Telemetry Block data sent to Ingress Block # {0}", rtt.Payload.BlockNum); } } catch (Exception ex) { Console.WriteLine("ERROR Occurred While sending data to Ingress. {0}", ex); } }
/// <summary> /// Sends data to Ingress restful end point /// </summary> /// <param name="state">Object state</param> private static void FlushToIngress(object state) { // Flush to ingress if more than 10 telemetry recordings -or- last flush older that 1 minute if (_globalQueue.Count <= 10 && DateTime.UtcNow - _lastFlush <= new TimeSpan(0, 1, 0)) { Console.WriteLine($"Not flushing: {_globalQueue.Count} Queued - {(DateTime.UtcNow - _lastFlush).TotalSeconds} seconds since flush"); return; } List <string> telemetryToSend = new List <string>(); while (telemetryToSend.Count < 50 && _globalQueue.TryDequeue(out string lineFromQueue)) { telemetryToSend.Add(lineFromQueue); } Console.WriteLine($"Flushing {telemetryToSend.Count} to ingress. {_globalQueue.Count} still in queue." + (_flushHighspeed ? " [HighSpeed]" : "")); TelemetryPacket pkt = new TelemetryPacket { NodeId = _configuration.NodeId, Payload = telemetryToSend, Signature = _signer.SignPayload(string.Join(string.Empty, telemetryToSend)) }; string jsonPayload = JsonConvert.SerializeObject(pkt); // Send data TalkToIngress tti = new TalkToIngress(_configuration.IngressHost + "/api/ingress/influx", _configuration.IngressFingerprint); bool sendSuccess = tti.SendRequest(jsonPayload).Result; if (!sendSuccess) { if (DateTime.UtcNow - _lastFlush > TimeSpan.FromMinutes(5)) { // unable to send to ingress for 5 minutes - send by second channel Console.WriteLine("ERROR: Unable to send to ingress for more then 5 minutes. Sending queue on second channel."); string fileName = $"{_configuration.NodeId}-{DateTime.UtcNow:yyyy-MM-dd_HH:mm:ss}.json"; try { if (!_ftpMgr.TransferData(jsonPayload, fileName)) { Console.WriteLine("ERROR: Unable to send data on second channel. Data File {0}", fileName); // second channel also not available - requeue Console.WriteLine("Unable to flush to second channel - re queueing"); telemetryToSend.ForEach(_globalQueue.Enqueue); } } catch (Exception ex) { Console.WriteLine("ERROR: Unable to send data on second channel. Error Details {0}", ex); // second channel also not available - requeue Console.WriteLine("Unable to flush to second channel - re queueing"); telemetryToSend.ForEach(_globalQueue.Enqueue); } } else { // 5min second channel delay not reached -> re-queue Console.WriteLine("Unable to flush. Re-queuing..."); telemetryToSend.ForEach(_globalQueue.Enqueue); } } else { if (_globalQueue.Count > 250 && !_flushHighspeed) { // increase processing speed to 2 seconds Console.WriteLine("Increasing push speed due to queue size"); _flushTimer.Change(2000, 2000); _flushHighspeed = true; } else if (_globalQueue.Count < 250 && _flushHighspeed) // queue is small enough to get processed. back to normal speed { Console.WriteLine("Decreasing push speed due to queue size"); _flushTimer.Change(10000, 10000); _flushHighspeed = false; } _lastFlush = DateTime.UtcNow; } }