Files
Partsource/PartSource.Automation/Jobs/ExecuteSsisPackages.cs
2025-04-09 11:51:59 -04:00

70 lines
2.2 KiB
C#

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using PartSource.Automation.Models.Configuration;
using PartSource.Automation.Models.Ftp;
using PartSource.Automation.Services;
using Ratermania.Automation.Interfaces;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace PartSource.Automation.Jobs
{
public class ExecuteSsisPackages : IAutomationJob
{
private readonly EmailService _emailService;
private readonly FtpService _ftpService;
private readonly SsisService _ssisService;
private readonly ILogger<ExecuteSsisPackages> _logger;
// TODO: set from config
private readonly string[] _ssisPackages = {"Parts Price" };
public ExecuteSsisPackages(EmailService emailService, IConfiguration configuration, SsisService ssisService, ILogger<ExecuteSsisPackages> logger)
{
FtpConfiguration ftpConfiguration = configuration.GetSection("FtpServers:AutomationConfiguration").Get<FtpConfiguration>();
_emailService = emailService;
_ftpService = new FtpService(ftpConfiguration);
_ssisService = ssisService;
_logger = logger;
}
public async Task Run(CancellationToken token, params string[] arguments)
{
await Task.Run(() =>
{
foreach (string package in _ssisPackages)
{
try
{
FtpFileInfo lastUploadedFile = _ftpService.ListFilesExtended()
.Where(f => f.FileType == FtpFileType.File && f.Filename.IndexOf(package) > -1)
.OrderByDescending(f => f.Modified)
.FirstOrDefault();
if (lastUploadedFile == null)
{
_logger.LogInformation($"No {package} file available.");
return;
}
_ftpService.Download($"{package}.txt");
_ssisService.Execute($"{package}.dtsx");
_logger.LogInformation($"Execution of SSIS package {package} completed successfully.");
}
catch (Exception ex)
{
_logger.LogError($"Execution of SSIS package {package} failed.", ex);
throw;
}
}
// _emailService.Send("Database Updates Complete", "Database updates to support pricing and availability have completed successfully.");
});
}
}
}