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,8 +28,11 @@ 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}");
try
{
TelemetryPublisher?.RegisterTelemeter(Sensor); TelemetryPublisher?.RegisterTelemeter(Sensor);
Sensor.Initialize(); Sensor.Initialize();
@ -63,5 +67,11 @@ namespace NucuCar.Sensors
TelemetryPublisher?.UnRegisterTelemeter(Sensor); TelemetryPublisher?.UnRegisterTelemeter(Sensor);
} }
catch (Exception e)
{
Logger?.LogError($"Unhandled exception in SensorWorker {sensorIdentifier}: {e.Message}");
Logger?.LogDebug(e.StackTrace);
}
}
} }
} }

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,6 +28,8 @@ namespace NucuCar.Telemetry
} }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{ {
if (!_serviceEnabled) if (!_serviceEnabled)
{ {
@ -48,5 +51,11 @@ namespace NucuCar.Telemetry
await Task.Delay(_interval, stoppingToken); await Task.Delay(_interval, stoppingToken);
} }
} }
catch (Exception e)
{
_logger?.LogError($"Unhandled exception in TelemetryWorker. {e.Message}");
_logger?.LogDebug(e.StackTrace);
}
}
} }
} }