Skip to content

Commit

Permalink
Added migration projects and wait processes at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
csharpfritz committed Jun 25, 2024
1 parent 4ce41e5 commit c4c38bb
Show file tree
Hide file tree
Showing 12 changed files with 453 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/TagzApp.AppHost/DatabaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public static class DatabaseConfig
public static IDistributedApplicationBuilder AddDatabase(
this IDistributedApplicationBuilder builder
, out IResourceBuilder<PostgresDatabaseResource> db
, out IResourceBuilder<PostgresDatabaseResource> securityDb)
, out IResourceBuilder<PostgresDatabaseResource> securityDb
, out IResourceBuilder<ProjectResource> migration)
{

var dbPassword = builder.AddParameter("dbPassword", false);
Expand All @@ -25,6 +26,13 @@ this IDistributedApplicationBuilder builder

securityDb = dbServer.AddDatabase("securitydb");


migration = builder.AddProject<Projects.TagzApp_MigrationService>("db-migrations")
.WaitFor(db)
.WaitFor(securityDb)
.WithReference(db)
.WithReference(securityDb);

return builder;

}
Expand Down
6 changes: 5 additions & 1 deletion src/TagzApp.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

var builder = DistributedApplication.CreateBuilder(args);

builder.AddDatabase(out var db, out var securityDb);
builder.AddDatabase(
out var db,
out var securityDb,
out var migration);

var twitchCache = builder.AddRedis("twitchCache");
var twitchRelay = builder.AddExecutable("twitchrelay",
Expand All @@ -15,6 +18,7 @@
#region Website

var tagzAppWeb = builder.AddProject<Projects.TagzApp_Blazor>("web", "https")
.WaitForCompletion(migration)
.WithReference(db)
.WithReference(securityDb)
.WithEnvironment("TwitchRelayUri", "http://localhost:7082");
Expand Down
2 changes: 2 additions & 0 deletions src/TagzApp.AppHost/TagzApp.AppHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TagzApp.AppHostExtensions\TagzApp.AppHostExtensions.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\TagzApp.Blazor\TagzApp.Blazor.csproj" />
<ProjectReference Include="..\TagzApp.DatabaseMigration\TagzApp.MigrationService.csproj" />
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions src/TagzApp.AppHostExtensions/HealthCheckAnnotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using Aspire.Hosting.ApplicationModel;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Aspire.Hosting;

/// <summary>
/// An annotation that associates a health check factory with a resource
/// </summary>
/// <param name="healthCheckFactory">A function that creates the health check</param>
public class HealthCheckAnnotation(Func<IResource, CancellationToken, Task<IHealthCheck?>> healthCheckFactory) : IResourceAnnotation
{
public Func<IResource, CancellationToken, Task<IHealthCheck?>> HealthCheckFactory { get; } = healthCheckFactory;

public static HealthCheckAnnotation Create(Func<string, IHealthCheck> connectionStringFactory)
{
return new(async (resource, token) =>
{
if (resource is not IResourceWithConnectionString c)
{
return null;
}

if (await c.GetConnectionStringAsync(token) is not string cs)
{
return null;
}

return connectionStringFactory(cs);
});
}
}
23 changes: 23 additions & 0 deletions src/TagzApp.AppHostExtensions/PostgreSqlHealthCheckExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Aspire.Hosting.ApplicationModel;
using HealthChecks.NpgSql;

namespace Aspire.Hosting;

public static class PostgreSqlHealthCheckExtensions
{
/// <summary>
/// Adds a health check to the PostgreSQL server resource.
/// </summary>
public static IResourceBuilder<PostgresServerResource> WithHealthCheck(this IResourceBuilder<PostgresServerResource> builder)
{
return builder.WithAnnotation(HealthCheckAnnotation.Create(cs => new NpgSqlHealthCheck(new NpgSqlHealthCheckOptions(cs))));
}

/// <summary>
/// Adds a health check to the PostgreSQL database resource.
/// </summary>
public static IResourceBuilder<PostgresDatabaseResource> WithHealthCheck(this IResourceBuilder<PostgresDatabaseResource> builder)
{
return builder.WithAnnotation(HealthCheckAnnotation.Create(cs => new NpgSqlHealthCheck(new NpgSqlHealthCheckOptions(cs))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Aspire.Hosting.ApplicationModel;
using HealthChecks.Redis;

namespace Aspire.Hosting;

public static class RedisResourceHealthCheckExtensions
{
/// <summary>
/// Adds a health check to the Redis server resource.
/// </summary>
public static IResourceBuilder<RedisResource> WithHealthCheck(this IResourceBuilder<RedisResource> builder)
{
return builder.WithAnnotation(HealthCheckAnnotation.Create(cs => new RedisHealthCheck(cs)));
}
}
18 changes: 18 additions & 0 deletions src/TagzApp.AppHostExtensions/TagzApp.AppHostExtensions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting" Version="8.0.*" />
<PackageReference Include="Aspire.Hosting.Redis" Version="8.0.*" />
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="8.0.*" />
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="8.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="8.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.Uris" Version="8.0.1" />
</ItemGroup>

</Project>
Loading

0 comments on commit c4c38bb

Please sign in to comment.