Refactor job execution to remove Quartz and job runner
Simplify FinalizeDocumentJob execution by removing Quartz dependencies and the IFinalizeDocumentJobRunner abstraction. The job now uses an ExecuteAsync method with direct access to configuration and options via dependency injection. Worker is updated to call the job directly, and service registration is streamlined. This improves clarity and integration with .NET DI.
This commit is contained in:
@@ -10,7 +10,6 @@ public static class ServiceCollectionExtensions
|
||||
{
|
||||
services.Configure<WorkerOptions>(configuration.GetSection(nameof(WorkerOptions)));
|
||||
services.AddSingleton<FinalizeDocumentJob>();
|
||||
services.AddSingleton<IFinalizeDocumentJobRunner, FinalizeDocumentJobRunner>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,14 +49,14 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, IConfiguration
|
||||
public byte[]? DocAsByte { get; set; }
|
||||
}
|
||||
|
||||
public Task Execute(IJobExecutionContext context)
|
||||
public Task ExecuteAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var gdPictureKey = (string)context.MergedJobDataMap[Value.GDPICTURE];
|
||||
_logConfig = (LogConfig)context.MergedJobDataMap[Value.LOGCONFIG];
|
||||
var gdPictureKey = _options.GdPictureLicenseKey;
|
||||
_logConfig = new LogConfig { Debug = _options.Debug };
|
||||
_logger = _logConfig.GetLogger();
|
||||
_tempFiles = new TempFiles(_logConfig);
|
||||
_tempFiles.Create();
|
||||
var jobId = context.JobDetail.Key;
|
||||
var jobId = typeof(FinalizeDocumentJob).FullName;
|
||||
_logger.Debug("Starting job {0}", jobId);
|
||||
|
||||
try
|
||||
@@ -66,7 +66,7 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, IConfiguration
|
||||
_licenseManager.RegisterKEY(gdPictureKey);
|
||||
|
||||
_logger.Debug("Loading Database..");
|
||||
var connectionString = (string)context.MergedJobDataMap[Value.DATABASE];
|
||||
var connectionString = config.GetConnectionString("Default") ?? throw new InvalidOperationException("Connection string 'Default' not found.");
|
||||
_databaseConnectionString = MSSQLServer.DecryptConnectionString(connectionString);
|
||||
_database = new MSSQLServer(_logConfig, _databaseConnectionString);
|
||||
|
||||
@@ -81,7 +81,7 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, IConfiguration
|
||||
InitializeServices(state);
|
||||
|
||||
_logger.Debug("Loading PDFBurner..");
|
||||
var pdfBurnerParams = (PDFBurnerParams)context.MergedJobDataMap[Value.PDF_BURNER_PARAMS];
|
||||
var pdfBurnerParams = _options.PdfBurnerParams;
|
||||
_pdfBurner = new PDFBurner(_logConfig, gdPictureKey, pdfBurnerParams, _databaseConnectionString);
|
||||
|
||||
_logger.Debug("Loading PDFMerger..");
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
using DigitalData.Modules.Logging;
|
||||
using EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Quartz;
|
||||
|
||||
namespace EnvelopeGenerator.ServiceHost.Jobs;
|
||||
|
||||
public interface IFinalizeDocumentJobRunner
|
||||
{
|
||||
Task RunAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
public class FinalizeDocumentJobRunner : IFinalizeDocumentJobRunner
|
||||
{
|
||||
private readonly FinalizeDocumentJob _job;
|
||||
private readonly IOptions<FinalizeDocumentJobOptions> _options;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<FinalizeDocumentJobRunner> _logger;
|
||||
|
||||
public FinalizeDocumentJobRunner(
|
||||
FinalizeDocumentJob job,
|
||||
IOptions<FinalizeDocumentJobOptions> options,
|
||||
IConfiguration configuration,
|
||||
ILogger<FinalizeDocumentJobRunner> logger)
|
||||
{
|
||||
_job = job;
|
||||
_options = options;
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task RunAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var options = _options.Value;
|
||||
var connectionString = options.ConnectionString;
|
||||
if (string.IsNullOrWhiteSpace(connectionString))
|
||||
{
|
||||
connectionString = _configuration.GetConnectionString("Default") ?? string.Empty;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(connectionString))
|
||||
{
|
||||
_logger.LogWarning("FinalizeDocumentJob skipped: missing connection string.");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
var dataMap = new JobDataMap
|
||||
{
|
||||
{ JobDataKeys.GdPicture, options.GdPictureLicenseKey },
|
||||
{ JobDataKeys.Database, connectionString },
|
||||
{ JobDataKeys.LogConfig, new LogConfig { Debug = options.Debug } },
|
||||
{ JobDataKeys.PdfBurnerParams, options.PdfBurnerParams }
|
||||
};
|
||||
|
||||
return _job.Execute(dataMap, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,18 @@
|
||||
using EnvelopeGenerator.ServiceHost.Jobs;
|
||||
|
||||
namespace EnvelopeGenerator.ServiceHost;
|
||||
|
||||
public class Worker : BackgroundService
|
||||
{
|
||||
private readonly ILogger<Worker> _logger;
|
||||
private readonly int _delayMilliseconds;
|
||||
private readonly IFinalizeDocumentJobRunner _jobRunner;
|
||||
private readonly FinalizeDocumentJob _finalizeDocumentJob;
|
||||
|
||||
public Worker(ILogger<Worker> logger, IConfiguration configuration, IFinalizeDocumentJobRunner jobRunner)
|
||||
public Worker(ILogger<Worker> logger, IConfiguration configuration, FinalizeDocumentJob finalizeDocumentJob)
|
||||
{
|
||||
_logger = logger;
|
||||
_delayMilliseconds = Math.Max(1, configuration.GetValue("Worker:DelayMilliseconds", 1000));
|
||||
_jobRunner = jobRunner;
|
||||
_finalizeDocumentJob = finalizeDocumentJob;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
@@ -22,7 +24,7 @@ public class Worker : BackgroundService
|
||||
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
}
|
||||
|
||||
await _jobRunner.RunAsync(stoppingToken);
|
||||
await _finalizeDocumentJob.ExecuteAsync(stoppingToken);
|
||||
await Task.Delay(_delayMilliseconds, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user