Skip to content
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
12 changes: 9 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<!-- AWS SDK for .NET dependencies -->
<PackageVersion Include="AspNetCore.HealthChecks.Oracle" Version="8.0.1" />
<PackageVersion Include="AWSSDK.CloudFormation" Version="3.7.400.15" />
<PackageVersion Include="AWSSDK.SQS" Version="3.7.400.15" />
<PackageVersion Include="AWSSDK.SimpleNotificationService" Version="3.7.400.15" />
Expand Down Expand Up @@ -108,7 +109,7 @@
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.2.2" />
<PackageVersion Include="Microsoft.FluentUI.AspNetCore.Components" Version="4.9.3" />
<PackageVersion Include="Microsoft.FluentUI.AspNetCore.Components.Icons" Version="4.9.3" />
<PackageVersion Include="Milvus.Client" Version="2.3.0-preview.1"/>
<PackageVersion Include="Milvus.Client" Version="2.3.0-preview.1" />
<PackageVersion Include="MongoDB.Driver" Version="2.28.0" />
<PackageVersion Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="1.5.0" />
<PackageVersion Include="MySqlConnector.DependencyInjection" Version="2.3.6" />
Expand Down Expand Up @@ -175,13 +176,18 @@
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="6.6.0" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" Version="5.5.0" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.22.0" />



<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.EventHubs" Version="6.3.6"/>

<PackageVersion Include="Microsoft.Azure.Functions.Worker.OpenTelemetry" Version="1.0.0-preview1" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.4" />
<PackageVersion Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<!-- Pinned version for Component Governance - Remove when root dependencies are updated -->
<PackageVersion Include="Azure.Core" Version="1.42.0" />
<PackageVersion Include="Azure.Identity" Version="1.12.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" /> <!-- https://github.com/Azure/azure-cosmos-dotnet-v3/pull/3313 -->
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<!-- https://github.com/Azure/azure-cosmos-dotnet-v3/pull/3313 -->
</ItemGroup>
</Project>
</Project>
2 changes: 1 addition & 1 deletion NuGet.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
Expand Down
3 changes: 2 additions & 1 deletion playground/OracleEndToEnd/OracleEndToEnd.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
var pdb = oracle.AddDatabase("FREEPDB1");

builder.AddProject<Projects.OracleEndToEnd_ApiService>("api")
.WithReference(pdb);
.WithReference(pdb)
.WaitFor(pdb);

#if !SKIP_DASHBOARD_REFERENCE
// This project is only added in playground projects to support development/debugging
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static IResourceBuilder<MySqlServerResource> AddMySql(this IDistributedAp
{
if (!lookup.TryGetValue(databaseName.Key, out var databaseResource))
{
throw new DistributedApplicationException($"Database resource '{databaseName}' under SQL Server resource '{resource.Name}' was not found in the model.");
throw new DistributedApplicationException($"Database resource '{databaseName}' under MySql resource '{resource.Name}' was not found in the model.");
}

var connectionStringAvailableEvent = new ConnectionStringAvailableEvent(databaseResource, @event.Services);
Expand Down
4 changes: 4 additions & 0 deletions src/Aspire.Hosting.Oracle/Aspire.Hosting.Oracle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<Compile Include="$(SharedDir)VolumeNameGenerator.cs" Link="Utils\VolumeNameGenerator.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.Oracle" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Aspire.Hosting\Aspire.Hosting.csproj" />
</ItemGroup>
Expand Down
57 changes: 55 additions & 2 deletions src/Aspire.Hosting.Oracle/OracleDatabaseBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Utils;
using Microsoft.Extensions.DependencyInjection;

namespace Aspire.Hosting;

Expand All @@ -26,14 +27,48 @@ public static IResourceBuilder<OracleDatabaseServerResource> AddOracle(this IDis
var passwordParameter = password?.Resource ?? ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder, $"{name}-password");

var oracleDatabaseServer = new OracleDatabaseServerResource(name, passwordParameter);

string? connectionString = null;

builder.Eventing.Subscribe<ConnectionStringAvailableEvent>(oracleDatabaseServer, async (@event, ct) =>
{
connectionString = await oracleDatabaseServer.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false);

if (connectionString == null)
{
throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{oracleDatabaseServer.Name}' resource but the connection string was null.");
}

var lookup = builder.Resources.OfType<OracleDatabaseResource>().ToDictionary(d => d.Name);

foreach (var databaseName in oracleDatabaseServer.Databases)
{
if (!lookup.TryGetValue(databaseName.Key, out var databaseResource))
{
throw new DistributedApplicationException($"Database resource '{databaseName}' under Oracle resource '{oracleDatabaseServer.Name}' was not found in the model.");
}

var connectionStringAvailableEvent = new ConnectionStringAvailableEvent(databaseResource, @event.Services);
await builder.Eventing.PublishAsync<ConnectionStringAvailableEvent>(connectionStringAvailableEvent, ct).ConfigureAwait(false);

var beforeResourceStartedEvent = new BeforeResourceStartedEvent(databaseResource, @event.Services);
await builder.Eventing.PublishAsync(beforeResourceStartedEvent, ct).ConfigureAwait(false);
}
});

var healthCheckKey = $"{name}_check";
builder.Services.AddHealthChecks()
.AddOracle(sp => connectionString ?? throw new InvalidOperationException("Connection string is unavailable"), name: healthCheckKey);

return builder.AddResource(oracleDatabaseServer)
.WithEndpoint(port: port, targetPort: 1521, name: OracleDatabaseServerResource.PrimaryEndpointName)
.WithImage(OracleContainerImageTags.Image, OracleContainerImageTags.Tag)
.WithImageRegistry(OracleContainerImageTags.Registry)
.WithEnvironment(context =>
{
context.EnvironmentVariables[PasswordEnvVarName] = oracleDatabaseServer.PasswordParameter;
});
})
.WithHealthCheck(healthCheckKey);
}

