1
0
voyager-api/ScrapperAPI/Bus/SignalRScrapeEventBus.cs

37 lines
1.2 KiB
C#

using Microsoft.AspNetCore.SignalR;
using ScrapperAPI.Enums;
using ScrapperAPI.Hub;
using ScrapperAPI.Interfaces;
using ScrapperAPI.Records;
namespace ScrapperAPI.Bus;
public sealed class SignalRScrapeEventBus : IScrapeEventBus
{
private readonly IHubContext<ScrapeHub> _hub;
public SignalRScrapeEventBus(IHubContext<ScrapeHub> hub) => _hub = hub;
public Task PublishAsync(ScrapeEvent ev, CancellationToken ct = default)
{
var tasks = new List<Task>(2);
// Detalhes só para a sessão
if (ev.Type is ScrapeEventType.ItemStarted or ScrapeEventType.ItemSucceeded or ScrapeEventType.ItemFailed)
{
tasks.Add(_hub.Clients.Group(ScrapeHub.GroupName(ev.SessionId))
.SendAsync("scrapeEvent", ev, ct));
return Task.WhenAll(tasks);
}
// Overview recebe eventos de "estado/progresso"
tasks.Add(_hub.Clients.Group(ScrapeHub.OverviewGroup)
.SendAsync("scrapeEvent", ev, ct));
// E a própria sessão também recebe (pra tela da sessão atualizar sem depender do overview)
tasks.Add(_hub.Clients.Group(ScrapeHub.GroupName(ev.SessionId))
.SendAsync("scrapeEvent", ev, ct));
return Task.WhenAll(tasks);
}
}