Created PendingOOOStatusWorker
This commit is contained in:
181
FabApprovalWorkerService/Services/MonInWorkerClient.cs
Normal file
181
FabApprovalWorkerService/Services/MonInWorkerClient.cs
Normal file
@ -0,0 +1,181 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
using FabApprovalWorkerService.Models;
|
||||
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
namespace FabApprovalWorkerService.Services;
|
||||
|
||||
public interface IMonInWorkerClient {
|
||||
void PostAverage(string metricName, double metricValue);
|
||||
void PostCount(string metricName, double metricValue);
|
||||
void PostStatus(string statusName, StatusValue statusValue);
|
||||
}
|
||||
|
||||
public class MonInWorkerClient : IMonInWorkerClient {
|
||||
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IConfiguration _config;
|
||||
private readonly ILogger<MonInWorkerClient> _logger;
|
||||
|
||||
private readonly string _baseUrl;
|
||||
private readonly int _retryLimit = -1;
|
||||
private readonly int _backoffInSeconds = -1;
|
||||
|
||||
private readonly string _resource;
|
||||
|
||||
public MonInWorkerClient(IHttpClientFactory httpClientFactory,
|
||||
IConfiguration config,
|
||||
ILogger<MonInWorkerClient> logger) {
|
||||
_httpClientFactory = httpClientFactory;
|
||||
if (_httpClientFactory is null) throw new ArgumentNullException("IHttpClientFactory not injected");
|
||||
|
||||
_config = config;
|
||||
if (_config is null) throw new ArgumentNullException("IConfiguration not injected");
|
||||
|
||||
_logger = logger;
|
||||
if (_logger is null) throw new ArgumentNullException("ILogger not injected");
|
||||
|
||||
_baseUrl = _config["MonIn:workerUrl"];
|
||||
if (_baseUrl is null) throw new ArgumentNullException("MonIn:workerUrl not found in config");
|
||||
|
||||
Int32.TryParse(_config["MonIn:retries"], out _retryLimit);
|
||||
if (_retryLimit == -1) throw new ArgumentNullException("MonIn:retries not found in config");
|
||||
|
||||
Int32.TryParse(_config["MonIn:backoffInSeconds"], out _backoffInSeconds);
|
||||
if (_backoffInSeconds == -1) throw new ArgumentNullException("MonIn:backoffInSeconds not found in config");
|
||||
|
||||
_resource = _config["MonIn:resource"];
|
||||
if (_resource is null) throw new ArgumentNullException("MonIn:resource not found in config");
|
||||
}
|
||||
|
||||
public async void PostStatus(string statusName, StatusValue statusValue) {
|
||||
string url = _baseUrl + "status";
|
||||
|
||||
_logger.LogInformation("Attempting to send MonIn status request for resource {0} with name {1} and value {2} to url {3}",
|
||||
_resource,
|
||||
statusName,
|
||||
statusValue.ToString(),
|
||||
url);
|
||||
|
||||
try {
|
||||
bool success = false;
|
||||
int retries = _retryLimit;
|
||||
while (!success && retries > 0) {
|
||||
Task.Delay(TimeSpan.FromSeconds((_retryLimit - retries) * _backoffInSeconds)).Wait();
|
||||
|
||||
HttpClient httpClient = _httpClientFactory.CreateClient();
|
||||
|
||||
RawMonInStatusRequest statusRequest = new RawMonInStatusRequest() {
|
||||
resource = _resource,
|
||||
dateTime = DateTime.Now,
|
||||
statusName = statusName,
|
||||
statusValue = statusValue.ToString()
|
||||
};
|
||||
|
||||
StringContent statusRequestJson = new StringContent(JsonSerializer.Serialize(statusRequest),
|
||||
Encoding.UTF8,
|
||||
Application.Json);
|
||||
using HttpResponseMessage httpResponseMessage =
|
||||
await httpClient.PostAsync(url, statusRequestJson);
|
||||
|
||||
success = httpResponseMessage.IsSuccessStatusCode;
|
||||
retries--;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError("An exception occurred when attempting to send MonIn status request for resource {0} with name {1} and value {2} to url {3}. Exception: {4}",
|
||||
_resource,
|
||||
statusName,
|
||||
statusValue.ToString(),
|
||||
url,
|
||||
ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async void PostCount(string metricName, double metricValue) {
|
||||
string url = _baseUrl + "count";
|
||||
|
||||
_logger.LogInformation("Attempting to send MonIn count request for resource {0} with name {1} and value {2} to url {3}",
|
||||
_resource,
|
||||
metricName,
|
||||
metricValue,
|
||||
url);
|
||||
|
||||
try {
|
||||
bool success = false;
|
||||
int retries = _retryLimit;
|
||||
while (!success && retries > 0) {
|
||||
Task.Delay(TimeSpan.FromSeconds((_retryLimit - retries) * _backoffInSeconds)).Wait();
|
||||
|
||||
HttpClient httpClient = _httpClientFactory.CreateClient();
|
||||
|
||||
MonInMetricRequest metricRequest = new MonInMetricRequest() {
|
||||
resource = _resource,
|
||||
dateTime = DateTime.Now,
|
||||
metricName = metricName,
|
||||
metricValue = metricValue
|
||||
};
|
||||
|
||||
StringContent metricRequestJson = new StringContent(JsonSerializer.Serialize(metricRequest),
|
||||
Encoding.UTF8,
|
||||
Application.Json);
|
||||
using HttpResponseMessage httpResponseMessage =
|
||||
await httpClient.PostAsync(url, metricRequestJson);
|
||||
|
||||
success = httpResponseMessage.IsSuccessStatusCode;
|
||||
retries--;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError("An exception occurred when attempting to send MonIn count request for resource {0} with name {1} and value {2} to url {3}. Exception: {4}",
|
||||
_resource,
|
||||
metricName,
|
||||
metricValue,
|
||||
url,
|
||||
ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async void PostAverage(string metricName, double metricValue) {
|
||||
string url = _baseUrl + "average";
|
||||
|
||||
_logger.LogInformation("Attempting to send MonIn average request for resource {0} with name {1} and value {2} to url {3}",
|
||||
_resource,
|
||||
metricName,
|
||||
metricValue,
|
||||
url);
|
||||
|
||||
try {
|
||||
bool success = false;
|
||||
int retries = _retryLimit;
|
||||
while (!success && retries > 0) {
|
||||
Task.Delay(TimeSpan.FromSeconds((_retryLimit - retries) * _backoffInSeconds)).Wait();
|
||||
|
||||
HttpClient httpClient = _httpClientFactory.CreateClient();
|
||||
|
||||
MonInMetricRequest metricRequest = new MonInMetricRequest() {
|
||||
resource = _resource,
|
||||
dateTime = DateTime.Now,
|
||||
metricName = metricName,
|
||||
metricValue = metricValue
|
||||
};
|
||||
|
||||
StringContent metricRequestJson = new StringContent(JsonSerializer.Serialize(metricRequest),
|
||||
Encoding.UTF8,
|
||||
Application.Json);
|
||||
using HttpResponseMessage httpResponseMessage =
|
||||
await httpClient.PostAsync(url, metricRequestJson);
|
||||
|
||||
success = httpResponseMessage.IsSuccessStatusCode;
|
||||
retries--;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError("An exception occurred when attempting to send MonIn average request for resource {0} with name {1} and value {2} to url {3}. Exception: {4}",
|
||||
_resource,
|
||||
metricName,
|
||||
metricValue,
|
||||
url,
|
||||
ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user