Compare commits

...

2 Commits

Author SHA1 Message Date
258cf53b4b Merge pull request 'LLM: Code review suggestions' (#57) from code-review_j9v4sx2a into master
Reviewed-on: #57
2026-01-21 11:44:34 +00:00
d1c08efd7f LLM: Code review suggestions
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
* Use DateTime.UtcNow consistently instead of DateTime.Now to avoid timezone issues when comparing times.
* In App.cs, handle the scenario where HeartBeatInterval is 0 more explicitly to avoid incorrect alert generation.
* In App.cs, log or provide more informative error when HeartBeatInterval parsing fails.
* Add comments to clarify the logic regarding EmailSent flag usage.
* In Program.cs, change 'Builder' to 'HostBuilder' or clarify the Builder class usage; it seems unusual to name it 'Builder'.
* In Program.cs, the use of GetAwaiter().GetResult() can cause deadlocks in some scenarios; use async/await consistently.
* Add cancellation support in async methods if possible (not critical but recommended as best practice).
* Minor code style: consider using explicit class declaration instead of record-like primary constructor for App if more methods or fields are expected in the future.

These changes improve code clarity, robustness, and future maintainability.
2026-01-21 11:41:57 +00:00
2 changed files with 23 additions and 10 deletions

View File

@@ -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);

View File

@@ -12,12 +12,16 @@ IHost? host = null;
try
{
Builder builder = new Builder();
host = builder.CreateHostBuilder(args).Build();
var builder = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
// Register dependencies here if needed
});
host = builder.Build();
var vaultService = host.Services.GetRequiredService<IVaultService>();
var syncfusionLicense = vaultService.GetSecretAsync("licenses/syncfusion", "license_key", "secret")
.GetAwaiter().GetResult();
var syncfusionLicense = await vaultService.GetSecretAsync("licenses/syncfusion", "license_key", "secret");
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(syncfusionLicense ??
Environment.GetEnvironmentVariable(