healthcheks
This commit is contained in:
Binary file not shown.
@@ -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"
|
||||
}
|
@@ -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>(DatabaseConfig.BlobStorage, config.GetSection(DatabaseConfig.BlobStorage));
|
||||
services.Configure<MailServerOptions>(config.GetSection(nameof(MailServerOptions)));
|
||||
|
||||
services.Configure<HealthChecksConfigs>(HealthChecksConfigs.Sqlite, config.GetSection(HealthChecksConfigs.Sqlite));
|
||||
|
||||
services.PostConfigure<Databases>(databases =>
|
||||
{
|
||||
if (!string.IsNullOrEmpty(databases.BaseDirectory) && !Directory.Exists(databases.BaseDirectory))
|
||||
|
@@ -17,5 +17,13 @@
|
||||
"Usuario": "",
|
||||
"Password": "",
|
||||
"EnableSsl": true
|
||||
},
|
||||
"HealthChecksConfigs": {
|
||||
"Sqlite": {
|
||||
"RetryAttempts" : 2,
|
||||
"Timeout" : "00:05:00",
|
||||
"RetryDelay" : "00:00:10",
|
||||
"Severity": "Info"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
8
back/healthchecks/options/HealthChecksConfigs.cs
Normal file
8
back/healthchecks/options/HealthChecksConfigs.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using HealthChecksConfigsBase = healthchecks.Options.HealthChecksConfigs;
|
||||
|
||||
namespace back.healthchecks.Options;
|
||||
|
||||
public partial class HealthChecksConfigs : HealthChecksConfigsBase
|
||||
{
|
||||
public const string Sqlite = "Sqlite";
|
||||
}
|
@@ -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> databaseConfig, IOptionsMonitor<HealthChecksConfigs> healthchecksConfig) : IHealthCheck
|
||||
{
|
||||
private readonly DatabaseConfig config;
|
||||
public SqliteHealthCheck(IOptionsMonitor<DatabaseConfig> 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<HealthCheckResult> 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}';";
|
||||
|
@@ -1,6 +1,5 @@
|
||||
using back.DataModels;
|
||||
using back.DTO;
|
||||
using DependencyInjector.Abstractions.ClassTypes;
|
||||
using DependencyInjector.Lifetimes;
|
||||
|
||||
namespace back.services.bussines.PhotoService;
|
||||
|
@@ -1,5 +1,4 @@
|
||||
using back.DataModels;
|
||||
using DependencyInjector.Abstractions.ClassTypes;
|
||||
using DependencyInjector.Lifetimes;
|
||||
|
||||
namespace back.services.bussines.UserService;
|
||||
|
Reference in New Issue
Block a user