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.
This commit is contained in:
OlgunR
2026-04-21 13:42:31 +02:00
parent 612d8371d3
commit 4fa3bcae3d
3 changed files with 16 additions and 7 deletions

View File

@@ -6,14 +6,23 @@ namespace DbFirst.API.Dashboards;
public class DashboardChangeNotifier : IDashboardChangeNotifier public class DashboardChangeNotifier : IDashboardChangeNotifier
{ {
private readonly IHubContext<DashboardsHub> _hubContext; private readonly IHubContext<DashboardsHub> _hubContext;
private readonly ILogger<DashboardChangeNotifier> _logger;
public DashboardChangeNotifier(IHubContext<DashboardsHub> hubContext) public DashboardChangeNotifier(IHubContext<DashboardsHub> hubContext, ILogger<DashboardChangeNotifier> logger)
{ {
_hubContext = hubContext; _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.");
}
} }
} }

View File

@@ -2,5 +2,5 @@ namespace DbFirst.API.Dashboards;
public interface IDashboardChangeNotifier public interface IDashboardChangeNotifier
{ {
void NotifyChanged(); Task NotifyChangedAsync();
} }

View File

@@ -100,7 +100,7 @@ public sealed class SqlDashboardStorage : IEditableDashboardStorage
connection.Open(); connection.Open();
command.ExecuteNonQuery(); command.ExecuteNonQuery();
_notifier?.NotifyChanged(); _ = _notifier?.NotifyChangedAsync();
return id; return id;
} }
@@ -122,7 +122,7 @@ public sealed class SqlDashboardStorage : IEditableDashboardStorage
throw new ArgumentException($"Dashboard '{dashboardId}' not found."); throw new ArgumentException($"Dashboard '{dashboardId}' not found.");
} }
_notifier?.NotifyChanged(); _ = _notifier?.NotifyChangedAsync();
} }
public void DeleteDashboard(string dashboardId) public void DeleteDashboard(string dashboardId)
@@ -133,6 +133,6 @@ public sealed class SqlDashboardStorage : IEditableDashboardStorage
connection.Open(); connection.Open();
command.ExecuteNonQuery(); command.ExecuteNonQuery();
_notifier?.NotifyChanged(); _ = _notifier?.NotifyChangedAsync();
} }
} }