elsa-core/src/modules/Elsa.Diagnostics.StructuredLogs.Persistence.Sqlite/Services/SqliteStructuredLogStartupService.cs
Sipke Schoorstra 0687b5f9f3
Fix SQLite structured log shell lifecycle (#7461)
* Fix SQLite structured log shell lifecycle

* Address Greptile lifecycle feedback

* Tighten structured log lifecycle guards

* Address structured log background task review
2026-05-18 01:04:04 +02:00

47 lines
1.5 KiB
C#

using Elsa.Common;
using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Contracts;
using Elsa.Diagnostics.StructuredLogs.Persistence.Relational.Services;
using Elsa.Diagnostics.StructuredLogs.Persistence.Sqlite.Options;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
namespace Elsa.Diagnostics.StructuredLogs.Persistence.Sqlite.Services;
public class SqliteStructuredLogStartupService(
IStructuredLogSchemaMigrator schemaMigrator,
StructuredLogRetentionService retentionService,
IOptions<SqliteStructuredLogOptions> options) : IHostedService, IStartupTask
{
private readonly SemaphoreSlim _startupLock = new(1, 1);
private bool _executed;
public async Task StartAsync(CancellationToken cancellationToken)
{
await ExecuteAsync(cancellationToken);
}
public async Task ExecuteAsync(CancellationToken cancellationToken)
{
await _startupLock.WaitAsync(cancellationToken);
try
{
if (_executed)
return;
if (options.Value.RunMigrationsOnStartup)
await schemaMigrator.MigrateAsync(cancellationToken);
if (options.Value.Relational.Retention.CleanupOnStartup)
await retentionService.CleanupAsync(cancellationToken);
_executed = true;
}
finally
{
_startupLock.Release();
}
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}