Refactor worker to use config, DI, and Quartz scheduling
- Add WorkerSettings class and update appsettings for config-driven setup - Integrate Quartz.NET for job scheduling (FinalizeDocumentJob, APIEnvelopeJob) - Refactor Program.cs for DI of services (TempFileManager, PDFBurner, etc.) - Implement TempFileManager for temp folder management and cleanup - Rewrite Worker class for config validation, DB check, and lifecycle logging - Update csproj to include Quartz and EnvelopeGenerator.Jobs references - Improve maintainability, error handling, and logging throughout
This commit is contained in:
@@ -1,24 +1,71 @@
|
||||
namespace EnvelopeGenerator.WorkerService
|
||||
using EnvelopeGenerator.WorkerService.Configuration;
|
||||
using EnvelopeGenerator.WorkerService.Services;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace EnvelopeGenerator.WorkerService;
|
||||
|
||||
public class Worker : BackgroundService
|
||||
{
|
||||
public class Worker : BackgroundService
|
||||
private readonly ILogger<Worker> _logger;
|
||||
private readonly WorkerSettings _settings;
|
||||
private readonly TempFileManager _tempFiles;
|
||||
|
||||
public Worker(
|
||||
ILogger<Worker> logger,
|
||||
IOptions<WorkerSettings> settings,
|
||||
TempFileManager tempFiles)
|
||||
{
|
||||
private readonly ILogger<Worker> _logger;
|
||||
_logger = logger;
|
||||
_settings = settings.Value;
|
||||
_tempFiles = tempFiles;
|
||||
}
|
||||
|
||||
public Worker(ILogger<Worker> logger)
|
||||
public override async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Starting EnvelopeGenerator worker...");
|
||||
_logger.LogInformation("Debug mode: {Debug}", _settings.Debug);
|
||||
|
||||
ValidateConfiguration();
|
||||
await EnsureDatabaseConnectionAsync(cancellationToken);
|
||||
await _tempFiles.CreateAsync(cancellationToken);
|
||||
|
||||
await base.StartAsync(cancellationToken);
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_logger.LogInformation("EnvelopeGenerator worker is running. Jobs are scheduled every {Interval} minute(s).", Math.Max(1, _settings.IntervalMinutes));
|
||||
await Task.Delay(Timeout.Infinite, stoppingToken);
|
||||
}
|
||||
|
||||
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Stopping EnvelopeGenerator worker...");
|
||||
await _tempFiles.CleanupAsync(cancellationToken);
|
||||
await base.StopAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private void ValidateConfiguration()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_settings.ConnectionString))
|
||||
{
|
||||
_logger = logger;
|
||||
throw new InvalidOperationException("Connection string cannot be empty. Configure 'WorkerSettings:ConnectionString'.");
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
private async Task EnsureDatabaseConnectionAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
}
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
}
|
||||
await using var connection = new SqlConnection(_settings.ConnectionString);
|
||||
await connection.OpenAsync(cancellationToken);
|
||||
_logger.LogInformation("Database connection established successfully.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Database connection could not be established.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user