Created PendingOOOStatusWorker
This commit is contained in:
@ -1,15 +1,21 @@
|
||||
using Dapper;
|
||||
using Dapper.Contrib.Extensions;
|
||||
|
||||
using FabApprovalWorkerService.Models;
|
||||
|
||||
using System.Data;
|
||||
|
||||
namespace FabApprovalWorkerService.Services;
|
||||
|
||||
public interface IDalService {
|
||||
Task<IEnumerable<T>> QueryAsync<T>(string sql);
|
||||
Task<bool> UpdateAsync<T>(ICollection<T> collection);
|
||||
Task<int> ExecuteAsync(string sql);
|
||||
}
|
||||
|
||||
public class DalService : IDalService {
|
||||
private static readonly int RETRIES = 3;
|
||||
private static readonly int BACKOFF_SECONDS_INTERVAL = 30;
|
||||
|
||||
private readonly IDbConnection _dbConnection;
|
||||
private readonly ILogger<DalService> _logger;
|
||||
|
||||
@ -22,35 +28,73 @@ public class DalService : IDalService {
|
||||
|
||||
public async Task<IEnumerable<T>> QueryAsync<T>(string sql) {
|
||||
if (sql is null) throw new ArgumentNullException("sql cannot be null");
|
||||
try {
|
||||
_logger.LogInformation("Attempting to perform query with {sql}", sql);
|
||||
|
||||
_dbConnection.Open();
|
||||
return await _dbConnection.QueryAsync<T>(sql);
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError("An exception occurred while attempting to perform a query. Exception: {ex}",
|
||||
ex.Message);
|
||||
int remainingRetries = RETRIES;
|
||||
bool queryWasSuccessful = false;
|
||||
Exception exception = null;
|
||||
IEnumerable<T> result = new List<T>();
|
||||
while (!queryWasSuccessful && remainingRetries > 0) {
|
||||
int backoffSeconds = (RETRIES - remainingRetries--) * BACKOFF_SECONDS_INTERVAL;
|
||||
Task.Delay(backoffSeconds * 1000).Wait();
|
||||
|
||||
throw;
|
||||
} finally {
|
||||
_dbConnection.Close();
|
||||
try {
|
||||
_logger.LogInformation("Attempting to perform query with {sql}. Remaining retries: {remainingRetries}",
|
||||
sql,
|
||||
remainingRetries);
|
||||
|
||||
using (_dbConnection) {
|
||||
result = await _dbConnection.QueryAsync<T>(sql);
|
||||
}
|
||||
|
||||
queryWasSuccessful = true;
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError("An exception occurred while attempting to perform a query. Exception: {ex}",
|
||||
ex.Message);
|
||||
|
||||
exception = ex;
|
||||
}
|
||||
}
|
||||
|
||||
if (!queryWasSuccessful && exception is not null) {
|
||||
throw exception;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync<T>(ICollection<T> collection) {
|
||||
if (collection is null) throw new ArgumentNullException("collection cannot be null");
|
||||
try {
|
||||
_logger.LogInformation("Attempting to perform a bulk update");
|
||||
public async Task<int> ExecuteAsync(string sql) {
|
||||
if (sql is null) throw new ArgumentNullException("sql cannot be null");
|
||||
|
||||
_dbConnection.Open();
|
||||
return await _dbConnection.UpdateAsync<ICollection<T>>(collection);
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError("An exception occurred while attempting to perform a bulk update. Exception: {ex}",
|
||||
ex.Message);
|
||||
int remainingRetries = RETRIES;
|
||||
bool queryWasSuccessful = false;
|
||||
Exception exception = null;
|
||||
int rowsAffected = 0;
|
||||
while (!queryWasSuccessful && remainingRetries > 0) {
|
||||
int backoffSeconds = (RETRIES - remainingRetries--) * BACKOFF_SECONDS_INTERVAL;
|
||||
Task.Delay(backoffSeconds * 1000).Wait();
|
||||
|
||||
throw;
|
||||
} finally {
|
||||
_dbConnection.Close();
|
||||
try {
|
||||
_logger.LogInformation("Attempting to execute {sql}. Remaining retries: {remainingRetries}",
|
||||
sql,
|
||||
remainingRetries);
|
||||
|
||||
using (_dbConnection) {
|
||||
rowsAffected = await _dbConnection.ExecuteAsync(sql);
|
||||
}
|
||||
|
||||
queryWasSuccessful = true;
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError("An exception occurred while attempting to execute a query. Exception: {ex}",
|
||||
ex.Message);
|
||||
|
||||
exception = ex;
|
||||
}
|
||||
}
|
||||
|
||||
if (!queryWasSuccessful && exception is not null) {
|
||||
throw exception;
|
||||
}
|
||||
|
||||
return rowsAffected;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user