Migrated the legacy VB.NET Windows Service to a modern C# Worker Service targeting .NET Framework 4.6.2. Key changes include: - Added `Microsoft.Extensions.Hosting.WindowsServices`, `NLog`, and `Quartz` for logging and scheduling. - Introduced `LogConfigFactory` to bridge `ServiceConfig` and `LogConfig` for Quartz jobs. - Rewrote `Worker` to handle the full service lifecycle, including database connection, temp file management, and scheduler initialization. - Implemented `SchedulerFinishEnvelope` and `SchedulerEnvelopeTaskApi` for Quartz job scheduling. - Created `TempFiles` for service-level temp file management. - Configured `NLog` for file and console logging. - Updated `appsettings.json` for connection strings and service-specific configuration. - Replaced legacy `ProjectInstaller` with `AddWindowsService()` for SCM integration.
76 lines
2.0 KiB
C#
76 lines
2.0 KiB
C#
namespace EnvelopeGenerator.Service;
|
|
|
|
/// <summary>
|
|
/// Service-level temp file management. Mirrors <c>TempFiles.vb</c> from the legacy project.
|
|
/// Manages <c>%TEMP%\EnvelopeGenerator</c> at host startup and shutdown.
|
|
///
|
|
/// NOTE: This is independent from <c>EnvelopeGenerator.CommonServices.TempFiles</c>, which is
|
|
/// instantiated inside each Quartz job via <see cref="DigitalData.Modules.Logging.LogConfig"/>.
|
|
/// Do not confuse the two.
|
|
/// </summary>
|
|
public sealed class TempFiles
|
|
{
|
|
private readonly ILogger<TempFiles> _logger;
|
|
|
|
public string TempPath { get; }
|
|
|
|
public TempFiles(ILogger<TempFiles> logger)
|
|
{
|
|
_logger = logger;
|
|
TempPath = Path.Combine(Path.GetTempPath(), "EnvelopeGenerator");
|
|
}
|
|
|
|
public bool Create()
|
|
{
|
|
try
|
|
{
|
|
if (!Directory.Exists(TempPath))
|
|
{
|
|
Directory.CreateDirectory(TempPath);
|
|
}
|
|
else
|
|
{
|
|
CleanUpFiles();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to create temp directory: {TempPath}", TempPath);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private void CleanUpFiles()
|
|
{
|
|
try
|
|
{
|
|
foreach (var file in Directory.GetFiles(TempPath))
|
|
{
|
|
_logger.LogDebug("Deleting temp file: {File}", file);
|
|
File.Delete(file);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to clean up temp files in: {TempPath}", TempPath);
|
|
}
|
|
}
|
|
|
|
public bool CleanUp()
|
|
{
|
|
try
|
|
{
|
|
_logger.LogDebug("Deleting temp directory: {TempPath}", TempPath);
|
|
Directory.Delete(TempPath, recursive: true);
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to delete temp directory: {TempPath}", TempPath);
|
|
return false;
|
|
}
|
|
}
|
|
}
|