| | | 1 | | using System.Data.Common; |
| | | 2 | | using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Contracts; |
| | | 3 | | using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Options; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Services; |
| | | 7 | | |
| | 0 | 8 | | public class StructuredLogRetentionService( |
| | 0 | 9 | | IRelationalStructuredLogConnectionFactory connectionFactory, |
| | 0 | 10 | | IRelationalStructuredLogDialect dialect, |
| | 0 | 11 | | RelationalStructuredLogSqlBuilder sqlBuilder, |
| | 0 | 12 | | IOptions<RelationalStructuredLogOptions> options) |
| | | 13 | | { |
| | | 14 | | public async ValueTask CleanupAsync(CancellationToken cancellationToken = default) |
| | | 15 | | { |
| | 0 | 16 | | var retention = options.Value.Retention; |
| | | 17 | | |
| | 0 | 18 | | if (retention.MaxAge == null && retention.MaxRows == null) |
| | 0 | 19 | | return; |
| | | 20 | | |
| | 0 | 21 | | await using var connection = await connectionFactory.OpenConnectionAsync(cancellationToken); |
| | | 22 | | |
| | 0 | 23 | | if (retention.MaxAge is { } maxAge) |
| | | 24 | | { |
| | 0 | 25 | | var cutoff = RelationalStructuredLogMapper.FormatTimestamp(DateTimeOffset.UtcNow.Subtract(maxAge)); |
| | 0 | 26 | | await ExecuteAsync(connection, sqlBuilder.BuildDeleteOlderThan(cutoff), cancellationToken); |
| | | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | if (retention.MaxRows is { } maxRows and >= 0) |
| | 0 | 30 | | await ExecuteAsync(connection, sqlBuilder.BuildDeleteRowsBeyondMax(maxRows), cancellationToken); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | private async ValueTask ExecuteAsync(DbConnection connection, QueryDefinition query, CancellationToken cancellationT |
| | | 34 | | { |
| | 0 | 35 | | await using var command = connection.CreateCommand(); |
| | 0 | 36 | | command.CommandText = query.Sql; |
| | | 37 | | |
| | 0 | 38 | | foreach (var (name, value) in query.Parameters) |
| | | 39 | | { |
| | 0 | 40 | | var parameter = command.CreateParameter(); |
| | 0 | 41 | | parameter.ParameterName = name.StartsWith(dialect.ParameterPrefix, StringComparison.Ordinal) ? name : $"{dia |
| | 0 | 42 | | parameter.Value = value ?? DBNull.Value; |
| | 0 | 43 | | command.Parameters.Add(parameter); |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | await command.ExecuteNonQueryAsync(cancellationToken); |
| | 0 | 47 | | } |
| | | 48 | | } |