alteração da função de upload e insert do location status.
This commit is contained in:
parent
91be1c6a3d
commit
b29ae2e3d3
@ -235,21 +235,22 @@ namespace Webhook_4docs
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
int test = await UpdateErrorIdStatusAsync(CaminhoDB, JsonBody.GetProperty("locationID").GetInt64(), errorID, JsonBody.GetProperty("systemText").ToString());
|
int test = await UpdateErrorIdStatusAsync(CaminhoDB, JsonBody.GetProperty("locationID").GetInt64(), errorID, JsonBody.GetProperty("systemText").ToString(), logger);
|
||||||
|
|
||||||
if (test == 0)
|
if (test == 0)
|
||||||
{
|
{
|
||||||
InsertErrorIdStatus(CaminhoDB, errorID, requestBody);
|
await InsertErrorIdStatusAsync(CaminhoDB, errorID, requestBody, logger);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
app.Run();
|
app.Run();
|
||||||
}
|
}
|
||||||
public static async void InsertErrorIdStatus(string CaminhoDB, double errorID, string requestBody)
|
public static async Task InsertErrorIdStatusAsync(string CaminhoDB, double errorID, string requestBody, ILogger logger)
|
||||||
{
|
{
|
||||||
var JsonBody = JsonDocument.Parse(requestBody).RootElement;
|
var JsonBody = JsonDocument.Parse(requestBody).RootElement;
|
||||||
|
|
||||||
|
// Extração dos valores do JSON
|
||||||
double locationID = JsonBody.GetProperty("locationID").GetInt64();
|
double locationID = JsonBody.GetProperty("locationID").GetInt64();
|
||||||
string accountID = JsonBody.GetProperty("accountID").ToString();
|
string accountID = JsonBody.GetProperty("accountID").ToString();
|
||||||
string deliveryTimeStamp = JsonBody.GetProperty("deliveryTimestamp").ToString();
|
string deliveryTimeStamp = JsonBody.GetProperty("deliveryTimestamp").ToString();
|
||||||
@ -265,43 +266,10 @@ namespace Webhook_4docs
|
|||||||
string lastDeactivated = JsonBody.GetProperty("lastDeactivated").ToString();
|
string lastDeactivated = JsonBody.GetProperty("lastDeactivated").ToString();
|
||||||
string lastDeactivatedBy = JsonBody.GetProperty("lastDeactivatedBy").ToString();
|
string lastDeactivatedBy = JsonBody.GetProperty("lastDeactivatedBy").ToString();
|
||||||
|
|
||||||
int test = 0;
|
int maxRetries = 3;
|
||||||
|
int attempt = 0;
|
||||||
|
|
||||||
bool completed = false;
|
while (attempt < maxRetries)
|
||||||
|
|
||||||
while (!completed)
|
|
||||||
{
|
|
||||||
var connLease = await connRateLimiter.AcquireAsync();
|
|
||||||
|
|
||||||
if (connLease.IsAcquired)
|
|
||||||
{
|
|
||||||
using (OleDbConnection conn = new(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + CaminhoDB + ";Jet OLEDB:Database Password=gds21"))
|
|
||||||
{
|
|
||||||
if (conn != null)
|
|
||||||
{
|
|
||||||
if (conn.State == System.Data.ConnectionState.Closed)
|
|
||||||
{
|
|
||||||
await conn.OpenAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
using OleDbCommand cmd = new($"INSERT INTO AgVirtual4DocsErros (locationID, accountID, errorID, deliveryTimeStamp, provider, accessPoint, slaStatus, healthy, blame, lastSuccess, active, blacklistStatus, lastActivated, lastDeactivated, lastDeactivatedBy) VALUES ({locationID}, {accountID}, {errorID}, \'{deliveryTimeStamp}\', \'{provider}\', \'{accessPoint}\', \'{slaStatus}\', \'{healthy}\', \'{blame}\', \'{lastSuccess}\', \'{active}\', \'{blacklistStatus}\', \'{lastActivated}\', \'{lastDeactivated}\', \'{lastDeactivatedBy}\')", conn);
|
|
||||||
//cmd.Parameters.AddWithValue("@location_id", location_id);
|
|
||||||
//cmd.Parameters.AddWithValue("@errorID", errorID);
|
|
||||||
test = await cmd.ExecuteNonQueryAsync();
|
|
||||||
}
|
|
||||||
completed = true;
|
|
||||||
connLease.Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public static async Task<int> UpdateErrorIdStatusAsync(string CaminhoDB, double location_id, double errorID, string systemText)
|
|
||||||
{
|
|
||||||
int test = 0;
|
|
||||||
|
|
||||||
bool completed = false;
|
|
||||||
|
|
||||||
while (!completed)
|
|
||||||
{
|
{
|
||||||
var connLease = await connRateLimiter.AcquireAsync();
|
var connLease = await connRateLimiter.AcquireAsync();
|
||||||
|
|
||||||
@ -309,30 +277,132 @@ namespace Webhook_4docs
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (OleDbConnection conn = new(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + CaminhoDB + ";Jet OLEDB:Database Password=gds21"))
|
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + CaminhoDB + ";Jet OLEDB:Database Password=gds21"))
|
||||||
{
|
|
||||||
if (conn != null)
|
|
||||||
{
|
|
||||||
if (conn.State == System.Data.ConnectionState.Closed)
|
|
||||||
{
|
{
|
||||||
await conn.OpenAsync();
|
await conn.OpenAsync();
|
||||||
}
|
|
||||||
|
|
||||||
using (OleDbCommand cmd = new($"UPDATE AgVirtual4Docs SET AgVirtual4Docs.errorID = {errorID}, AgVirtual4Docs.status = \"{systemText}\"\r\nWHERE (((AgVirtual4Docs.location_id)={location_id}));\r\n", conn))
|
using (OleDbCommand cmd = new OleDbCommand(
|
||||||
|
@"INSERT INTO AgVirtual4DocsErros (
|
||||||
|
locationID, accountID, errorID, deliveryTimeStamp, provider, accessPoint, slaStatus,
|
||||||
|
healthy, blame, lastSuccess, active, blacklistStatus, lastActivated, lastDeactivated, lastDeactivatedBy
|
||||||
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", conn))
|
||||||
{
|
{
|
||||||
//cmd.Parameters.AddWithValue("@errorID", errorID);
|
// Adiciona parâmetros de forma segura
|
||||||
//cmd.Parameters.AddWithValue("@location_id", location_id);
|
cmd.Parameters.AddWithValue("@locationID", locationID);
|
||||||
test = await cmd.ExecuteNonQueryAsync();
|
cmd.Parameters.AddWithValue("@accountID", accountID);
|
||||||
|
cmd.Parameters.AddWithValue("@errorID", errorID);
|
||||||
|
cmd.Parameters.AddWithValue("@deliveryTimeStamp", deliveryTimeStamp);
|
||||||
|
cmd.Parameters.AddWithValue("@provider", provider);
|
||||||
|
cmd.Parameters.AddWithValue("@accessPoint", accessPoint);
|
||||||
|
cmd.Parameters.AddWithValue("@slaStatus", slaStatus);
|
||||||
|
cmd.Parameters.AddWithValue("@healthy", healthy);
|
||||||
|
cmd.Parameters.AddWithValue("@blame", blame);
|
||||||
|
cmd.Parameters.AddWithValue("@lastSuccess", lastSuccess);
|
||||||
|
cmd.Parameters.AddWithValue("@active", active);
|
||||||
|
cmd.Parameters.AddWithValue("@blacklistStatus", blacklistStatus);
|
||||||
|
cmd.Parameters.AddWithValue("@lastActivated", lastActivated);
|
||||||
|
cmd.Parameters.AddWithValue("@lastDeactivated", lastDeactivated);
|
||||||
|
cmd.Parameters.AddWithValue("@lastDeactivatedBy", lastDeactivatedBy);
|
||||||
|
|
||||||
|
// Executa o comando
|
||||||
|
await cmd.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break; // Sai do loop em caso de sucesso
|
||||||
}
|
}
|
||||||
|
catch (OleDbException ex)
|
||||||
|
{
|
||||||
|
// Registra o erro
|
||||||
|
logger.LogInformation($"Erro no OleDb insert: {ex.Message} (Tentativa {attempt + 1} de {maxRetries})");
|
||||||
|
|
||||||
|
if (attempt < maxRetries - 1)
|
||||||
|
{
|
||||||
|
// Aguarda antes de tentar novamente
|
||||||
|
await Task.Delay(1000 * (int)Math.Pow(2, attempt)); // Retry com Backoff Exponencial
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Propaga a exceção na última tentativa
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
completed = true;
|
|
||||||
connLease.Dispose();
|
connLease.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
attempt++; // Incrementa a tentativa após adquirir o lease, mesmo que falhe
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Aguarda um curto período antes de tentar novamente se não conseguiu adquirir o lease
|
||||||
|
await Task.Delay(200);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async Task<int> UpdateErrorIdStatusAsync(string CaminhoDB, double location_id, double errorID, string systemText, ILogger logger)
|
||||||
|
{
|
||||||
|
int test = 0;
|
||||||
|
int maxRetries = 3;
|
||||||
|
int attempt = 0;
|
||||||
|
|
||||||
|
while (attempt < maxRetries)
|
||||||
|
{
|
||||||
|
var connLease = await connRateLimiter.AcquireAsync();
|
||||||
|
|
||||||
|
if (connLease.IsAcquired)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + CaminhoDB + ";Jet OLEDB:Database Password=gds21"))
|
||||||
|
{
|
||||||
|
await conn.OpenAsync();
|
||||||
|
|
||||||
|
using (OleDbCommand cmd = new OleDbCommand(
|
||||||
|
@"UPDATE AgVirtual4Docs
|
||||||
|
SET errorID = ?, status = ?
|
||||||
|
WHERE location_id = ?", conn))
|
||||||
|
{
|
||||||
|
// Adiciona parâmetros de forma segura
|
||||||
|
cmd.Parameters.AddWithValue("@errorID", errorID);
|
||||||
|
cmd.Parameters.AddWithValue("@status", systemText);
|
||||||
|
cmd.Parameters.AddWithValue("@location_id", location_id);
|
||||||
|
|
||||||
|
// Executa o comando e captura o resultado
|
||||||
|
test = await cmd.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break; // Sai do loop em caso de sucesso
|
||||||
|
}
|
||||||
|
catch (OleDbException ex)
|
||||||
|
{
|
||||||
|
// Registra o erro
|
||||||
|
logger.LogInformation($"Erro no OleDb update: {ex.Message} (Tentativa {attempt + 1} de {maxRetries})");
|
||||||
|
|
||||||
|
if (attempt < maxRetries - 1)
|
||||||
|
{
|
||||||
|
// Aguarda antes de tentar novamente
|
||||||
|
await Task.Delay(1000 * (int)Math.Pow(2, attempt)); // Retry com Backoff Exponencial
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Propaga a exceção na última tentativa
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
connLease.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
attempt++; // Incrementa a tentativa após adquirir o lease, mesmo que falhe
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Aguarda um curto período antes de tentar novamente se não conseguiu adquirir o lease
|
||||||
|
await Task.Delay(200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user