2026-09-04 14:11:32 -03:00
|
|
|
using Api.SeaHavenIndustries.Observability;
|
2026-08-11 08:58:19 -03:00
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using SeaHaven.Services.Configuration;
|
|
|
|
|
using SeaHaven.Services.Interfaces;
|
2026-09-04 14:11:32 -03:00
|
|
|
using Sentry;
|
2026-08-11 08:58:19 -03:00
|
|
|
|
|
|
|
|
namespace Api.SeaHavenIndustries.HostedServices
|
|
|
|
|
{
|
|
|
|
|
public class UpliftLifecycleHostedService : BackgroundService
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
|
|
|
|
private readonly ILogger<UpliftLifecycleHostedService> _logger;
|
|
|
|
|
private readonly ApprovalsOptions _options;
|
2026-09-04 14:11:32 -03:00
|
|
|
private readonly IHub _sentryHub;
|
2026-08-11 08:58:19 -03:00
|
|
|
|
|
|
|
|
public UpliftLifecycleHostedService(
|
|
|
|
|
IServiceScopeFactory scopeFactory,
|
|
|
|
|
IOptions<ApprovalsOptions> options,
|
2026-09-04 14:11:32 -03:00
|
|
|
ILogger<UpliftLifecycleHostedService> logger,
|
|
|
|
|
IHub sentryHub)
|
2026-08-11 08:58:19 -03:00
|
|
|
{
|
|
|
|
|
_scopeFactory = scopeFactory;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_options = options.Value;
|
2026-09-04 14:11:32 -03:00
|
|
|
_sentryHub = sentryHub;
|
2026-08-11 08:58:19 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
|
{
|
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
{
|
2026-09-04 14:11:32 -03:00
|
|
|
using var transaction = SentryObservability.BeginBackgroundTransaction(
|
|
|
|
|
_sentryHub,
|
|
|
|
|
"uplifts.lifecycle-sweep",
|
|
|
|
|
$"{nameof(UpliftLifecycleHostedService)}.{nameof(SweepAsync)}");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await SweepAsync(stoppingToken);
|
|
|
|
|
transaction.FinishOk();
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
transaction.FinishCancelled();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Uplift lifecycle sweep failed; will retry on next interval");
|
|
|
|
|
transaction.FinishError(ex);
|
|
|
|
|
}
|
2026-08-11 08:58:19 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Task.Delay(_options.EffectiveSweepInterval, stoppingToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SweepAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
using var scope = _scopeFactory.CreateScope();
|
|
|
|
|
var lifecycle = scope.ServiceProvider.GetRequiredService<IUpliftLifecycleService>();
|
|
|
|
|
|
|
|
|
|
await lifecycle.ExpireDueAsync(cancellationToken);
|
|
|
|
|
await lifecycle.SendDueInitialNotificationsAsync(cancellationToken);
|
|
|
|
|
await lifecycle.EscalateDueAsync(cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|