Switched to standard MonIn library

This commit is contained in:
Chase Tucker
2024-04-09 10:44:57 -07:00
parent 794b616293
commit 156dee0751
22 changed files with 539 additions and 345 deletions

View File

@ -1,6 +1,8 @@
using FabApprovalWorkerService.Models;
using FabApprovalWorkerService.Services;
using Infineon.Monitoring.MonA;
using Quartz;
using System.Text;
@ -9,11 +11,11 @@ namespace FabApprovalWorkerService.Workers;
public sealed class ExpiredOOOStatusWorker : IJob {
private readonly ILogger<ExpiredOOOStatusWorker> _logger;
private readonly IMonInWorkerClient _monInClient;
private readonly IMonInClient _monInClient;
private readonly IUserService _userService;
public ExpiredOOOStatusWorker(ILogger<ExpiredOOOStatusWorker> logger,
IMonInWorkerClient monInClient,
IMonInClient monInClient,
IUserService userService) {
_logger = logger;
if (_logger is null)
@ -21,7 +23,7 @@ public sealed class ExpiredOOOStatusWorker : IJob {
_monInClient = monInClient;
if (_monInClient is null) {
throw new ArgumentNullException("IMonInWorkerClient not injected");
throw new ArgumentNullException("IMonInClient not injected");
}
_userService = userService;
@ -39,6 +41,8 @@ public sealed class ExpiredOOOStatusWorker : IJob {
_logger.LogInformation("Attempting to remove OOO status for users with OOO expired earlier than now");
List<User> expiredOOOUsers = await _userService.GetAllExpiredOOOUsersAsync();
_logger.LogInformation($"There are {expiredOOOUsers.Count()} OOO users expiring");
foreach (User user in expiredOOOUsers) {
bool approvalsRemoved = await _userService.RemoveDelegatedApprovalsForUser(user.UserID, user.DelegatedTo);
@ -67,12 +71,12 @@ public sealed class ExpiredOOOStatusWorker : IJob {
} finally {
DateTime end = DateTime.Now;
double latencyInMS = (end - start).TotalMilliseconds;
_monInClient.PostAverage(metricName + "Latency", latencyInMS);
_monInClient.PostMetric(metricName + "Latency", latencyInMS);
if (isInternalError) {
_monInClient.PostStatus(metricName, StatusValue.Critical);
_monInClient.PostStatus(metricName, State.Critical);
} else {
_monInClient.PostStatus(metricName, StatusValue.Ok);
_monInClient.PostStatus(metricName, State.Ok);
}
}
}

View File

@ -0,0 +1,109 @@
using FabApprovalWorkerService.Models;
using FabApprovalWorkerService.Services;
using Infineon.Monitoring.MonA;
using Quartz;
using System.Net.Mail;
using System.Text;
namespace FabApprovalWorkerService.Workers;
public class ExpiredTECNWorker : IJob {
private readonly ILogger<ExpiredTECNWorker> _logger;
private readonly IECNService _ecnService;
private readonly ITrainingService _trainingService;
private readonly IUserService _userService;
private readonly ISmtpService _smtpService;
private readonly IMonInClient _monInClient;
private readonly string _baseUrl;
public ExpiredTECNWorker(ILogger<ExpiredTECNWorker> logger,
IECNService ecnService,
ITrainingService trainingService,
IUserService userService,
ISmtpService smtpService,
IMonInClient monInClient) {
_logger = logger ??
throw new ArgumentNullException("ILogger not injected");
_ecnService = ecnService ??
throw new ArgumentNullException("IECNService not injected");
_trainingService = trainingService ??
throw new ArgumentNullException("ITrainingService not injected");
_userService = userService ??
throw new ArgumentNullException("IUserService not injected");
_smtpService = smtpService ??
throw new ArgumentNullException("ISmtpService not injected");
_monInClient = monInClient ??
throw new ArgumentNullException("IMonInClient not injected");
_baseUrl = Environment.GetEnvironmentVariable("FabApprovalBaseUrl") ??
throw new ArgumentNullException("FabApprovalBaseUrl environment variable not found");
}
public async Task Execute(IJobExecutionContext context) {
DateTime start = DateTime.Now;
bool isInternalError = false;
StringBuilder errorMessage = new();
string metricName = "ExpiredTECNWorker";
try {
_logger.LogInformation("Attempting to process expired TECNs");
List<ECN> expiredEcns = (await _ecnService.GetExpiredTECNs()).ToList();
List<MailAddress> tecnNotificationUserEmails = new();
if (expiredEcns.Any()) {
List<string> emails = (await _ecnService.GetTECNNotificationUserEmails()).ToList();
foreach (string email in emails) tecnNotificationUserEmails.Add(new MailAddress(email));
}
foreach (ECN ecn in expiredEcns) {
List<int> trainingIds = (await _trainingService.GetTrainingIdsForECN(ecn.ECNNumber)).ToList();
foreach (int trainingId in trainingIds) {
await _trainingService.DeleteTrainingAssignment(trainingId);
List<int> trainingAssignmentIds =
(await _trainingService.GetTrainingAssignmentIdsForTraining(trainingId)).ToList();
foreach (int assignmentId in trainingAssignmentIds) {
await _trainingService.DeleteDocAssignment(assignmentId);
}
await _trainingService.MarkTrainingAsComplete(trainingId);
}
string recipientEmail = await _userService.GetUserEmail(ecn.OriginatorID);
List<MailAddress> recipientEamils = new List<MailAddress>() {
new MailAddress(recipientEmail)
};
string subject = "Notice of Expired TECN";
StringBuilder bodyBuilder = new();
bodyBuilder.Append($"Good day, TECN# {ecn.ECNNumber} expired on {ecn.ExpirationDate.ToString("MMMM dd, yyyy")}. ");
bodyBuilder.Append($"<br />Review TECN <a href='{_baseUrl}/ECN/Edit?IssueID={ecn.ECNNumber}'> here. </a>");
await _smtpService.SendEmail(recipientEamils, tecnNotificationUserEmails, subject, bodyBuilder.ToString());
}
} catch (Exception ex) {
StringBuilder errMsgBuilder = new();
errMsgBuilder.Append("An exception occurred when attempting to process expired TECNs. ");
errMsgBuilder.Append($"Exception: {ex.Message}");
_logger.LogError(errMsgBuilder.ToString());
isInternalError = true;
} finally {
DateTime end = DateTime.Now;
double latencyInMS = (end - start).TotalMilliseconds;
_monInClient.PostMetric(metricName + "Latency", latencyInMS);
if (isInternalError) {
_monInClient.PostStatus(metricName, State.Critical);
} else {
_monInClient.PostStatus(metricName, State.Ok);
}
}
}
}

View File

@ -1,6 +1,8 @@
using FabApprovalWorkerService.Models;
using FabApprovalWorkerService.Services;
using Infineon.Monitoring.MonA;
using Quartz;
using System.Net.Mail;
@ -10,26 +12,29 @@ namespace FabApprovalWorkerService.Workers;
public class ExpiringTECNWorker : IJob {
private readonly ILogger<ExpiringTECNWorker> _logger;
private readonly IMonInWorkerClient _monInClient;
private readonly IMonInClient _monInClient;
private readonly IUserService _userService;
private readonly IECNService _ecnService;
private readonly ISmtpService _smtpService;
private readonly string _baseUrl;
public ExpiringTECNWorker(ILogger<ExpiringTECNWorker> logger,
IMonInWorkerClient monInClient,
IMonInClient monInClient,
IUserService userService,
IECNService ecnService,
ISmtpService smtpService) {
_logger = logger ??
throw new ArgumentNullException("ILogger not injected");
_monInClient = monInClient ??
throw new ArgumentNullException("IMonInWorkerClient not injected");
throw new ArgumentNullException("IMonInClient not injected");
_userService = userService ??
throw new ArgumentNullException("IUserService not injected");
_ecnService = ecnService ??
throw new ArgumentNullException("IECNService not injected");
_smtpService = smtpService ??
throw new ArgumentNullException("ISmtpService not injected");
_baseUrl = Environment.GetEnvironmentVariable("FabApprovalBaseUrl") ??
throw new ArgumentNullException("FabApprovalBaseUrl environment variable not found");
}
public async Task Execute(IJobExecutionContext context) {
@ -45,12 +50,15 @@ public class ExpiringTECNWorker : IJob {
_logger.LogInformation($"There are {expiringTECNs.Count()} TECNs expiring in the next 5 days");
IEnumerable<string> tecnNotificationUserEmails = new List<string>();
if (expiringTECNs.Any())
tecnNotificationUserEmails = await _ecnService.GetTECNNotificationUserEmails();
foreach (ECN eCN in expiringTECNs) {
string recipientEmail = await _userService.GetUserEmail(eCN.OriginatorID);
MailAddress recipientAddress = new MailAddress(recipientEmail);
List<MailAddress> recipientList = new () { recipientAddress };
IEnumerable<string> tecnNotificationUserEmails = await _ecnService.GetTECNNotificationUserEmails();
List<MailAddress> ccRecipientList = new();
foreach (string email in tecnNotificationUserEmails) {
ccRecipientList.Add(new MailAddress(email));
@ -59,10 +67,10 @@ public class ExpiringTECNWorker : IJob {
StringBuilder bodyBuilder = new();
bodyBuilder.Append($"Good day, TECN# {eCN.ECNNumber} will be expire on ");
bodyBuilder.Append($"{eCN.ExpirationDate.ToString("MMMM dd, yyyy")}. ");
bodyBuilder.Append($"<br /> Review TECN <a href='https://mesaapproval.mes.com/ECN/Edit?IssueID={eCN.ECNNumber}'> ");
bodyBuilder.Append($"<br /> Review TECN <a href='{_baseUrl}/ECN/Edit?IssueID={eCN.ECNNumber}'> ");
bodyBuilder.Append("here </a>");
string subject = $"Notice of expiring TECN - {eCN.Title}";
string subject = $"Notice of Expiring TECN - {eCN.Title}";
await _smtpService.SendEmail(recipientList, ccRecipientList, subject, bodyBuilder.ToString());
}
@ -75,12 +83,12 @@ public class ExpiringTECNWorker : IJob {
} finally {
DateTime end = DateTime.Now;
double latencyInMS = (end - start).TotalMilliseconds;
_monInClient.PostAverage(metricName + "Latency", latencyInMS);
_monInClient.PostMetric(metricName + "Latency", latencyInMS);
if (isInternalError) {
_monInClient.PostStatus(metricName, StatusValue.Critical);
_monInClient.PostStatus(metricName, State.Critical);
} else {
_monInClient.PostStatus(metricName, StatusValue.Ok);
_monInClient.PostStatus(metricName, State.Ok);
}
}
}

View File

@ -1,6 +1,8 @@
using FabApprovalWorkerService.Models;
using FabApprovalWorkerService.Services;
using Infineon.Monitoring.MonA;
using Quartz;
using System.Text;
@ -9,11 +11,11 @@ namespace FabApprovalWorkerService.Workers;
public sealed class PendingOOOStatusWorker : IJob {
private readonly ILogger<PendingOOOStatusWorker> _logger;
private readonly IMonInWorkerClient _monInClient;
private readonly IMonInClient _monInClient;
private readonly IUserService _userService;
public PendingOOOStatusWorker(ILogger<PendingOOOStatusWorker> logger,
IMonInWorkerClient monInClient,
IMonInClient monInClient,
IUserService userService) {
_logger = logger;
if (_logger is null)
@ -21,7 +23,7 @@ public sealed class PendingOOOStatusWorker : IJob {
_monInClient = monInClient;
if (_monInClient is null) {
throw new ArgumentNullException("IMonInWorkerClient not injected");
throw new ArgumentNullException("IMonInClient not injected");
}
_userService = userService;
@ -39,25 +41,27 @@ public sealed class PendingOOOStatusWorker : IJob {
_logger.LogInformation("Attempting to set OOO status for users pending earlier than now");
List<OOOTemp> pendingOOOUsers = await _userService.GetAllPendingOOOUsersAsync();
_logger.LogInformation($"There are {pendingOOOUsers.Count()} pending OOO users");
foreach (OOOTemp oooTemp in pendingOOOUsers) {
Task<bool> userAlreadyOOO = _userService.IsUserAlreadyOOO(oooTemp.OOOUserID);
Task<bool> delegateAlreadyADelegate = _userService.IsDelegatorAlreadyDelegatedTo(oooTemp.DelegatedTo);
bool userAlreadyOOO = await _userService.IsUserAlreadyOOO(oooTemp.OOOUserID);
bool delegateAlreadyADelegate = await _userService.IsDelegatorAlreadyDelegatedTo(oooTemp.DelegatedTo);
if (!userAlreadyOOO.Result && !delegateAlreadyADelegate.Result) {
List<Task> enableOOOTasks = new();
enableOOOTasks.Add(_userService.InsertDelegatedRoles(oooTemp.OOOUserID));
enableOOOTasks.Add(_userService.DelegateUserSubRoles(oooTemp.OOOUserID, oooTemp.DelegatedTo));
enableOOOTasks.Add(_userService.DelegateApprovalsForUser(oooTemp.OOOUserID, oooTemp.DelegatedTo));
Task enableOOOTasksResult = Task.WhenAll(enableOOOTasks);
if (!userAlreadyOOO && !delegateAlreadyADelegate) {
await _userService.InsertDelegatedRoles(oooTemp.OOOUserID);
await _userService.DelegateUserSubRoles(oooTemp.OOOUserID, oooTemp.DelegatedTo);
await _userService.DelegateApprovalsForUser(oooTemp.OOOUserID, oooTemp.DelegatedTo);
if (enableOOOTasksResult.IsCompletedSuccessfully) {
bool userIsNowOOO = await _userService.FlagUserAsOOO(oooTemp);
if (userIsNowOOO) {
oooTemp.Processed = true;
await _userService.SetOOOTempProcessed(oooTemp);
}
bool userIsNowOOO = await _userService.FlagUserAsOOO(oooTemp);
if (userIsNowOOO) {
oooTemp.Processed = true;
await _userService.SetOOOTempProcessed(oooTemp);
}
} else if (userAlreadyOOO) {
_logger.LogInformation($"User {oooTemp.OOOUserID} is already OOO");
} else if (delegateAlreadyADelegate) {
_logger.LogInformation($"Delegate {oooTemp.DelegatedTo} is already a delegate");
}
}
@ -71,12 +75,12 @@ public sealed class PendingOOOStatusWorker : IJob {
} finally {
DateTime end = DateTime.Now;
double latencyInMS = (end - start).TotalMilliseconds;
_monInClient.PostAverage(metricName + "Latency", latencyInMS);
_monInClient.PostMetric(metricName + "Latency", latencyInMS);
if (isInternalError) {
_monInClient.PostStatus(metricName, StatusValue.Critical);
_monInClient.PostStatus(metricName, State.Critical);
} else {
_monInClient.PostStatus(metricName, StatusValue.Ok);
_monInClient.PostStatus(metricName, State.Ok);
}
}
}