From 4fa3bcae3d3ef6ad525518dbc6e0ec633c6365d4 Mon Sep 17 00:00:00 2001 From: OlgunR Date: Tue, 21 Apr 2026 13:42:31 +0200 Subject: [PATCH] Make dashboard change notifications async and robust Refactored DashboardChangeNotifier and IDashboardChangeNotifier to use async notification with improved error handling and logging. Updated SqlDashboardStorage to call the new async notification method after dashboard changes, ensuring non-blocking and reliable client updates. --- DbFirst.API/Dashboards/DashboardChangeNotifier.cs | 15 ++++++++++++--- .../Dashboards/IDashboardChangeNotifier.cs | 2 +- DbFirst.API/Dashboards/SqlDashboardStorage.cs | 6 +++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/DbFirst.API/Dashboards/DashboardChangeNotifier.cs b/DbFirst.API/Dashboards/DashboardChangeNotifier.cs index eb5f049..265ed96 100644 --- a/DbFirst.API/Dashboards/DashboardChangeNotifier.cs +++ b/DbFirst.API/Dashboards/DashboardChangeNotifier.cs @@ -6,14 +6,23 @@ namespace DbFirst.API.Dashboards; public class DashboardChangeNotifier : IDashboardChangeNotifier { private readonly IHubContext _hubContext; + private readonly ILogger _logger; - public DashboardChangeNotifier(IHubContext hubContext) + public DashboardChangeNotifier(IHubContext hubContext, ILogger logger) { _hubContext = hubContext; + _logger = logger; } - public void NotifyChanged() + public async Task NotifyChangedAsync() { - _ = _hubContext.Clients.All.SendAsync("DashboardsChanged"); + try + { + await _hubContext.Clients.All.SendAsync("DashboardsChanged"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to notify dashboard clients."); + } } } diff --git a/DbFirst.API/Dashboards/IDashboardChangeNotifier.cs b/DbFirst.API/Dashboards/IDashboardChangeNotifier.cs index 94ddee5..b1f6604 100644 --- a/DbFirst.API/Dashboards/IDashboardChangeNotifier.cs +++ b/DbFirst.API/Dashboards/IDashboardChangeNotifier.cs @@ -2,5 +2,5 @@ namespace DbFirst.API.Dashboards; public interface IDashboardChangeNotifier { - void NotifyChanged(); + Task NotifyChangedAsync(); } diff --git a/DbFirst.API/Dashboards/SqlDashboardStorage.cs b/DbFirst.API/Dashboards/SqlDashboardStorage.cs index 263cb60..3bfaafe 100644 --- a/DbFirst.API/Dashboards/SqlDashboardStorage.cs +++ b/DbFirst.API/Dashboards/SqlDashboardStorage.cs @@ -100,7 +100,7 @@ public sealed class SqlDashboardStorage : IEditableDashboardStorage connection.Open(); command.ExecuteNonQuery(); - _notifier?.NotifyChanged(); + _ = _notifier?.NotifyChangedAsync(); return id; } @@ -122,7 +122,7 @@ public sealed class SqlDashboardStorage : IEditableDashboardStorage throw new ArgumentException($"Dashboard '{dashboardId}' not found."); } - _notifier?.NotifyChanged(); + _ = _notifier?.NotifyChangedAsync(); } public void DeleteDashboard(string dashboardId) @@ -133,6 +133,6 @@ public sealed class SqlDashboardStorage : IEditableDashboardStorage connection.Open(); command.ExecuteNonQuery(); - _notifier?.NotifyChanged(); + _ = _notifier?.NotifyChangedAsync(); } }