NUC-37: Add exception handling to the workers.
This commit is contained in:
parent
efe6c945b0
commit
5d16e93bda
3 changed files with 61 additions and 46 deletions
|
@ -81,8 +81,4 @@
|
|||
<ProjectReference Include="..\NucuCar.Domain\NucuCar.Domain.csproj" />
|
||||
<ProjectReference Include="..\NucuCar.Telemetry\NucuCar.Telemetry.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Telemetry" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue