- 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
75 lines
1.9 KiB
C#
75 lines
1.9 KiB
C#
using System.IO;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace EnvelopeGenerator.WorkerService.Services;
|
|
|
|
public sealed class TempFileManager
|
|
{
|
|
private readonly ILogger<TempFileManager> _logger;
|
|
|
|
public TempFileManager(ILogger<TempFileManager> logger)
|
|
{
|
|
_logger = logger;
|
|
TempPath = Path.Combine(Path.GetTempPath(), "EnvelopeGenerator");
|
|
}
|
|
|
|
public string TempPath { get; }
|
|
|
|
public Task CreateAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
if (!Directory.Exists(TempPath))
|
|
{
|
|
Directory.CreateDirectory(TempPath);
|
|
_logger.LogDebug("Created temp folder {TempPath}", TempPath);
|
|
}
|
|
else
|
|
{
|
|
CleanUpFiles();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to create temp folder {TempPath}", TempPath);
|
|
throw;
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task CleanupAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
if (Directory.Exists(TempPath))
|
|
{
|
|
_logger.LogDebug("Deleting temp folder {TempPath}", TempPath);
|
|
Directory.Delete(TempPath, recursive: true);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to clean up temp folder {TempPath}", TempPath);
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private void CleanUpFiles()
|
|
{
|
|
foreach (var file in Directory.GetFiles(TempPath))
|
|
{
|
|
try
|
|
{
|
|
_logger.LogDebug("Deleting temp file {File}", file);
|
|
File.Delete(file);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "Failed to delete temp file {File}", file);
|
|
}
|
|
}
|
|
}
|
|
}
|