Heimdall Gitea / Detect CI scope (pull_request) Successful in 4s
Heimdall Gitea / Build and tests (pull_request) Successful in 14m48s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan server image (pull_request) Successful in 4m24s
Heimdall Gitea / Build and scan agent image (pull_request) Successful in 9m12s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Successful in 4s
89 lines
3.3 KiB
C#
89 lines
3.3 KiB
C#
using Heimdall.Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Heimdall.Infrastructure.Jobs;
|
|
|
|
/// <summary>
|
|
/// DatabaseMaintenanceJob — maintenance PostgreSQL hebdomadaire :
|
|
/// VACUUM + REINDEX per partition. VACUUM FULL mensuel (1er dimanche du
|
|
/// mois). Cron : dimanche 7h.
|
|
/// </summary>
|
|
public class DatabaseMaintenanceJob
|
|
{
|
|
private readonly IDbContextFactory<AppDbContext> _dbFactory;
|
|
private readonly ILogger<DatabaseMaintenanceJob> _logger;
|
|
|
|
public DatabaseMaintenanceJob(IDbContextFactory<AppDbContext> dbFactory, ILogger<DatabaseMaintenanceJob> logger)
|
|
{
|
|
_dbFactory = dbFactory;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task ExecuteAsync(CancellationToken ct)
|
|
{
|
|
await using var db = await _dbFactory.CreateDbContextAsync(ct);
|
|
var isFirstSunday = DateTime.UtcNow.Day <= 7;
|
|
|
|
try
|
|
{
|
|
// List all camera_events partitions
|
|
var partitions = await GetPartitionsAsync(db, ct).ConfigureAwait(false);
|
|
|
|
foreach (var partition in partitions)
|
|
{
|
|
try
|
|
{
|
|
// EF Core exception: VACUUM and REINDEX are PostgreSQL maintenance commands.
|
|
// VACUUM per partition (FULL on first Sunday of month)
|
|
var vacuumSql = isFirstSunday
|
|
? $"VACUUM FULL \"{partition}\";"
|
|
: $"VACUUM \"{partition}\";";
|
|
await db.Database.ExecuteSqlRawAsync(vacuumSql, ct).ConfigureAwait(false);
|
|
_logger.LogDebug("Vacuum: {Partition}", partition);
|
|
|
|
// REINDEX per partition
|
|
var reindexSql = $"REINDEX TABLE \"{partition}\";";
|
|
await db.Database.ExecuteSqlRawAsync(reindexSql, ct).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// VACUUM can fail if another transaction is active — non fatal.
|
|
_logger.LogWarning(ex, "Maintenance failed for partition {Partition} (may be locked)", partition);
|
|
}
|
|
}
|
|
|
|
_logger.LogInformation("Database maintenance complete: {Count} partitions processed", partitions.Count);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "Database maintenance failed (may be locked by another transaction)");
|
|
}
|
|
}
|
|
|
|
private static async Task<List<string>> GetPartitionsAsync(AppDbContext db, CancellationToken ct)
|
|
{
|
|
// EF Core exception: pg_tables is a PostgreSQL system catalog with no portable model.
|
|
var partitions = new List<string>();
|
|
|
|
await using var connection = db.Database.GetDbConnection();
|
|
await connection.OpenAsync(ct).ConfigureAwait(false);
|
|
|
|
await using var command = connection.CreateCommand();
|
|
command.CommandText = @"
|
|
SELECT tablename FROM pg_tables
|
|
WHERE schemaname = 'public'
|
|
AND tablename LIKE 'camera_events_%'
|
|
ORDER BY tablename;
|
|
";
|
|
|
|
await using var reader = await command.ExecuteReaderAsync(ct).ConfigureAwait(false);
|
|
while (await reader.ReadAsync(ct).ConfigureAwait(false))
|
|
{
|
|
partitions.Add(reader.GetString(0));
|
|
}
|
|
|
|
return partitions;
|
|
}
|
|
}
|