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:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user