Skip to content

Commit

Permalink
WaitFor support for Dapr components. (#6240)
Browse files Browse the repository at this point in the history
* Dapr with wait support (WIP).

* WIP

* Copy WaitAnnotation to dapr cli resources.
  • Loading branch information
mitchdenny authored Oct 15, 2024
1 parent 887859b commit 277781b
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 3 deletions.
1 change: 1 addition & 0 deletions playground/dapr/Dapr.AppHost/Dapr.AppHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ItemGroup>
<AspireProjectOrPackageReference Include="Aspire.Hosting.AppHost" />
<AspireProjectOrPackageReference Include="Aspire.Hosting.Dapr" />
<AspireProjectOrPackageReference Include="Aspire.Hosting.RabbitMQ" />

<ProjectReference Include="..\ServiceA\DaprServiceA.csproj" />
<ProjectReference Include="..\ServiceB\DaprServiceB.csproj" />
Expand Down
8 changes: 7 additions & 1 deletion playground/dapr/Dapr.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
var builder = DistributedApplication.CreateBuilder(args);

var rmq = builder.AddRabbitMQ("rabbitMQ")
.WithManagementPlugin()
.WithEndpoint("tcp", e => e.Port = 5672)
.WithEndpoint("management", e => e.Port = 15672);

var stateStore = builder.AddDaprStateStore("statestore");
var pubSub = builder.AddDaprPubSub("pubsub");
var pubSub = builder.AddDaprPubSub("pubsub")
.WaitFor(rmq);

builder.AddProject<Projects.DaprServiceA>("servicea")
.WithDaprSidecar()
Expand Down
17 changes: 17 additions & 0 deletions playground/dapr/Dapr.AppHost/pubsub.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: pubsub
namespace: default
spec:
type: pubsub.rabbitmq
version: v1
metadata:
- name: protocol
value: amqp
- name: hostname
value: localhost
- name: username
value: guest
- name: password
value: guest
81 changes: 81 additions & 0 deletions src/Aspire.Dashboard/Resources/Logs.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/Aspire.Hosting.Dapr/DaprDistributedApplicationLifecycleHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,16 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell

var componentReferenceAnnotations = resource.Annotations.OfType<DaprComponentReferenceAnnotation>();

var waitAnnotationsToCopyToDaprCli = new List<WaitAnnotation>();

foreach (var componentReferenceAnnotation in componentReferenceAnnotations)
{
// Whilst we are passing over each component annotations collect the list of annotations to copy to the Dapr CLI.
if (componentReferenceAnnotation.Component.TryGetAnnotationsOfType<WaitAnnotation>(out var componentWaitAnnotations))
{
waitAnnotationsToCopyToDaprCli.AddRange(componentWaitAnnotations);
}

if (componentReferenceAnnotation.Component.Options?.LocalPath is not null)
{
var localPathDirectory = Path.GetDirectoryName(NormalizePath(componentReferenceAnnotation.Component.Options.LocalPath));
Expand All @@ -94,6 +102,9 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell
}
}

// It is possible that we have duplicate wate annotations so we just dedupe them here.
var distinctWaitAnnotationsToCopyToDaprCli = waitAnnotationsToCopyToDaprCli.DistinctBy(w => (w.Resource, w.WaitType));

var daprAppPortArg = (int? port) => ModelNamedArg("--app-port", port);
var daprGrpcPortArg = (object port) => ModelNamedObjectArg("--dapr-grpc-port", port);
var daprHttpPortArg = (object port) => ModelNamedObjectArg("--dapr-http-port", port);
Expand Down Expand Up @@ -137,6 +148,9 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell
var daprCliResourceName = $"{daprSidecar.Name}-cli";
var daprCli = new ExecutableResource(daprCliResourceName, fileName, appHostDirectory);

// Add all the unique wait annotations to the CLI.
daprCli.Annotations.AddRange(distinctWaitAnnotationsToCopyToDaprCli);

resource.Annotations.Add(
new EnvironmentCallbackAnnotation(
context =>
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Hosting.Dapr/IDaprComponentResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Aspire.Hosting.Dapr;
/// <summary>
/// Represents a Dapr component resource.
/// </summary>
public interface IDaprComponentResource : IResource
public interface IDaprComponentResource : IResource, IResourceWithWaitSupport
{
/// <summary>
/// Gets the type of the Dapr component.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public static IDistributedApplicationBuilder AddDapr(this IDistributedApplicatio
public static IResourceBuilder<IDaprComponentResource> AddDaprComponent(this IDistributedApplicationBuilder builder, [ResourceName] string name, string type, DaprComponentOptions? options = null)
{
var resource = new DaprComponentResource(name, type) { Options = options };

return builder
.AddResource(resource)
.WithInitialState(new()
Expand Down

0 comments on commit 277781b

Please sign in to comment.