Esempio n. 1
0
        private static void ConfigSiloBuilder(ISiloBuilder siloBuilder,
                                              SiloConfigOption siloConfig, OrleansProviderOption orleansProvider, GrainLoadOption grainLoadOption, OrleansDashboardOption orleansDashboard)
        {
            if (orleansDashboard.Enable)
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    siloBuilder.UseLinuxEnvironmentStatistics();
                }

                Log.Information($"Enable Orleans Dashboard (https://github.com/OrleansContrib/OrleansDashboard) on this host {orleansDashboard.Port} port");
                siloBuilder.UseDashboard(options =>
                {
                    options.Port = orleansDashboard.Port;
                });
            }

            if (!string.IsNullOrEmpty(siloConfig.SiloName))
            {
                siloBuilder.Configure <SiloOptions>(options => { options.SiloName = siloConfig.SiloName; });
            }

            siloBuilder.Configure <SiloMessagingOptions>(options =>
            {
                options.ResponseTimeout             = TimeSpan.FromMinutes(siloConfig.ResponseTimeoutMinutes);
                options.ResponseTimeoutWithDebugger = TimeSpan.FromMinutes(siloConfig.ResponseTimeoutMinutes + 60);
            }).Configure <ClusterOptions>(options =>
            {
                options.ClusterId = siloConfig.ClusterId;
                options.ServiceId = siloConfig.ServiceId;
            });

            if (!string.IsNullOrEmpty(siloConfig.AzureApplicationInsightKey))
            {
                siloBuilder.AddApplicationInsightsTelemetryConsumer(siloConfig.AzureApplicationInsightKey);
            }

            if (siloConfig.IsMultiCluster.HasValue && siloConfig.IsMultiCluster.Value)
            {
                if (siloConfig.GossipChannels == null || siloConfig.GossipChannels.Any())
                {
                    throw new OrleansSiloHostConfigException("Gossip Channels configuration value(s) required.");
                }

                siloBuilder.Configure <MultiClusterOptions>(options =>
                {
                    options.HasMultiClusterNetwork = true;

                    if (siloConfig.DefaultMultiCluster != null && siloConfig.DefaultMultiCluster.Any())
                    {
                        options.DefaultMultiCluster = siloConfig.DefaultMultiCluster;
                    }
                    else
                    {
                        options.DefaultMultiCluster.Add(siloConfig.ClusterId);
                    }

                    options.GossipChannels = siloConfig.GossipChannels;
                });
            }

            string pathResolver(string path)
            {
                if (!path.Contains("{GrainLoadPath}"))
                {
                    return(Path.GetFullPath(path, AssemblyUtil.GetCurrentAssemblyPath()));
                }

                var loadPathStr = Environment.GetEnvironmentVariable("GrainLoadPath");

                if (string.IsNullOrEmpty(loadPathStr))
                {
                    return(Path.GetFullPath(path, AssemblyUtil.GetCurrentAssemblyPath()));
                }
                var expendedPathStr = path.Replace("{GrainLoadPath}", loadPathStr);

                var ret = expendedPathStr;

                var envCwd = Environment.CurrentDirectory;
                var cwd    = Directory.GetCurrentDirectory();

                if (!Path.IsPathRooted(expendedPathStr))
                {
                    ret = !string.IsNullOrEmpty(envCwd)
                        ? Path.GetFullPath(expendedPathStr, envCwd)
                        : Path.GetFullPath(ret, cwd);
                }

                return(ret);
            }

            siloBuilder.ConfigureApplicationParts(parts =>
            {
                parts.AddFromApplicationBaseDirectory().WithReferences();

                var dllPaths = grainLoadOption.LoadPaths;

                ConfigOtherFolderGrainLoad(parts, dllPaths, pathResolver);
            });

            foreach (var serviceConfigAction in GetGrainServiceConfigurationAction(grainLoadOption, pathResolver))
            {
                Log.Information($"Configure DI using {serviceConfigAction}");

                siloBuilder.ConfigureApplicationParts(serviceConfigAction.AppPartConfigurationAction);
                siloBuilder.ConfigureServices(serviceConfigAction.ServiceConfigurationAction);
            }

            if (IpAddressNotSpecified(siloConfig.AdvertisedIp))
            {
                siloBuilder.ConfigureEndpoints(siloConfig.SiloPort, siloConfig.GatewayPort,
                                               listenOnAnyHostAddress: siloConfig.ListenOnAnyHostAddress);
            }
            else
            {
                var advertisedIp = IPAddress.Parse(siloConfig.AdvertisedIp.Trim());
                siloBuilder.ConfigureEndpoints(advertisedIp, siloConfig.SiloPort, siloConfig.GatewayPort,
                                               siloConfig.ListenOnAnyHostAddress);
            }

            switch (orleansProvider.DefaultProvider)
            {
            case "MongoDB":
                var mongoDbConfig = orleansProvider.MongoDB;
                siloBuilder.UseMongoDBClustering(options =>
                {
                    var cluster = mongoDbConfig.Cluster;

                    options.ConnectionString = cluster.DbConn;
                    options.DatabaseName     = cluster.DbName;

                    if (!string.IsNullOrEmpty(cluster.CollectionPrefix))
                    {
                        options.CollectionPrefix = cluster.CollectionPrefix;
                    }
                })
                .AddMongoDBGrainStorageAsDefault(optionsBuilder =>
                {
                    var storage = mongoDbConfig.Storage;
                    optionsBuilder.Configure(options =>
                    {
                        options.ConnectionString = storage.DbConn;
                        options.DatabaseName     = storage.DbName;

                        if (!string.IsNullOrEmpty(storage.CollectionPrefix))
                        {
                            options.CollectionPrefix = storage.CollectionPrefix;
                        }
                    });
                })
                .UseMongoDBReminders(options =>
                {
                    var reminder = mongoDbConfig.Reminder;

                    options.ConnectionString = reminder.DbConn;
                    options.DatabaseName     = reminder.DbName;

                    if (!string.IsNullOrEmpty(reminder.CollectionPrefix))
                    {
                        options.CollectionPrefix = reminder.CollectionPrefix;
                    }
                });
                break;

            default:
                siloBuilder.UseLocalhostClustering().UseInMemoryReminderService();
                break;
            }
        }