NUC-3: Fix unhandled TaskCanceledException in SensorWorker.cs and TelemetryWorker.cs

This commit is contained in:
Denis-Cosmin Nutiu 2020-10-25 15:31:54 +02:00
parent fb689b2440
commit 2658e49687
3 changed files with 12 additions and 2 deletions

View file

@ -51,6 +51,10 @@ namespace NucuCar.Telemetry
await Task.Delay(_interval, stoppingToken);
}
}
catch (TaskCanceledException)
{
_logger?.LogInformation("The TelemetryWorker task was canceled.");
}
catch (Exception e)
{
_logger?.LogError($"Unhandled exception in TelemetryWorker. {e.Message}");

View file

@ -31,9 +31,11 @@ namespace NucuCar.Sensors
services.AddSingleton<ISensor<Bme680Sensor>, Bme680Sensor>();
services.AddSingleton<ISensor<CpuTempSensor>, CpuTempSensor>();
services.AddSingleton<ISensor<HeartbeatSensor>, HeartbeatSensor>();
// Workers
// Telemetry
services.AddHostedService<TelemetryWorker>();
// Sensors
services.AddHostedService<Bme680Worker>();
services.AddHostedService<CpuTempWorker>();
services.AddHostedService<HeartbeatWorker>();

View file

@ -67,9 +67,13 @@ namespace NucuCar.Sensors
TelemetryPublisher?.UnRegisterTelemeter(Sensor);
}
catch (TaskCanceledException)
{
Logger?.LogInformation("The SensorWorker task was canceled.");
}
catch (Exception e)
{
Logger?.LogError($"Unhandled exception in SensorWorker {sensorIdentifier}: {e.Message}");
Logger?.LogError($"Unhandled exception in SensorWorker {sensorIdentifier}. {e.GetType()}: {e.Message}");
Logger?.LogDebug(e.StackTrace);
}
}