Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ResourceReadyEvent to avoid blocking startup importing redis databases #6308

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,16 @@ public static IResourceBuilder<RedisResource> WithRedisInsight(this IResourceBui
.WithHttpEndpoint(targetPort: 5540, name: "http")
.ExcludeFromManifest();

builder.ApplicationBuilder.Eventing.Subscribe<AfterResourcesCreatedEvent>(async (e, ct) =>
// We need to wait for all endpoints to be allocated before attempting to import databases
var endpointsAllocatedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

builder.ApplicationBuilder.Eventing.Subscribe<AfterEndpointsAllocatedEvent>((e, ct) =>
{
endpointsAllocatedTcs.TrySetResult();
return Task.CompletedTask;
});

builder.ApplicationBuilder.Eventing.Subscribe<ResourceReadyEvent>(resource, async (e, ct) =>
{
var redisInstances = builder.ApplicationBuilder.Resources.OfType<RedisResource>();

Expand All @@ -163,6 +172,9 @@ public static IResourceBuilder<RedisResource> WithRedisInsight(this IResourceBui
return;
}

// Wait for all endpoints to be allocated before attempting to import databases
await endpointsAllocatedTcs.Task.ConfigureAwait(false);

var redisInsightResource = builder.ApplicationBuilder.Resources.OfType<RedisInsightResource>().Single();
var insightEndpoint = redisInsightResource.PrimaryEndpoint;

Expand Down
14 changes: 11 additions & 3 deletions tests/Aspire.Hosting.Redis.Tests/RedisFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,21 @@ public async Task VerifyWithRedisInsightImportDatabases()
IResourceBuilder<RedisInsightResource>? redisInsightBuilder = null;
var redis2 = builder.AddRedis("redis-2").WithRedisInsight(c => redisInsightBuilder = c);
Assert.NotNull(redisInsightBuilder);

// RedisInsight will import databases when it is ready, this task will run after the initial databases import
// so we will use that to know when the databases have been successfully imported
var redisInsightsReady = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
builder.Eventing.Subscribe<ResourceReadyEvent>(redisInsightBuilder.Resource, (evt, ct) =>
{
redisInsightsReady.TrySetResult();
return Task.CompletedTask;
});

using var app = builder.Build();

await app.StartAsync();

var rns = app.Services.GetRequiredService<ResourceNotificationService>();

await rns.WaitForResourceAsync(redisInsightBuilder.Resource.Name, KnownResourceStates.Running, cts.Token);
await redisInsightsReady.Task.WaitAsync(cts.Token);

var client = app.CreateHttpClient(redisInsightBuilder.Resource.Name, "http");

Expand Down