Add profiles domain, infra, API endpoints
This commit is contained in:
@@ -1,7 +1,16 @@
|
||||
using RecNet.MigrationService;
|
||||
using RecNet.Infrastructure.Persistence;
|
||||
using RecNet.ServiceDefaults;
|
||||
|
||||
var builder = Host.CreateApplicationBuilder(args);
|
||||
|
||||
builder.AddServiceDefaults();
|
||||
builder.Services.AddHostedService<Worker>();
|
||||
|
||||
builder.Services.AddOpenTelemetry()
|
||||
.WithTracing(tracing => tracing.AddSource(Worker.ActivitySourceName));
|
||||
|
||||
builder.AddNpgsqlDbContext<DatabaseContext>("recnet");
|
||||
|
||||
var host = builder.Build();
|
||||
host.Run();
|
||||
@@ -8,6 +8,15 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.8">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.8"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\RecNet.Infrastructure\RecNet.Infrastructure.csproj" />
|
||||
<ProjectReference Include="..\RecNet.ServiceDefaults\RecNet.ServiceDefaults.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -1,17 +1,48 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
using RecNet.Infrastructure.Persistence;
|
||||
|
||||
namespace RecNet.MigrationService;
|
||||
|
||||
public class Worker(ILogger<Worker> logger) : BackgroundService
|
||||
public class Worker(
|
||||
IServiceProvider serviceProvider,
|
||||
IHostApplicationLifetime hostApplicationLifetime) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
}
|
||||
public const string ActivitySourceName = "Migrations";
|
||||
private static readonly ActivitySource ActivitySource = new(ActivitySourceName);
|
||||
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
protected override async Task ExecuteAsync(
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
using var activity = ActivitySource.StartActivity(
|
||||
"Migrating database", ActivityKind.Client);
|
||||
|
||||
try
|
||||
{
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var dbContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||
|
||||
await RunMigrationAsync(dbContext, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
activity?.AddException(ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
hostApplicationLifetime.StopApplication();
|
||||
}
|
||||
|
||||
private static async Task RunMigrationAsync(
|
||||
DatabaseContext dbContext, CancellationToken cancellationToken)
|
||||
{
|
||||
var strategy = dbContext.Database.CreateExecutionStrategy();
|
||||
await strategy.ExecuteAsync(async () =>
|
||||
{
|
||||
// Run migration in a transaction to avoid partial migration if it fails.
|
||||
await dbContext.Database.MigrateAsync(cancellationToken);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user