|
4 | 4 | using System.Net.Sockets;
|
5 | 5 | using Aspire.Hosting.ApplicationModel;
|
6 | 6 | using Aspire.Hosting.Publishing;
|
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.Extensions.Logging; |
7 | 9 |
|
8 | 10 | namespace Aspire.Hosting;
|
9 | 11 |
|
@@ -549,4 +551,145 @@ public static IResourceBuilder<T> ExcludeFromManifest<T>(this IResourceBuilder<T
|
549 | 551 | {
|
550 | 552 | return builder.WithAnnotation(ManifestPublishingCallbackAnnotation.Ignore);
|
551 | 553 | }
|
| 554 | + |
| 555 | + /// <summary> |
| 556 | + /// Waits for the dependency resource to enter the Running state before starting the resource. |
| 557 | + /// </summary> |
| 558 | + /// <typeparam name="T">The type of the resource.</typeparam> |
| 559 | + /// <param name="builder">The resource builder for the resource that will be waiting.</param> |
| 560 | + /// <param name="dependency">The resource builder for the dependency resource.</param> |
| 561 | + /// <returns>The resource builder.</returns> |
| 562 | + /// <remarks> |
| 563 | + /// <para>This method is useful when a resource should wait until another has started running. This can help |
| 564 | + /// reduce errors in logs during local development where dependency resources.</para> |
| 565 | + /// </remarks> |
| 566 | + /// <example> |
| 567 | + /// Start message queue before starting the worker service. |
| 568 | + /// <code lang="C#"> |
| 569 | + /// var builder = DistributedApplication.CreateBuilder(args); |
| 570 | + /// var messaging = builder.AddRabbitMQ("messaging"); |
| 571 | + /// builder.AddProject<Projects.MyApp>("myapp") |
| 572 | + /// .WithReference(messaging) |
| 573 | + /// .WaitFor(messaging); |
| 574 | + /// </code> |
| 575 | + /// </example> |
| 576 | + public static IResourceBuilder<T> WaitFor<T>(this IResourceBuilder<T> builder, IResourceBuilder<IResource> dependency) where T : IResource |
| 577 | + { |
| 578 | + builder.ApplicationBuilder.Eventing.Subscribe<BeforeResourceStartedEvent>(builder.Resource, async (e, ct) => |
| 579 | + { |
| 580 | + var rls = e.Services.GetRequiredService<ResourceLoggerService>(); |
| 581 | + var resourceLogger = rls.GetLogger(builder.Resource); |
| 582 | + resourceLogger.LogInformation("Waiting for resource '{Name}' to enter the '{State}' state.", dependency.Resource.Name, KnownResourceStates.Running); |
| 583 | + |
| 584 | + var rns = e.Services.GetRequiredService<ResourceNotificationService>(); |
| 585 | + await rns.PublishUpdateAsync(builder.Resource, s => s with { State = KnownResourceStates.Waiting }).ConfigureAwait(false); |
| 586 | + var resourceEvent = await rns.WaitForResourceAsync(dependency.Resource.Name, re => IsContinuableState(re.Snapshot), cancellationToken: ct).ConfigureAwait(false); |
| 587 | + var snapshot = resourceEvent.Snapshot; |
| 588 | + |
| 589 | + if (snapshot.State == KnownResourceStates.FailedToStart) |
| 590 | + { |
| 591 | + resourceLogger.LogError( |
| 592 | + "Dependency resource '{ResourceName}' failed to start.", |
| 593 | + dependency.Resource.Name |
| 594 | + ); |
| 595 | + |
| 596 | + throw new DistributedApplicationException($"Dependency resource '{dependency.Resource.Name}' failed to start."); |
| 597 | + } |
| 598 | + else if (snapshot.State!.Text == KnownResourceStates.Finished || snapshot.State!.Text == KnownResourceStates.Exited) |
| 599 | + { |
| 600 | + resourceLogger.LogError( |
| 601 | + "Resource '{ResourceName}' has entered the '{State}' state prematurely.", |
| 602 | + dependency.Resource.Name, |
| 603 | + snapshot.State.Text |
| 604 | + ); |
| 605 | + |
| 606 | + throw new DistributedApplicationException( |
| 607 | + $"Resource '{dependency.Resource.Name}' has entered the '{snapshot.State.Text}' state prematurely." |
| 608 | + ); |
| 609 | + } |
| 610 | + }); |
| 611 | + |
| 612 | + return builder; |
| 613 | + |
| 614 | + static bool IsContinuableState(CustomResourceSnapshot snapshot) => |
| 615 | + snapshot.State?.Text == KnownResourceStates.Running || |
| 616 | + snapshot.State?.Text == KnownResourceStates.Finished || |
| 617 | + snapshot.State?.Text == KnownResourceStates.Exited || |
| 618 | + snapshot.State?.Text == KnownResourceStates.FailedToStart; |
| 619 | + } |
| 620 | + |
| 621 | + /// <summary> |
| 622 | + /// Waits for the dependency resource to enter the Exited or Finished state before starting the resource. |
| 623 | + /// </summary> |
| 624 | + /// <typeparam name="T">The type of the resource.</typeparam> |
| 625 | + /// <param name="builder">The resource builder for the resource that will be waiting.</param> |
| 626 | + /// <param name="dependency">The resource builder for the dependency resource.</param> |
| 627 | + /// <param name="exitCode">The exit code which is interpretted as successful.</param> |
| 628 | + /// <returns>The resource builder.</returns> |
| 629 | + /// <remarks> |
| 630 | + /// <para>This method is useful when a resource should wait until another has completed. A common usage pattern |
| 631 | + /// would be to include a console application that initializes the database schema or performs other one off |
| 632 | + /// initialization tasks.</para> |
| 633 | + /// <para>Note that this method has no impact at deployment time and only works for local development.</para> |
| 634 | + /// </remarks> |
| 635 | + /// <example> |
| 636 | + /// Wait for database initialization app to complete running. |
| 637 | + /// <code lang="C#"> |
| 638 | + /// var builder = DistributedApplication.CreateBuilder(args); |
| 639 | + /// var pgsql = builder.AddPostgres("postgres"); |
| 640 | + /// var dbprep = builder.AddProject<Projects.DbPrepApp>("dbprep") |
| 641 | + /// .WithReference(pgsql); |
| 642 | + /// builder.AddProject<Projects.DatabasePrepTool>("dbprep") |
| 643 | + /// .WithReference(pgsql) |
| 644 | + /// .WaitForCompletion(dbprep); |
| 645 | + /// </code> |
| 646 | + /// </example> |
| 647 | + public static IResourceBuilder<T> WaitForCompletion<T>(this IResourceBuilder<T> builder, IResourceBuilder<IResource> dependency, int exitCode = 0) where T : IResource |
| 648 | + { |
| 649 | + builder.ApplicationBuilder.Eventing.Subscribe<BeforeResourceStartedEvent>(builder.Resource, async (e, ct) => |
| 650 | + { |
| 651 | + if (dependency.Resource.TryGetLastAnnotation<ReplicaAnnotation>(out var replicaAnnotation) && replicaAnnotation.Replicas > 1) |
| 652 | + { |
| 653 | + throw new DistributedApplicationException("WaitForCompletion cannot be used with resources that have replicas."); |
| 654 | + } |
| 655 | + |
| 656 | + var rls = e.Services.GetRequiredService<ResourceLoggerService>(); |
| 657 | + var resourceLogger = rls.GetLogger(builder.Resource); |
| 658 | + resourceLogger.LogInformation($"Waiting for resource '{dependency.Resource.Name}' to complete."); |
| 659 | + |
| 660 | + var rns = e.Services.GetRequiredService<ResourceNotificationService>(); |
| 661 | + await rns.PublishUpdateAsync(builder.Resource, s => s with { State = KnownResourceStates.Waiting }).ConfigureAwait(false); |
| 662 | + var resourceEvent = await rns.WaitForResourceAsync(dependency.Resource.Name, re => IsKnownTerminalState(re.Snapshot), cancellationToken: ct).ConfigureAwait(false); |
| 663 | + var snapshot = resourceEvent.Snapshot; |
| 664 | + |
| 665 | + if (snapshot.State == KnownResourceStates.FailedToStart) |
| 666 | + { |
| 667 | + resourceLogger.LogError( |
| 668 | + "Dependency resource '{ResourceName}' failed to start.", |
| 669 | + dependency.Resource.Name |
| 670 | + ); |
| 671 | + |
| 672 | + throw new DistributedApplicationException($"Dependency resource '{dependency.Resource.Name}' failed to start."); |
| 673 | + } |
| 674 | + else if ((snapshot.State!.Text == KnownResourceStates.Finished || snapshot.State!.Text == KnownResourceStates.Exited) && snapshot.ExitCode != exitCode) |
| 675 | + { |
| 676 | + resourceLogger.LogError( |
| 677 | + "Resource '{ResourceName}' has entered the '{State}' state with exit code '{ExitCode}'", |
| 678 | + dependency.Resource.Name, |
| 679 | + snapshot.State.Text, |
| 680 | + snapshot.ExitCode |
| 681 | + ); |
| 682 | + |
| 683 | + throw new DistributedApplicationException( |
| 684 | + $"Resource '{dependency.Resource.Name}' has entered the '{snapshot.State.Text}' state with exit code '{snapshot.ExitCode}'" |
| 685 | + ); |
| 686 | + } |
| 687 | + }); |
| 688 | + |
| 689 | + return builder; |
| 690 | + |
| 691 | + static bool IsKnownTerminalState(CustomResourceSnapshot snapshot) => |
| 692 | + KnownResourceStates.TerminalStates.Contains(snapshot.State?.Text) && |
| 693 | + snapshot.ExitCode is not null; |
| 694 | + } |
552 | 695 | }
|
0 commit comments