From 61d819ea8a7334219590c5af0193afcf5b2f94fe Mon Sep 17 00:00:00 2001 From: sleevezipper Date: Sat, 9 Jan 2021 23:21:36 +0100 Subject: [PATCH] make sure the worker doesn't crash when a sensor fails --- hass-workstation-service/Worker.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/hass-workstation-service/Worker.cs b/hass-workstation-service/Worker.cs index 4b13192..540225b 100644 --- a/hass-workstation-service/Worker.cs +++ b/hass-workstation-service/Worker.cs @@ -9,6 +9,7 @@ using hass_workstation_service.Domain.Sensors; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using MQTTnet.Client; +using Serilog; namespace hass_workstation_service { @@ -51,7 +52,15 @@ namespace hass_workstation_service foreach (AbstractSensor sensor in sensors) { - await sensor.PublishStateAsync(); + try + { + await sensor.PublishStateAsync(); + } + catch (Exception ex) + { + Log.Logger.Warning("Sensor failed: " + sensor.Name, ex); + } + } // announce autodiscovery every 30 seconds if (_mqttPublisher.LastConfigAnnounce < DateTime.UtcNow.AddSeconds(-30))