| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Contracts; |
| | | 3 | | using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Services; |
| | | 4 | | using Elsa.Diagnostics.StructuredLogs.Persistence.Sqlite.Options; |
| | | 5 | | using Microsoft.Extensions.Hosting; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Diagnostics.StructuredLogs.Persistence.Sqlite.Services; |
| | | 9 | | |
| | 8 | 10 | | public class SqliteStructuredLogStartupService( |
| | 8 | 11 | | IStructuredLogSchemaMigrator schemaMigrator, |
| | 8 | 12 | | StructuredLogRetentionService retentionService, |
| | 8 | 13 | | IOptions<SqliteStructuredLogOptions> options) : IHostedService, IStartupTask |
| | | 14 | | { |
| | 8 | 15 | | private readonly SemaphoreSlim _startupLock = new(1, 1); |
| | | 16 | | private bool _executed; |
| | | 17 | | |
| | | 18 | | public async Task StartAsync(CancellationToken cancellationToken) |
| | | 19 | | { |
| | 4 | 20 | | await ExecuteAsync(cancellationToken); |
| | 4 | 21 | | } |
| | | 22 | | |
| | | 23 | | public async Task ExecuteAsync(CancellationToken cancellationToken) |
| | | 24 | | { |
| | 6 | 25 | | await _startupLock.WaitAsync(cancellationToken); |
| | | 26 | | try |
| | | 27 | | { |
| | 6 | 28 | | if (_executed) |
| | 0 | 29 | | return; |
| | | 30 | | |
| | 6 | 31 | | if (options.Value.RunMigrationsOnStartup) |
| | 5 | 32 | | await schemaMigrator.MigrateAsync(cancellationToken); |
| | | 33 | | |
| | 6 | 34 | | if (options.Value.Relational.Retention.CleanupOnStartup) |
| | 0 | 35 | | await retentionService.CleanupAsync(cancellationToken); |
| | | 36 | | |
| | 6 | 37 | | _executed = true; |
| | 6 | 38 | | } |
| | | 39 | | finally |
| | | 40 | | { |
| | 6 | 41 | | _startupLock.Release(); |
| | | 42 | | } |
| | 6 | 43 | | } |
| | | 44 | | |
| | 3 | 45 | | public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; |
| | | 46 | | } |