NUC-37: Add exception handling to the workers.

This commit is contained in:
Denis-Cosmin Nutiu 2020-04-20 13:30:22 +03:00
parent efe6c945b0
commit 5d16e93bda
3 changed files with 61 additions and 46 deletions

View file

@ -81,8 +81,4 @@
<ProjectReference Include="..\NucuCar.Domain\NucuCar.Domain.csproj" /> <ProjectReference Include="..\NucuCar.Domain\NucuCar.Domain.csproj" />
<ProjectReference Include="..\NucuCar.Telemetry\NucuCar.Telemetry.csproj" /> <ProjectReference Include="..\NucuCar.Telemetry\NucuCar.Telemetry.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Telemetry" />
</ItemGroup>
</Project> </Project>

View file

@ -1,3 +1,4 @@
using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@ -27,41 +28,50 @@ namespace NucuCar.Sensors
{ {
return; return;
} }
var sensorIdentifier = Sensor.GetIdentifier(); var sensorIdentifier = Sensor.GetIdentifier();
Logger?.LogInformation($"Starting sensor worker for {sensorIdentifier}"); Logger?.LogInformation($"Starting sensor worker for {sensorIdentifier}");
TelemetryPublisher?.RegisterTelemeter(Sensor); try
Sensor.Initialize();
while (!stoppingToken.IsCancellationRequested)
{ {
var sensorState = Sensor.GetState(); TelemetryPublisher?.RegisterTelemeter(Sensor);
/* If sensor is ok attempt to read. */
if (sensorState == SensorStateEnum.Initialized) Sensor.Initialize();
while (!stoppingToken.IsCancellationRequested)
{ {
Logger?.LogTrace($"{sensorIdentifier} is taking a measurement!"); var sensorState = Sensor.GetState();
await Sensor.TakeMeasurementAsync(); /* If sensor is ok attempt to read. */
} if (sensorState == SensorStateEnum.Initialized)
/* Else attempt to re-initialize. */ {
else if (sensorState == SensorStateEnum.Uninitialized || Logger?.LogTrace($"{sensorIdentifier} is taking a measurement!");
sensorState == SensorStateEnum.Error) await Sensor.TakeMeasurementAsync();
{ }
Logger?.LogWarning( /* Else attempt to re-initialize. */
$"{sensorIdentifier} is in {sensorState}! Attempting to re-initialize in {_intializationDelay}ms."); else if (sensorState == SensorStateEnum.Uninitialized ||
_intializationDelay += 10000; sensorState == SensorStateEnum.Error)
await Task.Delay(_intializationDelay, stoppingToken); {
Sensor.Initialize(); Logger?.LogWarning(
} $"{sensorIdentifier} is in {sensorState}! Attempting to re-initialize in {_intializationDelay}ms.");
else if (sensorState == SensorStateEnum.Disabled) _intializationDelay += 10000;
{ await Task.Delay(_intializationDelay, stoppingToken);
// Break from while. Sensor.Initialize();
Logger?.LogInformation($"{sensorIdentifier} has been disabled!"); }
break; 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);
} }
} }
} }

View file

@ -1,3 +1,4 @@
using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@ -28,24 +29,32 @@ namespace NucuCar.Telemetry
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
if (!_serviceEnabled) try
{ {
_logger.LogInformation("Telemetry service is disabled!"); if (!_serviceEnabled)
return; {
} _logger.LogInformation("Telemetry service is disabled!");
return;
}
if (_telemetryPublisher == null) if (_telemetryPublisher == null)
{ {
_logger?.LogCritical("Invalid state! TelemetryPublisher is null!"); _logger?.LogCritical("Invalid state! TelemetryPublisher is null!");
return; return;
} }
await Task.Delay(_interval, stoppingToken);
while (!stoppingToken.IsCancellationRequested)
{
await _telemetryPublisher.PublishAsync(stoppingToken);
_logger?.LogDebug("Telemetry data published!");
await Task.Delay(_interval, stoppingToken); 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);
} }
} }
} }