2024-04-10 10:23:54 -07:00

72 lines
3.1 KiB
C#

using FabApprovalWorkerService.Models;
using FabApprovalWorkerService.Services;
using Infineon.Monitoring.MonA;
using Quartz;
using System.Text;
namespace FabApprovalWorkerService.Workers;
public class TrainingNotificationWorker : IJob {
private readonly ILogger<TrainingNotificationWorker> _logger;
private readonly ITrainingService _trainingService;
private readonly IUserService _userService;
private readonly IECNService _ecnService;
private readonly ISmtpService _smtpService;
private readonly IMonInClient _monInClient;
public TrainingNotificationWorker(ILogger<TrainingNotificationWorker> logger,
ITrainingService trainingService,
IUserService userService,
IECNService ecnService,
ISmtpService smtpService,
IMonInClient monInClient) {
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
_trainingService = trainingService ?? throw new ArgumentNullException("ITrainingService 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");
_monInClient = monInClient ?? throw new ArgumentNullException("IMonInClient not injected");
}
public async Task Execute(IJobExecutionContext context) {
DateTime start = DateTime.Now;
bool isInternalError = false;
StringBuilder errorMessage = new();
string metricName = "TrainingNotificationWorker";
try {
_logger.LogInformation("Attempting to send training notifications");
IEnumerable<TrainingAssignment> trainingAssignments = await _trainingService.GetActiveTrainingAssignments();
foreach (TrainingAssignment trainingAssignment in trainingAssignments) {
ECN ecn = await _ecnService.GetEcnByNumber
User user = await _userService.GetUserById(trainingAssignment.UserID);
}
_logger.LogInformation("Successfully sent training notifications");
} catch (Exception ex) {
StringBuilder errMsgBuilder = new();
errMsgBuilder.Append("An exception occurred when attempting to send training notifications. ");
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);
}
}
}
}