Skip to content

Commit

Permalink
Move resource creation in ResourceReadyEvent event (#7211)
Browse files Browse the repository at this point in the history
* Move resource creation in ResourceReadyEvent event

* Update src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBExtensions.cs

---------

Co-authored-by: David Fowler <[email protected]>
  • Loading branch information
sebastienros and davidfowl authored Jan 23, 2025
1 parent 71ba8ac commit 174f923
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#pragma warning disable ASPIRECOSMOS001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
var db = builder.AddAzureCosmosDB("cosmos")
.WithDatabase("db", database => database.Containers.Add(new("entries", "/Id")))
.WithDatabase("db", database => database.Containers.Add(new("entries", "/id")))
.RunAsPreviewEmulator(e => e.WithDataExplorer());
#pragma warning restore ASPIRECOSMOS001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ProjectReference Include="..\Aspire.Hosting.Azure\Aspire.Hosting.Azure.csproj" />
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Microsoft.Azure.Cosmos" />
<PackageReference Include="AspNetCore.HealthChecks.CosmosDb" />
<PackageReference Include="Newtonsoft.Json" /> <!-- Required by Microsoft.Azure.Cosmos -->
<PackageReference Include="Azure.Provisioning" />
<PackageReference Include="Azure.Provisioning.CosmosDB" />
Expand Down

This file was deleted.

35 changes: 24 additions & 11 deletions src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Azure.Provisioning.KeyVault;
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Aspire.Hosting;

Expand Down Expand Up @@ -97,18 +96,32 @@ private static IResourceBuilder<AzureCosmosDBResource> RunAsEmulator(this IResou
cosmosClient = CreateCosmosClient(connectionString);
});

builder.ApplicationBuilder.Eventing.Subscribe<ResourceReadyEvent>(builder.Resource, async (@event, ct) =>
{
if (cosmosClient is null)
{
throw new InvalidOperationException("CosmosClient is not initialized.");
}

await cosmosClient.ReadAccountAsync().WaitAsync(ct).ConfigureAwait(false);

foreach (var database in builder.Resource.Databases)
{
var db = (await cosmosClient.CreateDatabaseIfNotExistsAsync(database.Name, cancellationToken: ct).ConfigureAwait(false)).Database;

foreach (var container in database.Containers)
{
await db.CreateContainerIfNotExistsAsync(container.Name, container.PartitionKeyPath, cancellationToken: ct).ConfigureAwait(false);
}
}
});

// Use custom health check that also seeds the databases and containers
var healthCheckKey = $"{builder.Resource.Name}_check";
builder.ApplicationBuilder.Services.AddHealthChecks().Add(
new HealthCheckRegistration(
name: healthCheckKey,
new AzureCosmosDBEmulatorHealthCheck(
() => cosmosClient ?? throw new InvalidOperationException("CosmosClient is not initialized."),
builder.Resource.Databases.ToArray
),
failureStatus: null,
tags: null)
);
builder.ApplicationBuilder.Services.AddHealthChecks().AddAzureCosmosDB(
sp => cosmosClient ?? throw new InvalidOperationException("CosmosClient is not initialized."),
name: healthCheckKey
);

builder.WithHealthCheck(healthCheckKey);

Expand Down

0 comments on commit 174f923

Please sign in to comment.