Created Windows Service
This commit is contained in:
@ -32,13 +32,13 @@ public class ECNService : IECNService {
|
||||
queryBuilder.Append($"ExpirationDate between '{yesterday}' ");
|
||||
queryBuilder.Append($"and '{today}'");
|
||||
|
||||
IEnumerable<ECN> expiredTecns = (await _dalService.QueryAsync<ECN>(queryBuilder.ToString()))
|
||||
.Where(e => e.ExtensionDate is null || e.ExtensionDate < DateTime.Now)
|
||||
.ToList();
|
||||
IEnumerable<ECN> expiredTecns = (await _dalService.QueryAsync<ECN>(queryBuilder.ToString()));
|
||||
|
||||
_logger.LogInformation($"Found {expiredTecns.Count()} expired TECNs");
|
||||
IEnumerable<ECN> expiredTecnsNotExtended = expiredTecns
|
||||
.Where(e => e.ExtensionDate < DateTime.Now)
|
||||
.ToList();
|
||||
|
||||
return expiredTecns;
|
||||
return expiredTecnsNotExtended;
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append("An exception occurred when attempting to get all TECNs expired in the last day. ");
|
||||
@ -56,18 +56,18 @@ public class ECNService : IECNService {
|
||||
string fiveDaysFromToday = DateTime.Now.AddDays(5).ToString("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
StringBuilder queryBuilder = new StringBuilder();
|
||||
queryBuilder.Append("select ECNNumber, IsTECN, ExpirationDate, ExtensionDate, OriginatorID, Title ");
|
||||
queryBuilder.Append($"from ECN where IsTECN = 1 and ");
|
||||
queryBuilder.Append("select * from ECN ");
|
||||
queryBuilder.Append($"where IsTECN = 1 and ");
|
||||
queryBuilder.Append($"ExpirationDate between '{today}' ");
|
||||
queryBuilder.Append($"and '{fiveDaysFromToday}'");
|
||||
queryBuilder.Append($"and '{fiveDaysFromToday}';");
|
||||
|
||||
IEnumerable<ECN> expiringTecns = (await _dalService.QueryAsync<ECN>(queryBuilder.ToString()))
|
||||
.Where(e => e.ExtensionDate is null || e.ExtensionDate <= DateTime.Now.AddDays(5))
|
||||
IEnumerable<ECN> expiringTecns = (await _dalService.QueryAsync<ECN>(queryBuilder.ToString()));
|
||||
|
||||
IEnumerable<ECN> expiringTecnsNotExtended = expiringTecns
|
||||
.Where(e => e.ExtensionDate <= DateTime.Now.AddDays(5))
|
||||
.ToList();
|
||||
|
||||
_logger.LogInformation($"Found {expiringTecns.Count()} expiring TECNs");
|
||||
|
||||
return expiringTecns;
|
||||
return expiringTecnsNotExtended;
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append("An exception occurred when attempting to get all TECNs expiring in the next five days. ");
|
||||
|
102
FabApprovalWorkerService/Services/TrainingService.cs
Normal file
102
FabApprovalWorkerService/Services/TrainingService.cs
Normal file
@ -0,0 +1,102 @@
|
||||
using FabApprovalWorkerService.Models;
|
||||
|
||||
using System.Text;
|
||||
|
||||
namespace FabApprovalWorkerService.Services;
|
||||
|
||||
public interface ITrainingService {
|
||||
Task<IEnumerable<int>> GetTrainingIdsForECN(int ecnNumber);
|
||||
Task DeleteTrainingAssignment(int trainingId);
|
||||
Task<IEnumerable<int>> GetTrainingAssignmentIdsForTraining(int trainingId);
|
||||
Task DeleteDocAssignment(int trainingAssignmentId);
|
||||
}
|
||||
|
||||
public class TrainingService : ITrainingService {
|
||||
private ILogger<TrainingService> _logger;
|
||||
private IDalService _dalService;
|
||||
|
||||
public TrainingService(ILogger<TrainingService> logger, IDalService dalService) {
|
||||
_logger = logger ??
|
||||
throw new ArgumentNullException("ILogger not injected");
|
||||
_dalService = dalService ??
|
||||
throw new ArgumentNullException("IDalService not injected");
|
||||
}
|
||||
|
||||
public async Task DeleteDocAssignment(int trainingAssignmentId) {
|
||||
if (trainingAssignmentId <= 0) throw new ArgumentException($"Invalid training assignment id: {trainingAssignmentId}");
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to delete training doc assignments for training assignment {trainingAssignmentId}");
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append($"update TrainingDocAcks set Deleted = 1, ");
|
||||
queryBuilder.Append($"DeletedDate = {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} ");
|
||||
queryBuilder.Append($"where TrainingAssignmentID = {trainingAssignmentId} and Reviewed = 0;");
|
||||
|
||||
await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append($"An exception occurred when attempting to delete training doc assignment ");
|
||||
errMsgBuilder.Append($"{trainingAssignmentId}. Exception: {ex.Message}");
|
||||
_logger.LogError(errMsgBuilder.ToString());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteTrainingAssignment(int trainingId) {
|
||||
if (trainingId <= 0) throw new ArgumentException($"Invalid training id: {trainingId}");
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to delete training assignment {trainingId}");
|
||||
|
||||
StringBuilder queryBuilder = new();
|
||||
queryBuilder.Append($"update TrainingAssignments set Deleted = 1, ");
|
||||
queryBuilder.Append($"DeletedDate = {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} ");
|
||||
queryBuilder.Append($"where TrainingID = {trainingId} and status = 0;");
|
||||
|
||||
await _dalService.ExecuteAsync(queryBuilder.ToString());
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append($"An exception occurred when attempting to delete training assignment ");
|
||||
errMsgBuilder.Append($"{trainingId}. Exception: {ex.Message}");
|
||||
_logger.LogError(errMsgBuilder.ToString());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<int>> GetTrainingAssignmentIdsForTraining(int trainingId) {
|
||||
if (trainingId <= 0) throw new ArgumentException($"Invalid trainingID: {trainingId}");
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get training assignment ids for training id {trainingId}");
|
||||
|
||||
string sql = $"select ID from TrainingAssignments where TrainingID = {trainingId};";
|
||||
|
||||
return await _dalService.QueryAsync<int>(sql);
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append($"An exception occurred when attempting to get training assignment ids ");
|
||||
errMsgBuilder.Append($"for training id {trainingId}. Exception: {ex.Message}");
|
||||
_logger.LogError(errMsgBuilder.ToString());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<int>> GetTrainingIdsForECN(int ecnNumber) {
|
||||
if (ecnNumber <= 0) throw new ArgumentException($"Invalid ecnNumber: {ecnNumber}");
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get training ids for ecn {ecnNumber}");
|
||||
|
||||
string sql = $"select TrainingID from Training where ECN = {ecnNumber};";
|
||||
|
||||
return await _dalService.QueryAsync<int>(sql);
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append($"An exception occurred when attempting to get training ids ");
|
||||
errMsgBuilder.Append($"for ECN {ecnNumber}. Exception: {ex.Message}");
|
||||
_logger.LogError(errMsgBuilder.ToString());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
40
FabApprovalWorkerService/Services/WindowsService.cs
Normal file
40
FabApprovalWorkerService/Services/WindowsService.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using FabApprovalWorkerService.Models;
|
||||
|
||||
namespace FabApprovalWorkerService.Services;
|
||||
|
||||
public class WindowsService : BackgroundService {
|
||||
private readonly ILogger<WindowsService> _logger;
|
||||
private readonly IMonInWorkerClient _monInClient;
|
||||
|
||||
public WindowsService(ILogger<WindowsService> logger,
|
||||
IServiceProvider serviceProvider) {
|
||||
_logger = logger ??
|
||||
throw new ArgumentNullException("ILogger not injected");
|
||||
using (IServiceScope scope = serviceProvider.CreateScope()) {
|
||||
_monInClient = scope.ServiceProvider.GetService<IMonInWorkerClient>() ??
|
||||
throw new ArgumentNullException("IMonInWorkerClient not injected");
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
|
||||
_logger.LogInformation("Starting Windows service");
|
||||
|
||||
try {
|
||||
while (!stoppingToken.IsCancellationRequested) {
|
||||
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
|
||||
|
||||
_monInClient.PostStatus("WindowsService", StatusValue.Ok);
|
||||
}
|
||||
} catch (OperationCanceledException) {
|
||||
_logger.LogError("The Windows service has been stopped");
|
||||
|
||||
_monInClient.PostStatus("WindowsService", StatusValue.Critical);
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"An exception occurred when running Windows Service. Exception: {ex.Message}");
|
||||
|
||||
_monInClient.PostStatus("WindowsService", StatusValue.Critical);
|
||||
|
||||
Environment.Exit(1);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user