Files
mic-check/src/api/MicCheck.Api/Webhooks/WebhookBackgroundService.cs
James Wampler 6887d09f9c version-0.1 (#1)
Squash and merge of version-0.1

Co-authored-by: James Wampler <james@wamp.dev>
Co-committed-by: James Wampler <james@wamp.dev>
2026-07-01 13:30:07 -07:00

28 lines
957 B
C#
Executable File

using MicCheck.Api.Data;
using Microsoft.Extensions.DependencyInjection;
namespace MicCheck.Api.Webhooks;
public class WebhookBackgroundService(
WebhookQueue queue,
IServiceScopeFactory scopeFactory,
ILogger<WebhookBackgroundService> logger) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await foreach (var webhookEvent in queue.ReadAllAsync(stoppingToken))
{
try
{
await using var scope = scopeFactory.CreateAsyncScope();
var dispatcher = scope.ServiceProvider.GetRequiredService<WebhookDispatcher>();
await dispatcher.DispatchAsync(webhookEvent, attemptNumber: 1, stoppingToken);
}
catch (Exception ex)
{
logger.LogError(ex, "Unhandled error dispatching webhook event {EventType}", webhookEvent.EventType);
}
}
}
}