From 5d16e93bda625bf5096807cc7e880683a2b6127c Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Mon, 20 Apr 2020 13:30:22 +0300 Subject: [PATCH] NUC-37: Add exception handling to the workers. --- NucuCar.Sensors/NucuCar.Sensors.csproj | 4 -- NucuCar.Sensors/SensorWorker.cs | 66 +++++++++++++++----------- NucuCar.Telemetry/TelemetryWorker.cs | 37 +++++++++------ 3 files changed, 61 insertions(+), 46 deletions(-) diff --git a/NucuCar.Sensors/NucuCar.Sensors.csproj b/NucuCar.Sensors/NucuCar.Sensors.csproj index c257cd8..b9c5b5c 100644 --- a/NucuCar.Sensors/NucuCar.Sensors.csproj +++ b/NucuCar.Sensors/NucuCar.Sensors.csproj @@ -81,8 +81,4 @@ - - - - diff --git a/NucuCar.Sensors/SensorWorker.cs b/NucuCar.Sensors/SensorWorker.cs index 2bca3c4..c89f0e9 100644 --- a/NucuCar.Sensors/SensorWorker.cs +++ b/NucuCar.Sensors/SensorWorker.cs @@ -1,3 +1,4 @@ +using System; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; @@ -27,41 +28,50 @@ namespace NucuCar.Sensors { return; } + var sensorIdentifier = Sensor.GetIdentifier(); Logger?.LogInformation($"Starting sensor worker for {sensorIdentifier}"); - TelemetryPublisher?.RegisterTelemeter(Sensor); - - Sensor.Initialize(); - while (!stoppingToken.IsCancellationRequested) + try { - var sensorState = Sensor.GetState(); - /* If sensor is ok attempt to read. */ - if (sensorState == SensorStateEnum.Initialized) + TelemetryPublisher?.RegisterTelemeter(Sensor); + + Sensor.Initialize(); + while (!stoppingToken.IsCancellationRequested) { - Logger?.LogTrace($"{sensorIdentifier} is taking a measurement!"); - await Sensor.TakeMeasurementAsync(); - } - /* Else attempt to re-initialize. */ - else if (sensorState == SensorStateEnum.Uninitialized || - sensorState == SensorStateEnum.Error) - { - Logger?.LogWarning( - $"{sensorIdentifier} is in {sensorState}! Attempting to re-initialize in {_intializationDelay}ms."); - _intializationDelay += 10000; - await Task.Delay(_intializationDelay, stoppingToken); - Sensor.Initialize(); - } - else if (sensorState == SensorStateEnum.Disabled) - { - // Break from while. - Logger?.LogInformation($"{sensorIdentifier} has been disabled!"); - break; + var sensorState = Sensor.GetState(); + /* If sensor is ok attempt to read. */ + if (sensorState == SensorStateEnum.Initialized) + { + Logger?.LogTrace($"{sensorIdentifier} is taking a measurement!"); + await Sensor.TakeMeasurementAsync(); + } + /* Else attempt to re-initialize. */ + else if (sensorState == SensorStateEnum.Uninitialized || + sensorState == SensorStateEnum.Error) + { + Logger?.LogWarning( + $"{sensorIdentifier} is in {sensorState}! Attempting to re-initialize in {_intializationDelay}ms."); + _intializationDelay += 10000; + await Task.Delay(_intializationDelay, stoppingToken); + Sensor.Initialize(); + } + else if (sensorState == SensorStateEnum.Disabled) + { + // Break from while. + Logger?.LogInformation($"{sensorIdentifier} has been disabled!"); + break; + } + + await Task.Delay(MeasurementInterval, stoppingToken); } - await Task.Delay(MeasurementInterval, stoppingToken); + TelemetryPublisher?.UnRegisterTelemeter(Sensor); + } + catch (Exception e) + { + Logger?.LogError($"Unhandled exception in SensorWorker {sensorIdentifier}: {e.Message}"); + Logger?.LogDebug(e.StackTrace); } - - TelemetryPublisher?.UnRegisterTelemeter(Sensor); } } } \ No newline at end of file diff --git a/NucuCar.Telemetry/TelemetryWorker.cs b/NucuCar.Telemetry/TelemetryWorker.cs index 642d7c9..9676b59 100644 --- a/NucuCar.Telemetry/TelemetryWorker.cs +++ b/NucuCar.Telemetry/TelemetryWorker.cs @@ -1,3 +1,4 @@ +using System; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; @@ -28,24 +29,32 @@ namespace NucuCar.Telemetry protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - if (!_serviceEnabled) + try { - _logger.LogInformation("Telemetry service is disabled!"); - return; - } + if (!_serviceEnabled) + { + _logger.LogInformation("Telemetry service is disabled!"); + return; + } - if (_telemetryPublisher == null) - { - _logger?.LogCritical("Invalid state! TelemetryPublisher is null!"); - return; - } + if (_telemetryPublisher == null) + { + _logger?.LogCritical("Invalid state! TelemetryPublisher is null!"); + return; + } - await Task.Delay(_interval, stoppingToken); - while (!stoppingToken.IsCancellationRequested) - { - await _telemetryPublisher.PublishAsync(stoppingToken); - _logger?.LogDebug("Telemetry data published!"); await Task.Delay(_interval, stoppingToken); + while (!stoppingToken.IsCancellationRequested) + { + await _telemetryPublisher.PublishAsync(stoppingToken); + _logger?.LogDebug("Telemetry data published!"); + await Task.Delay(_interval, stoppingToken); + } + } + catch (Exception e) + { + _logger?.LogError($"Unhandled exception in TelemetryWorker. {e.Message}"); + _logger?.LogDebug(e.StackTrace); } } }