private static ActorSystemConfig GetSystemConfig() => ActorSystemConfig .Setup() .WithDeadLetterThrottleCount(3) .WithDeadLetterThrottleInterval(TimeSpan.FromSeconds(1)) .WithDeveloperSupervisionLogging(true);
/// <summary> /// Configures the <see cref="ActorSystem"/> service in the <see cref="ServiceCollection"/>. /// </summary> /// <param name="builder">The <see cref="IHostBuilder"/>.</param> /// <returns>The builder for continuation.</returns> public static IHostBuilder AddActorSystem(this IHostBuilder builder) => builder.ConfigureServices(_ => _ .AddSingleton(provider => new ActorSystem(ActorSystemConfig.Setup()) .WithServiceProvider(provider) .WithRemote(GrpcNetRemoteConfig .BindToLocalhost() .WithProtoMessages(provider.GetRequiredService <IProtobufFileDescriptors>().All.ToArray())) .WithCluster(ClusterConfig.Setup( "Dolittle", new TestProvider(new TestProviderOptions(), new InMemAgent()), new PartitionIdentityLookup()) .WithDiscoveredClusterKinds(provider))) .AddSingleton(provider => provider.GetRequiredService <ActorSystem>().Root.WithTracing()) .AddSingleton(provider => provider.GetRequiredService <ActorSystem>().Cluster()));
public static void Run() { var l = LoggerFactory.Create(x => x.AddConsole().SetMinimumLevel(LogLevel.Information)); Log.SetLoggerFactory(l); var config = ActorSystemConfig.Setup().WithMetricsProviders(new PrometheusConfigurator()); var remoteConfig = GrpcCoreRemoteConfig .BindToLocalhost() .WithProtoMessages(MessagesReflection.Descriptor); var clusterConfig = ClusterConfig .Setup("MyCluster", new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://127.0.0.1:8500/")), new PartitionIdentityLookup() ); var system = new ActorSystem(config) .WithRemote(remoteConfig) .WithCluster(clusterConfig); system .Cluster() .StartMemberAsync(); var props = Props.FromProducer(() => new MyActor()); var config2 = ActorSystemConfig.Setup().WithMetricsProviders(new PrometheusConfigurator()); var remoteConfig2 = GrpcCoreRemoteConfig .BindToLocalhost() .WithProtoMessages(MessagesReflection.Descriptor); var clusterConfig2 = ClusterConfig .Setup("MyCluster", new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://127.0.0.1:8500/")), new PartitionIdentityLookup() ) .WithClusterKind("somekind", props); var system2 = new ActorSystem(config2) .WithRemote(remoteConfig2) .WithCluster(clusterConfig2); system2 .Cluster() .StartMemberAsync(); _ = SafeTask.Run(async() => { var r = new Random(); while (true) { await Task.Delay(r.Next(1, 2000)); await system.Cluster().RequestAsync <SomeResponse>($"someactor{r.Next(1, 100)}", "somekind", new SomeRequest(), CancellationTokens.WithTimeout(5000) ); } } ); }
public static IHostBuilder UseProtoActor(this IHostBuilder host, Func <ActorSystemConfig, ActorSystemConfig> configFunc, Func <ActorSystem, ActorSystem> sysFunc, ProtoActorHostedServiceStart akkaHostedServiceStart) { host.ConfigureServices((context, services) => { services.AddSingleton(akkaHostedServiceStart); services.AddSingleton(sp => sysFunc(new ActorSystem(configFunc?.Invoke(ActorSystemConfig.Setup())) .WithServiceProvider(sp))); services.AddSingleton(typeof(IPropsFactory <>), typeof(PropsFactory <>)); services.AddHostedService <ProtoActorHostedService>(); services.AddSingleton(sp => (IRootContext) new RootContext(sp.GetService <ActorSystem>())); }); return(host); }
public async Task <Cluster> CreateCluster() { try { var actorSystemConfig = ActorSystemConfig.Setup(); if (_metricsProvider != null) { actorSystemConfig = actorSystemConfig .WithMetricsProviders(_metricsProvider); } var system = new ActorSystem(actorSystemConfig); _logger.LogInformation("Setting up Cluster"); _logger.LogInformation("ClusterName: " + _clusterSettings.ClusterName); _logger.LogInformation("PIDDatabaseName: " + _clusterSettings.PIDDatabaseName); _logger.LogInformation("PIDCollectionName: " + _clusterSettings.PIDCollectionName); var clusterProvider = _clusterProvider.CreateClusterProvider(_logger); //var identity = RedisIdentityLookup.GetIdentityLookup(_clusterSettings.ClusterName, _clusterSettings.Host, _clusterSettings.RedisPort); var identity = MongoIdentityLookup.GetIdentityLookup(_clusterSettings.ClusterName, _clusterSettings.PIDConnectionString, _clusterSettings.PIDCollectionName, _clusterSettings.PIDDatabaseName); var(clusterConfig, remoteConfig) = GenericClusterConfig.CreateClusterConfig(_clusterSettings, clusterProvider, identity, _descriptorProvider, _logger); if (_setupRootActors != null) { clusterConfig = _setupRootActors.AddRootActors(clusterConfig); } _ = new GrpcCoreRemote(system, remoteConfig); var cluster = new Cluster(system, clusterConfig); await cluster.StartMemberAsync().ConfigureAwait(false); if (this._subscriptionFactory != null) { _logger.LogInformation("Fire up subscriptions for system {id} {address}", system.Id, system.Address); await this._subscriptionFactory.FireUp(system).ConfigureAwait(false); } _ = SafeTask.Run(async() => { try { int counter = 0; while (!_cancellationTokenSource.IsCancellationRequested) { Member[] members = cluster.MemberList.GetAllMembers(); string[] clusterKinds = cluster.GetClusterKinds(); if (clusterKinds.Length == 0) { _logger.LogInformation("[SharedClusterWorker] clusterKinds {clusterKinds}", clusterKinds.Length); _logger.LogInformation("[SharedClusterWorker] Restarting"); _ = this.RestartMe(); break; } this.Connected = members.Length > 0; if (!this.Connected) { counter = 0; _logger.LogInformation("[SharedClusterWorker] Connected {Connected}", this.Connected); } if (this.Connected) { if (counter % 20 == 0) { _logger.LogDebug("[SharedClusterWorker] Members {@Members}", members.Select(m => m.ToLogString())); } counter++; } await Task.Delay(500); } } catch { // ignored } }, _cancellationTokenSource.Token); return(cluster); } catch (Exception ex) { _logger.LogError(ex, "SharedClusterWork failed"); throw; } }