From 68b74284c7e9e60f64eff2c4599dac793b6b8fea Mon Sep 17 00:00:00 2001 From: Manuel Date: Mon, 25 Aug 2025 21:16:11 +0200 Subject: [PATCH] healthcheks --- back/.program_data/app.db-wal | Bin 41232 -> 78312 bytes back/.program_data/imgs/systemkey.lock | 4 ++-- back/ServicesExtensions/Options.cs | 3 +++ back/appsettings.Development.json | 8 +++++++ .../options/HealthChecksConfigs.cs | 8 +++++++ back/healthchecks/sqlite.cs | 22 ++++++++++-------- .../bussines/PhotoService/IPhotoService.cs | 1 - .../bussines/UserService/IUserService.cs | 1 - 8 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 back/healthchecks/options/HealthChecksConfigs.cs diff --git a/back/.program_data/app.db-wal b/back/.program_data/app.db-wal index 40def1b23866b8526ad581da4e7d80d0e2d3c3aa..9157b2c1dc667b412aaecad52baee15b02d19c49 100644 GIT binary patch delta 1267 zcmZ|N&5PW090u@31@|H?2v(us-lPp>l3$X^B+F1Ud4HRiOkQRJ9+KHfCX?4mvXjXm ztoRRDS1kx$#DgcHDD@))q3W8KED2TP|Wta8vJ%2wu&vWDR$L{{{#I1|( zKD)EC^W4MR|KRK2|GaNnzjs}93I~lF7#W5sw46Nn3Q-NHK zJcyGE+-5-PsULN-9^=PrPMS<;)r<;`S;y_+Y%Gb0#mZZu9K(H@g~XUWl)3=F z26wIj7~p$Io5E2FCHEj0K8x9XVgDW%{c`ckhnnLHN1}~NrlFe5mP|yhVd6E$>LNZt zNsNbwwP&&3VnmnAT^JW+kmSaKmDB7DG@7 zI1lsLlRtm;%I&|Nf9)ii;f$RNz<2DKHjWnQbQ~|tq?`=3b&nuyeTI)zSW3hk$aFWM zHIt6k`ZBC1^Zu@W{-#tZ^4I;9~f7@;uqJk9R+Pj%}{5B|7* zqS<)0ulPi>9jr~I$19T#LKuWxOcs!iE=?wDk>s+itEn;ShFT3bjMs}BO^y{d_uFog z;4=a&S&z?6tWIYmZ6@`7TRWox2nCRHH9x(0<-6m{d;2Gvsa*s}Lt3i>2t_ut+pM~= z$C<$~p9NF2O|;CUzxbwSRGp>GR@=ndv#kP1m~ zRzp%aajxd=mzb+JzaZ}Zr!iSF5$GZYIiJaDjCPPl*Y!NEMnk$~^?^SK=~_mX72HL~ zhC`I=V5KpelM?hzSbC{%1G4S3h~@$J?(yc<+&B z!Pk?}3v@Lp8>Zc&=Yb3?mv=UB8;c-ksgHQGuSM$!*+|_5u&Wm2<^!>kT~5iihbxCR zGkmUa)0UD}*a~6A-a%pq*r{wf6b9b-^~R-L^ZuuQKlAPN_fLXI#R#CWR?fjvvc|UF z+w^V8253`7Tt|j1I?u$OWjpX;-EELWi~LIU4*WqA6QMmSiijU_Yzzyu5q2ustQgEN zW3xQ_8l!Od+*-c5Ox=0wx0`Q1(qv^3m{3gzEWI=xdt5Vp+rw3Nsi1S&0I delta 9 QcmaFym}SBtrVRoM0U2lng#Z8m diff --git a/back/.program_data/imgs/systemkey.lock b/back/.program_data/imgs/systemkey.lock index 718fc98..92b2ff1 100644 --- a/back/.program_data/imgs/systemkey.lock +++ b/back/.program_data/imgs/systemkey.lock @@ -1,5 +1,5 @@ { "email": "sys@t.em", - "key": "c1d6bd4e-ac32-4859-b2f5-fcda1c190934", - "password": "Tx,bA%8KPn_dç8v[" + "key": "aa0e0979-99db-42e7-8b60-91c2d055b9d0", + "password": "+z1L[oYUupZ>L{4a" } \ No newline at end of file diff --git a/back/ServicesExtensions/Options.cs b/back/ServicesExtensions/Options.cs index 7ee253a..686ebd1 100644 --- a/back/ServicesExtensions/Options.cs +++ b/back/ServicesExtensions/Options.cs @@ -1,3 +1,4 @@ +using back.healthchecks.Options; using back.Options; namespace back.ServicesExtensions; @@ -14,6 +15,8 @@ public static partial class ServicesExtensions services.Configure(DatabaseConfig.BlobStorage, config.GetSection(DatabaseConfig.BlobStorage)); services.Configure(config.GetSection(nameof(MailServerOptions))); + services.Configure(HealthChecksConfigs.Sqlite, config.GetSection(HealthChecksConfigs.Sqlite)); + services.PostConfigure(databases => { if (!string.IsNullOrEmpty(databases.BaseDirectory) && !Directory.Exists(databases.BaseDirectory)) diff --git a/back/appsettings.Development.json b/back/appsettings.Development.json index 0868ace..46009f1 100644 --- a/back/appsettings.Development.json +++ b/back/appsettings.Development.json @@ -17,5 +17,13 @@ "Usuario": "", "Password": "", "EnableSsl": true + }, + "HealthChecksConfigs": { + "Sqlite": { + "RetryAttempts" : 2, + "Timeout" : "00:05:00", + "RetryDelay" : "00:00:10", + "Severity": "Info" + } } } diff --git a/back/healthchecks/options/HealthChecksConfigs.cs b/back/healthchecks/options/HealthChecksConfigs.cs new file mode 100644 index 0000000..2c432ac --- /dev/null +++ b/back/healthchecks/options/HealthChecksConfigs.cs @@ -0,0 +1,8 @@ +using HealthChecksConfigsBase = healthchecks.Options.HealthChecksConfigs; + +namespace back.healthchecks.Options; + +public partial class HealthChecksConfigs : HealthChecksConfigsBase +{ + public const string Sqlite = "Sqlite"; +} diff --git a/back/healthchecks/sqlite.cs b/back/healthchecks/sqlite.cs index e7727a9..5f22ce3 100644 --- a/back/healthchecks/sqlite.cs +++ b/back/healthchecks/sqlite.cs @@ -1,27 +1,29 @@ using back.Options; using healthchecks; +using healthchecks.Abstracts; +using back.healthchecks.Options; using Microsoft.Extensions.Options; namespace back.healthchecks; -[HealthCheckExecutionOptions(retryAttempts: 2, timeout: "00:00:05", retryDelay: "00:00:01", severity: HealthCheckSeverity.Critical)] -public class SqliteHealthCheck : IHealthCheck +public class SqliteHealthCheck(IOptionsMonitor databaseConfig, IOptionsMonitor healthchecksConfig) : IHealthCheck { - private readonly DatabaseConfig config; - public SqliteHealthCheck(IOptionsMonitor optionsSnapshot) - { - config = optionsSnapshot.Get(DatabaseConfig.DataStorage); - } + private readonly DatabaseConfig databaseConfig = databaseConfig.Get(DatabaseConfig.DataStorage); + private readonly HealthChecksConfigs hcConfig = healthchecksConfig.Get(HealthChecksConfigs.Sqlite); + + public string Description => "Conecta con la base de datos SQLite y trata de hacer una consulta sobre la tabla Users."; + public int? RetryAttempts => hcConfig.RetryAttempts ?? 2; + public TimeSpan? Timeout => hcConfig.Timeout ?? TimeSpan.FromSeconds(5); + public TimeSpan? RetryDelay => hcConfig.RetryDelay ?? TimeSpan.FromSeconds(1); + public HealthCheckSeverity? Severity => hcConfig.Severity ?? HealthCheckSeverity.Critical; public Task CheckAsync(CancellationToken cancellationToken = default) { - // check if can connect to sqlite database - // then run a query to Users table to see if User.SystemUser exists var isHealthy = false; var details = string.Empty; try { - using var connection = new Microsoft.Data.Sqlite.SqliteConnection(config.ConnectionString); + using var connection = new Microsoft.Data.Sqlite.SqliteConnection(databaseConfig.ConnectionString); connection.Open(); using var command = connection.CreateCommand(); command.CommandText = $"SELECT COUNT(1) FROM Users WHERE Id = '{DataModels.User.SystemUserId}';"; diff --git a/back/services/bussines/PhotoService/IPhotoService.cs b/back/services/bussines/PhotoService/IPhotoService.cs index 80c24f9..8d045bb 100644 --- a/back/services/bussines/PhotoService/IPhotoService.cs +++ b/back/services/bussines/PhotoService/IPhotoService.cs @@ -1,6 +1,5 @@ using back.DataModels; using back.DTO; -using DependencyInjector.Abstractions.ClassTypes; using DependencyInjector.Lifetimes; namespace back.services.bussines.PhotoService; diff --git a/back/services/bussines/UserService/IUserService.cs b/back/services/bussines/UserService/IUserService.cs index 76dc8e3..ec9bc48 100644 --- a/back/services/bussines/UserService/IUserService.cs +++ b/back/services/bussines/UserService/IUserService.cs @@ -1,5 +1,4 @@ using back.DataModels; -using DependencyInjector.Abstractions.ClassTypes; using DependencyInjector.Lifetimes; namespace back.services.bussines.UserService;