using AutomationBLL.Run; using Microsoft.AspNetCore.SignalR; namespace AutomationSL.Hubs; public class AutomationRunNotifier : IAutomationRunNotifier { private readonly IHubContext _hubContext; private readonly ILogger _logger; public AutomationRunNotifier(IHubContext hubContext, ILogger logger) { _hubContext = hubContext; _logger = logger; } public async Task PushLogAppended(long runId, string logLevel, string message, CancellationToken ct = default) { try { await _hubContext.Clients.Group(AutomationRunHub.RunGroup(runId)) .ReceiveLogAppended(runId, logLevel, message); } catch (Exception ex) { _logger.LogWarning(ex, "AutomationRunNotifier: failed to push log for RunId {RunId}", runId); } } public async Task PushRunStarted(int tenantId, long runId, string triggeredBy, CancellationToken ct = default) { try { await _hubContext.Clients.Group(AutomationRunHub.RunGroup(runId)).ReceiveRunStarted(runId, triggeredBy); await _hubContext.Clients.Group(AutomationRunHub.TenantGroup(tenantId)).ReceiveRunStarted(runId, triggeredBy); } catch (Exception ex) { _logger.LogWarning(ex, "AutomationRunNotifier: failed to push run-started for RunId {RunId}", runId); } } public async Task PushRunCompleted(int tenantId, long runId, string status, string? summary, CancellationToken ct = default) { try { await _hubContext.Clients.Group(AutomationRunHub.RunGroup(runId)).ReceiveRunCompleted(runId, status, summary); await _hubContext.Clients.Group(AutomationRunHub.TenantGroup(tenantId)).ReceiveRunCompleted(runId, status, summary); } catch (Exception ex) { _logger.LogWarning(ex, "AutomationRunNotifier: failed to push run-completed for RunId {RunId}", runId); } } public async Task PushRunFailed(int tenantId, long runId, string? errorMessage, CancellationToken ct = default) { try { await _hubContext.Clients.Group(AutomationRunHub.RunGroup(runId)).ReceiveRunFailed(runId, errorMessage); await _hubContext.Clients.Group(AutomationRunHub.TenantGroup(tenantId)).ReceiveRunFailed(runId, errorMessage); } catch (Exception ex) { _logger.LogWarning(ex, "AutomationRunNotifier: failed to push run-failed for RunId {RunId}", runId); } } }