healthcheks
This commit is contained in:
Binary file not shown.
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"email": "sys@t.em",
|
"email": "sys@t.em",
|
||||||
"key": "c1d6bd4e-ac32-4859-b2f5-fcda1c190934",
|
"key": "aa0e0979-99db-42e7-8b60-91c2d055b9d0",
|
||||||
"password": "Tx,bA%8KPn_dç8v["
|
"password": "+z1L[oYUupZ>L{4a"
|
||||||
}
|
}
|
@@ -1,3 +1,4 @@
|
|||||||
|
using back.healthchecks.Options;
|
||||||
using back.Options;
|
using back.Options;
|
||||||
|
|
||||||
namespace back.ServicesExtensions;
|
namespace back.ServicesExtensions;
|
||||||
@@ -14,6 +15,8 @@ public static partial class ServicesExtensions
|
|||||||
services.Configure<DatabaseConfig>(DatabaseConfig.BlobStorage, config.GetSection(DatabaseConfig.BlobStorage));
|
services.Configure<DatabaseConfig>(DatabaseConfig.BlobStorage, config.GetSection(DatabaseConfig.BlobStorage));
|
||||||
services.Configure<MailServerOptions>(config.GetSection(nameof(MailServerOptions)));
|
services.Configure<MailServerOptions>(config.GetSection(nameof(MailServerOptions)));
|
||||||
|
|
||||||
|
services.Configure<HealthChecksConfigs>(HealthChecksConfigs.Sqlite, config.GetSection(HealthChecksConfigs.Sqlite));
|
||||||
|
|
||||||
services.PostConfigure<Databases>(databases =>
|
services.PostConfigure<Databases>(databases =>
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(databases.BaseDirectory) && !Directory.Exists(databases.BaseDirectory))
|
if (!string.IsNullOrEmpty(databases.BaseDirectory) && !Directory.Exists(databases.BaseDirectory))
|
||||||
|
@@ -17,5 +17,13 @@
|
|||||||
"Usuario": "",
|
"Usuario": "",
|
||||||
"Password": "",
|
"Password": "",
|
||||||
"EnableSsl": true
|
"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 back.Options;
|
||||||
using healthchecks;
|
using healthchecks;
|
||||||
|
using healthchecks.Abstracts;
|
||||||
|
using back.healthchecks.Options;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace back.healthchecks;
|
namespace back.healthchecks;
|
||||||
|
|
||||||
[HealthCheckExecutionOptions(retryAttempts: 2, timeout: "00:00:05", retryDelay: "00:00:01", severity: HealthCheckSeverity.Critical)]
|
public class SqliteHealthCheck(IOptionsMonitor<DatabaseConfig> databaseConfig, IOptionsMonitor<HealthChecksConfigs> healthchecksConfig) : IHealthCheck
|
||||||
public class SqliteHealthCheck : IHealthCheck
|
|
||||||
{
|
{
|
||||||
private readonly DatabaseConfig config;
|
private readonly DatabaseConfig databaseConfig = databaseConfig.Get(DatabaseConfig.DataStorage);
|
||||||
public SqliteHealthCheck(IOptionsMonitor<DatabaseConfig> optionsSnapshot)
|
private readonly HealthChecksConfigs hcConfig = healthchecksConfig.Get(HealthChecksConfigs.Sqlite);
|
||||||
{
|
|
||||||
config = optionsSnapshot.Get(DatabaseConfig.DataStorage);
|
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)
|
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 isHealthy = false;
|
||||||
var details = string.Empty;
|
var details = string.Empty;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var connection = new Microsoft.Data.Sqlite.SqliteConnection(config.ConnectionString);
|
using var connection = new Microsoft.Data.Sqlite.SqliteConnection(databaseConfig.ConnectionString);
|
||||||
connection.Open();
|
connection.Open();
|
||||||
using var command = connection.CreateCommand();
|
using var command = connection.CreateCommand();
|
||||||
command.CommandText = $"SELECT COUNT(1) FROM Users WHERE Id = '{DataModels.User.SystemUserId}';";
|
command.CommandText = $"SELECT COUNT(1) FROM Users WHERE Id = '{DataModels.User.SystemUserId}';";
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
using back.DataModels;
|
using back.DataModels;
|
||||||
using back.DTO;
|
using back.DTO;
|
||||||
using DependencyInjector.Abstractions.ClassTypes;
|
|
||||||
using DependencyInjector.Lifetimes;
|
using DependencyInjector.Lifetimes;
|
||||||
|
|
||||||
namespace back.services.bussines.PhotoService;
|
namespace back.services.bussines.PhotoService;
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
using back.DataModels;
|
using back.DataModels;
|
||||||
using DependencyInjector.Abstractions.ClassTypes;
|
|
||||||
using DependencyInjector.Lifetimes;
|
using DependencyInjector.Lifetimes;
|
||||||
|
|
||||||
namespace back.services.bussines.UserService;
|
namespace back.services.bussines.UserService;
|
||||||
|
Reference in New Issue
Block a user