Created CertificationTrainingGroupWorker
This commit is contained in:
@ -0,0 +1,69 @@
|
||||
using FabApprovalWorkerService.Models;
|
||||
using FabApprovalWorkerService.Services;
|
||||
|
||||
using Infineon.Monitoring.MonA;
|
||||
|
||||
using Quartz;
|
||||
|
||||
using System.Text;
|
||||
|
||||
namespace FabApprovalWorkerService.Workers;
|
||||
|
||||
public class CertificationTrainingGroupWorker : IJob {
|
||||
private static readonly int MAX_RETRIES = 3;
|
||||
private static readonly int BACKOFF_SECONDS = 30;
|
||||
|
||||
private readonly ILogger<CertificationTrainingGroupWorker> _logger;
|
||||
private readonly ITrainingService _trainingService;
|
||||
private readonly IMonInClient _monInClient;
|
||||
|
||||
public CertificationTrainingGroupWorker(ILogger<CertificationTrainingGroupWorker> logger,
|
||||
ITrainingService trainingService,
|
||||
IMonInClient monInClient) {
|
||||
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
|
||||
_trainingService = trainingService ?? throw new ArgumentNullException("ITrainingService not injected");
|
||||
_monInClient = monInClient ?? throw new ArgumentNullException("IMonInClient not injected");
|
||||
}
|
||||
|
||||
public async Task Execute(IJobExecutionContext context) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isInternalError = false;
|
||||
string metricName = "CertificationTrainingGroupWorker";
|
||||
|
||||
try {
|
||||
int remainingRetries = MAX_RETRIES;
|
||||
bool isSuccessful = false;
|
||||
|
||||
while (!isSuccessful && remainingRetries > 0) {
|
||||
await Task.Delay((MAX_RETRIES - remainingRetries--) * BACKOFF_SECONDS);
|
||||
|
||||
_logger.LogInformation($"Attempting to update certification training group membership. Remaining retries: {remainingRetries}");
|
||||
|
||||
IEnumerable<UserCertificationRecord> certRecords = await _trainingService.GetUserCertificationRecords();
|
||||
isSuccessful = await _trainingService.UpdateTrainingGroupMembership(certRecords);
|
||||
}
|
||||
|
||||
if (isSuccessful) {
|
||||
_logger.LogInformation("Successfully updated certification training group membership.");
|
||||
} else {
|
||||
throw new Exception("Unable to update certification training group membership.");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
StringBuilder errMsgBuilder = new();
|
||||
errMsgBuilder.Append("An exception occurred when attempting to update certification training group membership. ");
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user