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:
2026-01-22 09:51:35 +01:00
parent f078bafdde
commit d4b1a4921c
7 changed files with 250 additions and 15 deletions

View File

@@ -1,6 +1,66 @@
using EnvelopeGenerator.Domain.Constants;
using EnvelopeGenerator.Jobs.APIBackendJobs;
using EnvelopeGenerator.Jobs.FinalizeDocument;
using EnvelopeGenerator.WorkerService;
using EnvelopeGenerator.WorkerService.Configuration;
using EnvelopeGenerator.WorkerService.Services;
using Quartz;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.Configure<WorkerSettings>(builder.Configuration.GetSection("WorkerSettings"));
builder.Services.AddSingleton<TempFileManager>();
builder.Services.AddSingleton(provider =>
{
var settings = provider.GetRequiredService<Microsoft.Extensions.Options.IOptions<WorkerSettings>>().Value;
var logger = provider.GetRequiredService<ILogger<PDFBurner>>();
return new PDFBurner(logger, settings.PdfBurner);
});
builder.Services.AddSingleton<PDFMerger>();
builder.Services.AddSingleton<ReportCreator>();
builder.Services.AddQuartz(q =>
{
q.UseMicrosoftDependencyInjectionJobFactory();
q.UseDefaultThreadPool(tp => tp.MaxConcurrency = 5);
var settings = new WorkerSettings();
builder.Configuration.GetSection("WorkerSettings").Bind(settings);
var intervalMinutes = Math.Max(1, settings.IntervalMinutes);
var finalizeJobKey = new JobKey("FinalizeDocumentJob");
q.AddJob<FinalizeDocumentJob>(opts => opts
.WithIdentity(finalizeJobKey)
.UsingJobData(Value.DATABASE, settings.ConnectionString));
q.AddTrigger(opts => opts
.ForJob(finalizeJobKey)
.WithIdentity("FinalizeDocumentJob-trigger")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInMinutes(intervalMinutes)
.RepeatForever()));
var apiJobKey = new JobKey("APIEnvelopeJob");
q.AddJob<APIEnvelopeJob>(opts => opts
.WithIdentity(apiJobKey)
.UsingJobData(Value.DATABASE, settings.ConnectionString));
q.AddTrigger(opts => opts
.ForJob(apiJobKey)
.WithIdentity("APIEnvelopeJob-trigger")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInMinutes(intervalMinutes)
.RepeatForever()));
});
builder.Services.AddQuartzHostedService(options =>
{
options.WaitForJobsToComplete = true;
});
builder.Services.AddHostedService<Worker>();
var host = builder.Build();