|
|
|
|
@@ -13,24 +13,32 @@ public class App(IConfiguration configuration, IEmailGeneratorService emailGener
|
|
|
|
|
var contaboConnection = await vaultService.GetSecretAsync("database/contabo", "connection_string", "secret");
|
|
|
|
|
if (string.IsNullOrWhiteSpace(contaboConnection))
|
|
|
|
|
{
|
|
|
|
|
// Consider logging error or throwing as connection string must be valid
|
|
|
|
|
// Logging or throwing an exception is necessary here since connection string is mandatory
|
|
|
|
|
throw new InvalidOperationException("Database connection string cannot be null or empty.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var heartbeatSection = configuration.GetSection("HeartBeatClient");
|
|
|
|
|
if (!int.TryParse(heartbeatSection["HeartBeatInterval"], out int heartBeatInterval))
|
|
|
|
|
{
|
|
|
|
|
// You might want to provide a default or throw
|
|
|
|
|
heartBeatInterval = 0;
|
|
|
|
|
// Log or handle gracefully, here setting default to a safe positive value to avoid false alerts
|
|
|
|
|
heartBeatInterval = 5; // example default interval - adjust as necessary
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (heartBeatInterval <= 0)
|
|
|
|
|
{
|
|
|
|
|
// If interval is zero or less, skip heartbeat check or handle appropriately
|
|
|
|
|
// Could log warning here
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ResultModel? result = await GetLastUpdateDate(contaboConnection);
|
|
|
|
|
|
|
|
|
|
DateTime now = DateTime.Now;
|
|
|
|
|
DateTime now = DateTime.UtcNow;
|
|
|
|
|
DateTime lastUpdateDate = result?.LastUpdateDate.ToUniversalTime() ?? DateTime.MinValue;
|
|
|
|
|
|
|
|
|
|
double minutesDifference = (now - lastUpdateDate).TotalMinutes;
|
|
|
|
|
|
|
|
|
|
// Only generate alert if last update older than heartbeat interval and email has not already been sent
|
|
|
|
|
if (minutesDifference > heartBeatInterval && result?.EmailSent == false)
|
|
|
|
|
{
|
|
|
|
|
await GenerateAlert(lastUpdateDate);
|
|
|
|
|
@@ -43,7 +51,7 @@ public class App(IConfiguration configuration, IEmailGeneratorService emailGener
|
|
|
|
|
EmailModel emailModel = new EmailModel
|
|
|
|
|
{
|
|
|
|
|
Subject = $"HeartBeat: Serwer nie odpowiada",
|
|
|
|
|
Body = $"Serwer nie odpowiada od {lastUpdateDate}. Proszę o sprawdzenie stanu serwera."
|
|
|
|
|
Body = $"Serwer nie odpowiada od {lastUpdateDate:u}. Proszę o sprawdzenie stanu serwera."
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
emailModel.To.Add("piotr.kus@fakrosno.pl");
|
|
|
|
|
@@ -53,7 +61,8 @@ public class App(IConfiguration configuration, IEmailGeneratorService emailGener
|
|
|
|
|
|
|
|
|
|
private async Task<ResultModel?> GetLastUpdateDate(string connectionString)
|
|
|
|
|
{
|
|
|
|
|
const string query = "SELECT \"ID\", \"LastUpdateDate\", \"EmailSent\" FROM \"OrdersManagement\"." +
|
|
|
|
|
const string query = "SELECT \"ID\", \"LastUpdateDate\", \"EmailSent\" " +
|
|
|
|
|
"FROM \"OrdersManagement\"." +
|
|
|
|
|
"\"HeartBeat\" ORDER BY \"LastUpdateDate\" DESC LIMIT 1;";
|
|
|
|
|
|
|
|
|
|
await using var connection = new NpgsqlConnection(connectionString);
|
|
|
|
|
|