elsa-core/src/runtime/Elsa.Runtime.ProtoActor/Extensions/ServiceCollectionExtensions.cs

97 lines
3.9 KiB
C#
Raw Normal View History

2022-01-04 08:42:12 +00:00
using System;
using Elsa.Helpers;
using Elsa.Persistence.Models;
2022-01-04 08:42:12 +00:00
using Elsa.Runtime.Contracts;
using Elsa.Runtime.Extensions;
using Elsa.Runtime.ProtoActor.Grains;
2022-01-04 08:42:12 +00:00
using Elsa.Runtime.ProtoActor.HostedServices;
using Elsa.Runtime.ProtoActor.Services;
using Elsa.Runtime.Protos;
2022-01-04 08:42:12 +00:00
using Microsoft.Extensions.DependencyInjection;
using Proto;
using Proto.Cluster;
using Proto.Cluster.Partition;
using Proto.Cluster.Testing;
using Proto.DependencyInjection;
using Proto.Remote;
using Proto.Remote.GrpcCore;
namespace Elsa.Runtime.ProtoActor.Extensions;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddProtoActorWorkflowHost(this IServiceCollection services)
{
var systemConfig = GetSystemConfig();
// Actor System.
2022-01-04 08:42:12 +00:00
services.AddSingleton(sp =>
{
var system = new ActorSystem(systemConfig).WithServiceProvider(sp);
var remoteConfig = GetRemoteConfig();
var clusterConfig = GetClusterConfig(system, "my-cluster");
2022-01-04 08:42:12 +00:00
system
.WithRemote(remoteConfig)
.WithCluster(clusterConfig);
return system;
});
// Cluster.
2022-01-04 08:42:12 +00:00
services.AddSingleton(sp => sp.GetRequiredService<ActorSystem>().Cluster());
// Actors.
services
.AddSingleton(sp => new WorkflowDefinitionGrainActor((context, _) => ActivatorUtilities.CreateInstance<WorkflowDefinitionGrain>(sp, context)))
.AddSingleton(sp => new WorkflowInstanceGrainActor((context, _) => ActivatorUtilities.CreateInstance<WorkflowInstanceGrain>(sp, context)));
// Client factory.
services.AddSingleton<GrainClientFactory>();
// Configure runtime with ProtoActor workflow invoker.
services.ConfigureWorkflowRuntime(options => options.WorkflowInvokerFactory = sp => ActivatorUtilities.CreateInstance<ProtoActorWorkflowInvoker>(sp));
2022-01-04 08:42:12 +00:00
return services
.AddHostedService<WorkflowServerHost>();
2022-01-04 08:42:12 +00:00
}
private static ActorSystemConfig GetSystemConfig() =>
2022-01-04 08:42:12 +00:00
ActorSystemConfig
.Setup()
.WithDeadLetterThrottleCount(3)
2022-01-04 13:31:44 +00:00
.WithDeadLetterThrottleInterval(TimeSpan.FromSeconds(10000))
2022-01-04 08:42:12 +00:00
.WithDeveloperSupervisionLogging(true)
.WithDeadLetterRequestLogging(true);
2022-01-04 08:42:12 +00:00
private static GrpcCoreRemoteConfig GetRemoteConfig() => GrpcCoreRemoteConfig
.BindToLocalhost()
.WithProtoMessages(Protos.MessagesReflection.Descriptor);
2022-01-04 08:42:12 +00:00
private static ClusterConfig GetClusterConfig(ActorSystem system, string clusterName)
{
//var clusterProvider = new ConsulProvider(new ConsulProviderConfig{});
var clusterProvider = new TestProvider(new TestProviderOptions(), new InMemAgent());
var workflowDefinitionProps = system.DI().PropsFor<WorkflowDefinitionGrainActor>();
var workflowInstanceProps = system.DI().PropsFor<WorkflowInstanceGrainActor>();
2022-01-04 08:42:12 +00:00
var clusterConfig =
ClusterConfig
// .Setup("MyCluster", clusterProvider, new IdentityStorageLookup(GetIdentityLookup(clusterName)))
.Setup(clusterName, clusterProvider, new PartitionIdentityLookup())
2022-01-04 13:31:44 +00:00
.WithTimeout(TimeSpan.FromHours(1))
.WithActorRequestTimeout(TimeSpan.FromHours(1))
.WithActorActivationTimeout(TimeSpan.FromHours(1))
.WithActorSpawnTimeout(TimeSpan.FromHours(1))
.WithClusterKind(WorkflowDefinitionGrainActor.Kind, workflowDefinitionProps)
.WithClusterKind(WorkflowInstanceGrainActor.Kind, workflowInstanceProps)
2022-01-04 08:42:12 +00:00
;
return clusterConfig;
}
2022-01-04 08:42:12 +00:00
// private static IIdentityStorage GetIdentityLookup(string clusterName) =>
// new RedisIdentityStorage(clusterName, ConnectionMultiplexer
// .Connect("localhost:6379" /* use proper config */)
// );
}