diff --git a/src/Akka.Hosting/AkkaConfigurationBuilder.cs b/src/Akka.Hosting/AkkaConfigurationBuilder.cs index 83adb37b..0baaab5f 100644 --- a/src/Akka.Hosting/AkkaConfigurationBuilder.cs +++ b/src/Akka.Hosting/AkkaConfigurationBuilder.cs @@ -52,6 +52,8 @@ public enum HoconAddMode /// public delegate Task ActorStarter(ActorSystem system, IActorRegistry registry); + public delegate Task StartupTask(ActorSystem system, IActorRegistry registry); + /// /// Used to help populate a upon starting the , /// if any are added to the builder; @@ -99,6 +101,7 @@ public sealed class AkkaConfigurationBuilder internal Option Sys { get; set; } = Option.None; private readonly HashSet _actorStarters = new HashSet(); + private readonly HashSet _startupTasks = new HashSet(); private bool _complete = false; public AkkaConfigurationBuilder(IServiceCollection serviceCollection, string actorSystemName) @@ -179,6 +182,17 @@ Task Starter(ActorSystem f, IActorRegistry registry) return Starter; } + + private static StartupTask ToAsyncStartup(Action nonAsyncStartup) + { + Task Startup(ActorSystem f, IActorRegistry registry) + { + nonAsyncStartup(f, registry); + return Task.CompletedTask; + } + + return Startup; + } public AkkaConfigurationBuilder StartActors(Action starter) { @@ -194,6 +208,34 @@ public AkkaConfigurationBuilder StartActors(ActorStarter starter) return this; } + /// + /// Adds a delegate that will be executed exactly once for application initialization + /// once the and all actors is started in this process. + /// + /// A delegate that will be run after all actors + /// have been instantiated. + /// The same instance originally passed in. + public AkkaConfigurationBuilder AddStartup(Action startupTask) + { + if (_complete) return this; + _startupTasks.Add(ToAsyncStartup(startupTask)); + return this; + } + + /// + /// Adds a delegate that will be executed exactly once for application initialization + /// once the and all actors is started in this process. + /// + /// A delegate that will be run after all actors + /// have been instantiated. + /// The same instance originally passed in. + public AkkaConfigurationBuilder AddStartup(StartupTask startupTask) + { + if (_complete) return this; + _startupTasks.Add(startupTask); + return this; + } + public AkkaConfigurationBuilder WithCustomSerializer( string serializerIdentifier, IEnumerable boundTypes, Func serializerFactory) @@ -366,6 +408,11 @@ internal async Task StartAsync(ActorSystem sys) await starter(sys, registry).ConfigureAwait(false); } + foreach (var startupTask in _startupTasks) + { + await startupTask(sys, registry).ConfigureAwait(false); + } + return sys; } }