/// <summary>
Expand All @@ -50,7 +85,25 @@ public static IResourceBuilder<OracleDatabaseResource> AddDatabase(this IResourc

builder.Resource.AddDatabase(name, databaseName);
var oracleDatabase = new OracleDatabaseResource(name, databaseName, builder.Resource);
return builder.ApplicationBuilder.AddResource(oracleDatabase);

string? connectionString = null;

builder.ApplicationBuilder.Eventing.Subscribe<ConnectionStringAvailableEvent>(oracleDatabase, async (@event, ct) =>
{
connectionString = await oracleDatabase.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false);

if (connectionString == null)
{
throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{oracleDatabase.Name}' resource but the connection string was null.");
}
});

var healthCheckKey = $"{name}_check";
builder.ApplicationBuilder.Services.AddHealthChecks()
.AddOracle(sp => connectionString ?? throw new InvalidOperationException("Connection string is unavailable"), name: healthCheckKey);

return builder.ApplicationBuilder.AddResource(oracleDatabase)
.WithHealthCheck(healthCheckKey);
}

/// <summary>
Expand Down
88 changes: 88 additions & 0 deletions tests/Aspire.Hosting.Oracle.Tests/OracleFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Components.Common.Tests;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Tests.Utils;
using Aspire.Hosting.Utils;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Polly;
using Xunit;
Expand Down Expand Up @@ -344,4 +346,90 @@ await pipeline.ExecuteAsync(async token =>
}
}
}

[Fact]
[RequiresDocker]
public async Task VerifyWaitForOnOracleBlocksDependentResources()
{
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3));
using var builder = TestDistributedApplicationBuilder.Create(testOutputHelper);

var healthCheckTcs = new TaskCompletionSource<HealthCheckResult>();
builder.Services.AddHealthChecks().AddAsyncCheck("blocking_check", () =>
{
return healthCheckTcs.Task;
});

var resource = builder.AddOracle("resource")
.WithHealthCheck("blocking_check");

var dependentResource = builder.AddOracle("dependentresource")
.WaitFor(resource);

using var app = builder.Build();

var pendingStart = app.StartAsync(cts.Token);

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

await rns.WaitForResourceAsync(resource.Resource.Name, KnownResourceStates.Running, cts.Token);

await rns.WaitForResourceAsync(dependentResource.Resource.Name, KnownResourceStates.Waiting, cts.Token);

healthCheckTcs.SetResult(HealthCheckResult.Healthy());

await rns.WaitForResourceAsync(resource.Resource.Name, (re => re.Snapshot.HealthStatus == HealthStatus.Healthy), cts.Token);

await rns.WaitForResourceAsync(dependentResource.Resource.Name, KnownResourceStates.Running, cts.Token);

await pendingStart;

await app.StopAsync();
}

[Fact]
[RequiresDocker]
public async Task VerifyWaitForOnOracleDatabaseBlocksDependentResources()
{
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(5));
using var builder = TestDistributedApplicationBuilder.Create(testOutputHelper);

var healthCheckTcs = new TaskCompletionSource<HealthCheckResult>();
builder.Services.AddHealthChecks().AddAsyncCheck("blocking_check", () =>
{
return healthCheckTcs.Task;
});

var resource = builder.AddOracle("resource")
.WithHealthCheck("blocking_check");

var db = resource.AddDatabase("db");

var dependentResource = builder.AddOracle("dependentresource")
.WaitFor(db);

using var app = builder.Build();

var pendingStart = app.StartAsync(cts.Token);

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

await rns.WaitForResourceAsync(resource.Resource.Name, KnownResourceStates.Running, cts.Token);

await rns.WaitForResourceAsync(db.Resource.Name, KnownResourceStates.Running, cts.Token);

await rns.WaitForResourceAsync(dependentResource.Resource.Name, KnownResourceStates.Waiting, cts.Token);

healthCheckTcs.SetResult(HealthCheckResult.Healthy());

await rns.WaitForResourceAsync(resource.Resource.Name, (re => re.Snapshot.HealthStatus == HealthStatus.Healthy), cts.Token);

await rns.WaitForResourceAsync(db.Resource.Name, re => re.Snapshot.HealthStatus == HealthStatus.Healthy, cts.Token);

await rns.WaitForResourceAsync(dependentResource.Resource.Name, KnownResourceStates.Running, cts.Token);

await pendingStart;

await app.StopAsync();
}
}