Simplificação de lógica e remoção de tratamento de erros
- Ajustada a obtenção de `documentType` para evitar null reference. - Removida a variável `test` e simplificado o fluxo de controle. - Eliminado o método `InsertErrorIdStatusAsync` e sua lógica. - Mantido o método `UpdateErrorIdStatusAsync` com chamada direta. - Adicionado comentário sobre futura implementação de faturas via API. - Melhorada a clareza do código, mas com impacto no tratamento de erros.
This commit is contained in:
parent
03d40713af
commit
a870debbc3
@ -118,7 +118,7 @@ namespace Webhook_4docs
|
|||||||
|
|
||||||
JsonElement DadosJson = JsonDocument.Parse(root.ToString()).RootElement;
|
JsonElement DadosJson = JsonDocument.Parse(root.ToString()).RootElement;
|
||||||
|
|
||||||
string tipoDocumento = root.Get("documentType").ToString().ToLower();
|
string tipoDocumento = root.Get("documentType").ToString() ?? string.Empty.ToLower();
|
||||||
|
|
||||||
if (tipoDocumento != string.Empty && tipoDocumento != "nota_fiscal")
|
if (tipoDocumento != string.Empty && tipoDocumento != "nota_fiscal")
|
||||||
{
|
{
|
||||||
@ -248,14 +248,11 @@ namespace Webhook_4docs
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
int test = await UpdateErrorIdStatusAsync(CaminhoDB, JsonBody.GetProperty("locationID").GetInt64(), errorID, (JsonBody.GetProperty("systemText").ToString() == "" ? JsonBody.GetProperty("websiteText").ToString() : JsonBody.GetProperty("systemText").ToString()), logger);
|
await UpdateErrorIdStatusAsync(CaminhoDB, JsonBody.GetProperty("locationID").GetInt64(), errorID, (JsonBody.GetProperty("systemText").ToString() == "" ? JsonBody.GetProperty("websiteText").ToString() : JsonBody.GetProperty("systemText").ToString()), logger);
|
||||||
|
|
||||||
if (test == 0)
|
|
||||||
{
|
|
||||||
await InsertErrorIdStatusAsync(CaminhoDB, errorID, requestBody, logger);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
//implementação futura para receber as faturas enviadas via api de forma assíncrona - Upload4Docs (substituir parte do agendador de tarefas)
|
||||||
endpoints.MapPut("/api", async context =>
|
endpoints.MapPut("/api", async context =>
|
||||||
{
|
{
|
||||||
var a = 10;
|
var a = 10;
|
||||||
@ -263,101 +260,6 @@ namespace Webhook_4docs
|
|||||||
});
|
});
|
||||||
app.Run();
|
app.Run();
|
||||||
}
|
}
|
||||||
public static async Task InsertErrorIdStatusAsync(string CaminhoDB, double errorID, string requestBody, ILogger logger)
|
|
||||||
{
|
|
||||||
var JsonBody = JsonDocument.Parse(requestBody).RootElement;
|
|
||||||
|
|
||||||
// Extração dos valores do JSON
|
|
||||||
double locationID = JsonBody.GetProperty("locationID").GetInt64();
|
|
||||||
string accountID = JsonBody.GetProperty("accountID").ToString();
|
|
||||||
string deliveryTimeStamp = JsonBody.GetProperty("deliveryTimestamp").ToString();
|
|
||||||
string provider = JsonBody.GetProperty("provider").ToString();
|
|
||||||
string accessPoint = JsonBody.GetProperty("accessPoint").ToString();
|
|
||||||
string slaStatus = JsonBody.GetProperty("slaStatus").ToString();
|
|
||||||
string healthy = JsonBody.GetProperty("healthy").ToString();
|
|
||||||
string blame = JsonBody.GetProperty("blame").ToString();
|
|
||||||
string lastSuccess = JsonBody.GetProperty("lastSuccess").ToString();
|
|
||||||
string active = JsonBody.GetProperty("active").ToString();
|
|
||||||
string blacklistStatus = JsonBody.GetProperty("blacklistStatus").ToString();
|
|
||||||
string lastActivated = JsonBody.GetProperty("lastActivated").ToString();
|
|
||||||
string lastDeactivated = JsonBody.GetProperty("lastDeactivated").ToString();
|
|
||||||
string lastDeactivatedBy = JsonBody.GetProperty("lastDeactivatedBy").ToString();
|
|
||||||
|
|
||||||
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(
|
|
||||||
@"INSERT INTO AgVirtual4DocsErros (
|
|
||||||
locationID, accountID, errorID, deliveryTimeStamp, provider, accessPoint, slaStatus,
|
|
||||||
healthy, blame, lastSuccess, active, blacklistStatus, lastActivated, lastDeactivated, lastDeactivatedBy
|
|
||||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", conn))
|
|
||||||
{
|
|
||||||
// Adiciona parâmetros de forma segura
|
|
||||||
cmd.Parameters.AddWithValue("@locationID", locationID);
|
|
||||||
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
|
|
||||||
{
|
|
||||||
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)
|
public static async Task<int> UpdateErrorIdStatusAsync(string CaminhoDB, double location_id, double errorID, string systemText, ILogger logger)
|
||||||
{
|
{
|
||||||
int test = 0;
|
int test = 0;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user