Skip to content

Commit 72cb8ee

Browse files
authored
Cleanup KurrentDB integration (#930)
Use KurrentDB vars instead of outdated EventStore vars from #895. Updated client to latest verison. Replaced external health check dependency.
1 parent 3b1acbd commit 72cb8ee

File tree

13 files changed

+127
-32
lines changed

13 files changed

+127
-32
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
<PackageVersion Include="Aspire.Hosting.MongoDB" Version="$(AspireVersion)" />
2020
<PackageVersion Include="Aspire.Hosting.MySql" Version="$(AspireVersion)" />
2121
<PackageVersion Include="Aspire.Hosting.SqlServer" Version="$(AspireVersion)" />
22-
<PackageVersion Include="KurrentDB.Client" Version="1.0.0" />
2322
</ItemGroup>
2423
<ItemGroup Label="Core Packages">
2524
<!-- AspNetCore packages -->
@@ -92,6 +91,7 @@
9291
<PackageVersion Include="Microsoft.PowerShell.SDK" Version="7.4.10" />
9392
<PackageVersion Include="ModelContextProtocol" Version="0.3.0-preview.1" />
9493
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.3.0-preview.1" />
94+
<PackageVersion Include="KurrentDB.Client" Version="1.1.0" />
9595
</ItemGroup>
9696
<ItemGroup Label="Testing">
9797
<!-- Testing packages -->

src/CommunityToolkit.Aspire.Hosting.KurrentDB/CommunityToolkit.Aspire.Hosting.KurrentDB.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="AspNetCore.HealthChecks.EventStore.gRPC" />
109
<PackageReference Include="Aspire.Hosting" />
10+
<PackageReference Include="KurrentDB.Client" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

src/CommunityToolkit.Aspire.Hosting.KurrentDB/KurrentDBBuilderExtensions.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Aspire.Hosting.ApplicationModel;
55
using Aspire.Hosting.Utils;
66
using CommunityToolkit.Aspire.Hosting.KurrentDB;
7-
using HealthChecks.EventStore.gRPC;
87
using Microsoft.Extensions.DependencyInjection;
98
using Microsoft.Extensions.Diagnostics.HealthChecks;
109

@@ -60,7 +59,7 @@ public static IResourceBuilder<KurrentDBResource> AddKurrentDB(this IDistributed
6059
builder.Services.AddHealthChecks()
6160
.Add(new HealthCheckRegistration(
6261
healthCheckKey,
63-
sp => new EventStoreHealthCheck(connectionString!),
62+
sp => new KurrentDBHealthCheck(connectionString!),
6463
failureStatus: default,
6564
tags: default,
6665
timeout: default));
@@ -135,10 +134,10 @@ public static IResourceBuilder<KurrentDBResource> WithDataBindMount(this IResour
135134

136135
private static void ConfigureKurrentDBContainer(EnvironmentCallbackContext context)
137136
{
138-
context.EnvironmentVariables.Add("EVENTSTORE_CLUSTER_SIZE", "1");
139-
context.EnvironmentVariables.Add("EVENTSTORE_RUN_PROJECTIONS", "All");
140-
context.EnvironmentVariables.Add("EVENTSTORE_START_STANDARD_PROJECTIONS", "true");
141-
context.EnvironmentVariables.Add("EVENTSTORE_NODE_PORT", $"{KurrentDBResource.DefaultHttpPort}");
142-
context.EnvironmentVariables.Add("EVENTSTORE_INSECURE", "true");
137+
context.EnvironmentVariables.Add("KURRENTDB_CLUSTER_SIZE", "1");
138+
context.EnvironmentVariables.Add("KURRENTDB_RUN_PROJECTIONS", "All");
139+
context.EnvironmentVariables.Add("KURRENTDB_START_STANDARD_PROJECTIONS", "true");
140+
context.EnvironmentVariables.Add("KURRENTDB_NODE_PORT", $"{KurrentDBResource.DefaultHttpPort}");
141+
context.EnvironmentVariables.Add("KURRENTDB_INSECURE", "true");
143142
}
144143
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using KurrentDB.Client;
2+
using Microsoft.Extensions.Diagnostics.HealthChecks;
3+
4+
namespace CommunityToolkit.Aspire.Hosting.KurrentDB;
5+
6+
/// <summary>
7+
/// Checks whether a connection can be made to KurrentDB services using the supplied connection string.
8+
/// </summary>
9+
public class KurrentDBHealthCheck : IHealthCheck, IDisposable
10+
{
11+
private readonly KurrentDBClient _client;
12+
13+
/// <inheritdoc/>
14+
public KurrentDBHealthCheck(string connectionString)
15+
{
16+
ArgumentNullException.ThrowIfNull(connectionString);
17+
18+
_client = new KurrentDBClient(KurrentDBClientSettings.Create(connectionString));
19+
}
20+
21+
/// <inheritdoc/>
22+
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
23+
{
24+
try
25+
{
26+
var readAllStreamResult = _client.ReadAllAsync(
27+
direction: Direction.Backwards,
28+
position: Position.End,
29+
maxCount: 1,
30+
cancellationToken: cancellationToken);
31+
32+
var events = await readAllStreamResult.ToListAsync(cancellationToken);
33+
34+
if (events.Count > 0)
35+
{
36+
return HealthCheckResult.Healthy();
37+
}
38+
39+
return new HealthCheckResult(context.Registration.FailureStatus, "Failed to connect to KurrentDB.");
40+
}
41+
catch (Exception exception)
42+
{
43+
return new HealthCheckResult(context.Registration.FailureStatus, exception: exception);
44+
}
45+
}
46+
47+
/// <inheritdoc/>
48+
public virtual void Dispose() => _client.Dispose();
49+
}

src/CommunityToolkit.Aspire.Hosting.KurrentDB/KurrentDBResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ public class KurrentDBResource(string name) : ContainerResource(name), IResource
2424
/// </summary>
2525
public ReferenceExpression ConnectionStringExpression =>
2626
ReferenceExpression.Create(
27-
$"esdb://{PrimaryEndpoint.Property(EndpointProperty.Host)}:{PrimaryEndpoint.Property(EndpointProperty.Port)}?tls=false");
27+
$"kurrentdb://{PrimaryEndpoint.Property(EndpointProperty.Host)}:{PrimaryEndpoint.Property(EndpointProperty.Port)}?tls=false");
2828
}

src/CommunityToolkit.Aspire.KurrentDB/AspireKurrentDBExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using Aspire;
55
using CommunityToolkit.Aspire.KurrentDB;
6-
using HealthChecks.EventStore.gRPC;
76
using KurrentDB.Client;
87
using KurrentDB.Client.Extensions.OpenTelemetry;
98
using Microsoft.Extensions.Configuration;
@@ -91,7 +90,7 @@ private static void AddKurrentDBClient(
9190

9291
builder.TryAddHealthCheck(new HealthCheckRegistration(
9392
healthCheckName,
94-
sp => new EventStoreHealthCheck(settings.ConnectionString!),
93+
sp => new KurrentDBHealthCheck(settings.ConnectionString!),
9594
failureStatus: default,
9695
tags: default,
9796
timeout: settings.HealthCheckTimeout));

src/CommunityToolkit.Aspire.KurrentDB/CommunityToolkit.Aspire.KurrentDB.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="AspNetCore.HealthChecks.EventStore.gRPC" />
109
<PackageReference Include="KurrentDB.Client" />
1110
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
1211
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using KurrentDB.Client;
2+
using Microsoft.Extensions.Diagnostics.HealthChecks;
3+
4+
namespace CommunityToolkit.Aspire.KurrentDB;
5+
6+
/// <summary>
7+
/// Checks whether a connection can be made to KurrentDB services using the supplied connection string.
8+
/// </summary>
9+
public class KurrentDBHealthCheck : IHealthCheck, IDisposable
10+
{
11+
private readonly KurrentDBClient _client;
12+
13+
/// <inheritdoc/>
14+
public KurrentDBHealthCheck(string connectionString)
15+
{
16+
ArgumentNullException.ThrowIfNull(connectionString);
17+
18+
_client = new KurrentDBClient(KurrentDBClientSettings.Create(connectionString));
19+
}
20+
21+
/// <inheritdoc/>
22+
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
23+
{
24+
try
25+
{
26+
var readAllStreamResult = _client.ReadAllAsync(
27+
direction: Direction.Backwards,
28+
position: Position.End,
29+
maxCount: 1,
30+
cancellationToken: cancellationToken);
31+
32+
var events = await readAllStreamResult.ToListAsync(cancellationToken);
33+
34+
if (events.Count > 0)
35+
{
36+
return HealthCheckResult.Healthy();
37+
}
38+
39+
return new HealthCheckResult(context.Registration.FailureStatus, "Failed to connect to KurrentDB.");
40+
}
41+
catch (Exception exception)
42+
{
43+
return new HealthCheckResult(context.Registration.FailureStatus, exception: exception);
44+
}
45+
}
46+
47+
/// <inheritdoc/>
48+
public virtual void Dispose() => _client.Dispose();
49+
}

src/CommunityToolkit.Aspire.KurrentDB/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ And then the connection string will be retrieved from the `ConnectionStrings` co
4141
```json
4242
{
4343
"ConnectionStrings": {
44-
"kurrentdb": "esdb://localhost:22113?tls=false"
44+
"kurrentdb": "kurrentdb://localhost:22113?tls=false"
4545
}
4646
}
4747
```
@@ -55,7 +55,7 @@ The .NET Aspire KurrentDB Client integration supports [Microsoft.Extensions.Conf
5555
"Aspire": {
5656
"KurrentDB": {
5757
"Client": {
58-
"ConnectionString": "esdb://localhost:22113?tls=false",
58+
"ConnectionString": "kurrentdb://localhost:22113?tls=false",
5959
"DisableHealthChecks": true
6060
}
6161
}

tests/CommunityToolkit.Aspire.Hosting.KurrentDB.Tests/AddKurrentDBTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,27 @@ public async Task AddKurrentDBContainerWithDefaultsAddsAnnotationMetadata()
4444
Assert.Collection(config,
4545
env =>
4646
{
47-
Assert.Equal("EVENTSTORE_CLUSTER_SIZE", env.Key);
47+
Assert.Equal("KURRENTDB_CLUSTER_SIZE", env.Key);
4848
Assert.Equal("1", env.Value);
4949
},
5050
env =>
5151
{
52-
Assert.Equal("EVENTSTORE_RUN_PROJECTIONS", env.Key);
52+
Assert.Equal("KURRENTDB_RUN_PROJECTIONS", env.Key);
5353
Assert.Equal("All", env.Value);
5454
},
5555
env =>
5656
{
57-
Assert.Equal("EVENTSTORE_START_STANDARD_PROJECTIONS", env.Key);
57+
Assert.Equal("KURRENTDB_START_STANDARD_PROJECTIONS", env.Key);
5858
Assert.Equal("true", env.Value);
5959
},
6060
env =>
6161
{
62-
Assert.Equal("EVENTSTORE_NODE_PORT", env.Key);
62+
Assert.Equal("KURRENTDB_NODE_PORT", env.Key);
6363
Assert.Equal($"{KurrentDBResource.DefaultHttpPort}", env.Value);
6464
},
6565
ext =>
6666
{
67-
Assert.Equal("EVENTSTORE_INSECURE", ext.Key);
67+
Assert.Equal("KURRENTDB_INSECURE", ext.Key);
6868
Assert.Equal("true", ext.Value);
6969
});
7070
}
@@ -84,7 +84,7 @@ public async Task KurrentDBCreatesConnectionString()
8484
var connectionStringResource = Assert.Single(appModel.Resources.OfType<KurrentDBResource>()) as IResourceWithConnectionString;
8585
var connectionString = await connectionStringResource.GetConnectionStringAsync();
8686

87-
Assert.Equal("esdb://localhost:22113?tls=false", connectionString);
88-
Assert.Equal("esdb://{kurrentdb.bindings.http.host}:{kurrentdb.bindings.http.port}?tls=false", connectionStringResource.ConnectionStringExpression.ValueExpression);
87+
Assert.Equal("kurrentdb://localhost:22113?tls=false", connectionString);
88+
Assert.Equal("kurrentdb://{kurrentdb.bindings.http.host}:{kurrentdb.bindings.http.port}?tls=false", connectionStringResource.ConnectionStringExpression.ValueExpression);
8989
}
9090
}

0 commit comments

Comments
 (0)