Make worker delay configurable via appsettings.json

Refactored Worker to accept IConfiguration and read the delay interval from "Worker:DelayMilliseconds" in appsettings.json, replacing the previously hardcoded value. This allows the worker execution interval to be configured without code changes. Added a minimum delay safeguard and updated appsettings.json accordingly.
This commit is contained in:
2026-02-23 17:17:27 +01:00
parent c93c32307a
commit eb46590c1d
2 changed files with 7 additions and 2 deletions

View File

@@ -3,10 +3,12 @@ namespace EnvelopeGenerator.ServiceHost;
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly int _delayMilliseconds;
public Worker(ILogger<Worker> logger)
public Worker(ILogger<Worker> logger, IConfiguration configuration)
{
_logger = logger;
_delayMilliseconds = Math.Max(1, configuration.GetValue("Worker:DelayMilliseconds", 1000));
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
@@ -17,7 +19,7 @@ public class Worker : BackgroundService
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}
await Task.Delay(1000, stoppingToken);
await Task.Delay(_delayMilliseconds, stoppingToken);
}
}
}

View File

@@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"Worker": {
"DelayMilliseconds": 1000
},
"AllowedHosts": "*"
}