Public Access
34 lines
1.0 KiB
C#
34 lines
1.0 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace MeetingAssistant.Speakers;
|
|
|
|
public sealed class SpeakerIdentityDatabaseInitializer : IHostedService
|
|
{
|
|
private static readonly SemaphoreSlim InitializeLock = new(1, 1);
|
|
private readonly IDbContextFactory<SpeakerIdentityDbContext> dbContextFactory;
|
|
|
|
public SpeakerIdentityDatabaseInitializer(IDbContextFactory<SpeakerIdentityDbContext> dbContextFactory)
|
|
{
|
|
this.dbContextFactory = dbContextFactory;
|
|
}
|
|
|
|
public async Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
await InitializeLock.WaitAsync(cancellationToken);
|
|
try
|
|
{
|
|
await using var context = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
await SpeakerIdentitySchema.EnsureCreatedOrUpdatedAsync(context, cancellationToken);
|
|
}
|
|
finally
|
|
{
|
|
InitializeLock.Release();
|
|
}
|
|
}
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|