98 lines
4.2 KiB
C#
98 lines
4.2 KiB
C#
using FabApprovalWorkerService.Models;
|
|
using FabApprovalWorkerService.Services;
|
|
|
|
using Infineon.Monitoring.MonA;
|
|
|
|
using Quartz;
|
|
|
|
using System.Net.Mail;
|
|
using System.Text;
|
|
|
|
namespace FabApprovalWorkerService.Workers;
|
|
|
|
public class ExpiringTECNWorker : IJob {
|
|
private readonly ILogger<ExpiringTECNWorker> _logger;
|
|
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,
|
|
IMonInClient monInClient,
|
|
IUserService userService,
|
|
IECNService ecnService,
|
|
ISmtpService smtpService) {
|
|
_logger = logger ??
|
|
throw new ArgumentNullException("ILogger not injected");
|
|
_monInClient = monInClient ??
|
|
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) {
|
|
DateTime start = DateTime.Now;
|
|
bool isInternalError = false;
|
|
StringBuilder errorMessage = new();
|
|
string metricName = "ExpiringTECNWorker";
|
|
|
|
try {
|
|
_logger.LogInformation("Attempting to notify users of expiring TECNs");
|
|
|
|
IEnumerable<ECN> expiringTECNs = await _ecnService.GetExpiringTECNs();
|
|
|
|
_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 };
|
|
|
|
List<MailAddress> ccRecipientList = new();
|
|
foreach (string email in tecnNotificationUserEmails) {
|
|
ccRecipientList.Add(new MailAddress(email));
|
|
}
|
|
|
|
DateTime latestExpirationDate = (eCN.ExtensionDate > eCN.ExpirationDate) ? eCN.ExtensionDate : eCN.ExpirationDate;
|
|
|
|
StringBuilder bodyBuilder = new();
|
|
bodyBuilder.Append($"Good day, TECN# {eCN.ECNNumber} will be expire on ");
|
|
bodyBuilder.Append($"{latestExpirationDate.ToString("MMMM dd, yyyy")}. ");
|
|
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}";
|
|
|
|
await _smtpService.SendEmail(recipientList, ccRecipientList, subject, bodyBuilder.ToString());
|
|
}
|
|
} catch (Exception ex) {
|
|
StringBuilder errMsgBuilder = new();
|
|
errMsgBuilder.Append("An exception occurred when attempting to notify users of expiring 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);
|
|
}
|
|
}
|
|
}
|
|
}
|