|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using Microsoft.Extensions.Options; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Text; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using TradeHub.MissedBlocksAlerter.Services; |
| 10 | +using TradeHub.MissedBlocksAlerter.Settings; |
| 11 | + |
| 12 | +namespace TradeHub.MissedBlocksAlerter.Services |
| 13 | +{ |
| 14 | + public class AppWorkerService : BackgroundService, IDisposable |
| 15 | + { |
| 16 | + private readonly ILogger<AppWorkerService> _logger; |
| 17 | + private readonly IOptionsSnapshot<AppSettings> _appSettings; |
| 18 | + |
| 19 | + private readonly PushbulletService _pushbulletService; |
| 20 | + private readonly TradescanService _tradescanService; |
| 21 | + |
| 22 | + private Timer _timer; |
| 23 | + |
| 24 | + private DateTime? _lastError = null; |
| 25 | + private long _missedBlocks = 0; |
| 26 | + |
| 27 | + public AppWorkerService(ILogger<AppWorkerService> logger, IOptionsSnapshot<AppSettings> appSettings, PushbulletService pushbulletService, TradescanService tradescanService) |
| 28 | + { |
| 29 | + _logger = logger; |
| 30 | + _appSettings = appSettings; |
| 31 | + _pushbulletService = pushbulletService; |
| 32 | + _tradescanService = tradescanService; |
| 33 | + } |
| 34 | + |
| 35 | + protected override async Task ExecuteAsync(CancellationToken cancellationToken) |
| 36 | + { |
| 37 | + var deviceName = _appSettings.Value.Pushbullet.Target; |
| 38 | + var deviceIden = await _pushbulletService.GetDeviceIdenByDeviceName(deviceName); |
| 39 | + if (!string.IsNullOrEmpty(deviceIden)) |
| 40 | + { |
| 41 | + _logger.LogInformation($"Target: {deviceName}, Identifier: {deviceIden}"); |
| 42 | + |
| 43 | + await _pushbulletService.CreatePush(deviceIden, "Started successfully"); |
| 44 | + |
| 45 | + _timer = new Timer((state) => DoWorkAsync(deviceIden), null, TimeSpan.Zero, TimeSpan.FromSeconds(_appSettings.Value.Supervision.SecondsBetween)); |
| 46 | + while (!cancellationToken.IsCancellationRequested) { } |
| 47 | + |
| 48 | + await _pushbulletService.CreatePush(deviceIden, "Stopping"); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + _logger.LogError("An error occured while retrieving your target device identifier from Pushbullet API"); |
| 53 | + } |
| 54 | + |
| 55 | + await Task.CompletedTask; |
| 56 | + } |
| 57 | + |
| 58 | + public async void DoWorkAsync(string deviceIden) |
| 59 | + { |
| 60 | + var blockHeightAndMissedBlocks = await _tradescanService.GetBlockHeightAndMissedBlocksByConsensusAddr(_appSettings.Value.Validator.ConsensusAddr); |
| 61 | + |
| 62 | + if (!blockHeightAndMissedBlocks.Key.HasValue || !blockHeightAndMissedBlocks.Value.HasValue) |
| 63 | + { |
| 64 | + if (_lastError.HasValue && (DateTime.Now - _lastError.Value).TotalMinutes < _appSettings.Value.ErrorReports.MinutesBetween) |
| 65 | + return; |
| 66 | + |
| 67 | + if (_appSettings.Value.ErrorReports.Enabled) |
| 68 | + { |
| 69 | + var errorMessage = "An error occurred while retrieving signing informations from Tradescan API"; |
| 70 | + _logger.LogError(errorMessage); |
| 71 | + await _pushbulletService.CreatePush(deviceIden, errorMessage); |
| 72 | + |
| 73 | + _lastError = DateTime.Now; |
| 74 | + } |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + var blockHeight = blockHeightAndMissedBlocks.Key.Value; |
| 79 | + var missedBlocks = blockHeightAndMissedBlocks.Value.Value; |
| 80 | + |
| 81 | + _logger.LogDebug($"Height: {blockHeight}, MissedBlocks: {missedBlocks}"); |
| 82 | + |
| 83 | + if (missedBlocks >= _appSettings.Value.Thresholds.MaxBlocksMissed && _missedBlocks != missedBlocks) |
| 84 | + { |
| 85 | + var warningMessage = $"Your validator missed {missedBlocks} block(s)"; |
| 86 | + _logger.LogWarning(warningMessage); |
| 87 | + await _pushbulletService.CreatePush(deviceIden, warningMessage); |
| 88 | + |
| 89 | + _missedBlocks = missedBlocks; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public new void Dispose() |
| 95 | + { |
| 96 | + _timer?.Dispose(); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments