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,18 +1,20 @@
using FabApprovalWorkerService.Models;
using Infineon.Monitoring.MonA;
namespace FabApprovalWorkerService.Services;
public class WindowsService : BackgroundService {
private readonly ILogger<WindowsService> _logger;
private readonly IMonInWorkerClient _monInClient;
private readonly IMonInClient _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");
_monInClient = scope.ServiceProvider.GetService<IMonInClient>() ??
throw new ArgumentNullException("IMonInClient not injected");
}
}
@ -21,18 +23,18 @@ public class WindowsService : BackgroundService {
try {
while (!stoppingToken.IsCancellationRequested) {
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
_monInClient.PostStatus("WindowsService", State.Ok);
_monInClient.PostStatus("WindowsService", StatusValue.Ok);
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
}
} catch (OperationCanceledException) {
_logger.LogError("The Windows service has been stopped");
_monInClient.PostStatus("WindowsService", StatusValue.Critical);
_monInClient.PostStatus("WindowsService", State.Critical);
} catch (Exception ex) {
_logger.LogError($"An exception occurred when running Windows Service. Exception: {ex.Message}");
_monInClient.PostStatus("WindowsService", StatusValue.Critical);
_monInClient.PostStatus("WindowsService", State.Critical);
Environment.Exit(1);
}