Files
EnvelopeGenerator/EnvelopeGenerator.ServiceHost/Worker.cs
TekH 162f066b08 Refactor Worker to use IOptions for configuration
Replaced direct IConfiguration usage in Worker with IOptions<WorkerOptions> for strongly-typed configuration access. Updated delay interval assignment and added necessary namespace import.
2026-04-13 15:25:17 +02:00

36 lines
1.2 KiB
C#

using EnvelopeGenerator.ServiceHost.Jobs;
using Microsoft.Extensions.Options;
namespace EnvelopeGenerator.ServiceHost;
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly int _delayMilliseconds;
private readonly IServiceScopeFactory _scopeFactory;
public Worker(ILogger<Worker> logger, IOptions<WorkerOptions> options, IServiceScopeFactory scopeFactory)
{
_logger = logger;
_delayMilliseconds = options.Value.DelayMilliseconds;
_scopeFactory = scopeFactory;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}
using var scope = _scopeFactory.CreateScope();
var finalizeDocumentJob = scope.ServiceProvider.GetRequiredService<FinalizeDocumentJob>();
await finalizeDocumentJob.ExecuteAsync(stoppingToken);
await Task.Delay(_delayMilliseconds, stoppingToken);
}
}
}