Пример #1
0
        public SiloWrapper(IConfiguration config, ISemanticLog log)
        {
            this.log = log;

            silo = new Lazy <ISiloHost>(() =>
            {
                J.Serializer = SerializationServices.DefaultJsonSerializer;

                var hostBuilder = new SiloHostBuilder()
                                  .UseDashboard(options => options.HostSelf = true)
                                  .AddStartupTask <Bootstrap <IContentSchedulerGrain> >()
                                  .AddStartupTask <Bootstrap <IEventConsumerManagerGrain> >()
                                  .AddStartupTask <Bootstrap <IRuleDequeuerGrain> >()
                                  .AddStartupTask((services, ct) =>
                {
                    services.RunInitialization();

                    return(TaskHelper.Done);
                })
                                  .Configure <ClusterOptions>(options =>
                {
                    options.ClusterId = "squidex";
                })
                                  .ConfigureLogging((hostingContext, builder) =>
                {
                    builder.AddConfiguration(hostingContext.Configuration.GetSection("logging"));
                    builder.AddSemanticLog();
                    builder.AddFilter("Orleans.Runtime.SiloControl", LogLevel.Warning);
                })
                                  .ConfigureApplicationParts(builder =>
                {
                    builder.AddApplicationPart(SquidexEntities.Assembly);
                    builder.AddApplicationPart(SquidexInfrastructure.Assembly);
                })
                                  .ConfigureServices((context, services) =>
                {
                    services.AddAppSiloServices(context.Configuration);
                    services.AddAppServices(context.Configuration);

                    services.Configure <ProcessExitHandlingOptions>(options => options.FastKillOnProcessExit = false);
                })
                                  .ConfigureAppConfiguration((hostContext, builder) =>
                {
                    if (config is IConfigurationRoot root)
                    {
                        foreach (var provider in root.Providers)
                        {
                            builder.Add(new Source(provider));
                        }
                    }
                });

                config.ConfigureByOption("orleans:clustering", new Options
                {
                    ["MongoDB"] = () =>
                    {
                        hostBuilder.ConfigureEndpoints(Dns.GetHostName(), 11111, 40000, listenOnAnyHostAddress: true);

                        var mongoConfiguration = config.GetRequiredValue("store:mongoDb:configuration");
                        var mongoDatabaseName  = config.GetRequiredValue("store:mongoDb:database");

                        hostBuilder.UseMongoDBClustering(options =>
                        {
                            options.ConnectionString = mongoConfiguration;
                            options.CollectionPrefix = "Orleans_";
                            options.DatabaseName     = mongoDatabaseName;
                        });
                    },
                    ["Development"] = () =>
                    {
                        hostBuilder.UseLocalhostClustering(gatewayPort: 40000, clusterId: "squidex");
                        hostBuilder.Configure <ClusterMembershipOptions>(options => options.ExpectedClusterSize = 1);
                    }
                });

                config.ConfigureByOption("store:type", new Options
                {
                    ["MongoDB"] = () =>
                    {
                        var mongoConfiguration = config.GetRequiredValue("store:mongoDb:configuration");
                        var mongoDatabaseName  = config.GetRequiredValue("store:mongoDb:database");

                        hostBuilder.UseMongoDBReminders(options =>
                        {
                            options.ConnectionString = mongoConfiguration;
                            options.CollectionPrefix = "Orleans_";
                            options.DatabaseName     = mongoDatabaseName;
                        });
                    }
                });

                return(hostBuilder.Build());
            });
        }
Пример #2
0
        public SiloWrapper(IConfiguration config, ISemanticLog log, IApplicationLifetime lifetime)
        {
            this.lifetime = lifetime;
            this.log      = log;

            lazySilo = new Lazy <ISiloHost>(() =>
            {
                var hostBuilder = new SiloHostBuilder()
                                  .UseDashboard(options => options.HostSelf = false)
                                  .EnableDirectClient()
                                  .AddIncomingGrainCallFilter <LocalCacheFilter>()
                                  .AddStartupTask <InitializerStartup>()
                                  .AddStartupTask <Bootstrap <IContentSchedulerGrain> >()
                                  .AddStartupTask <Bootstrap <IEventConsumerManagerGrain> >()
                                  .AddStartupTask <Bootstrap <IRuleDequeuerGrain> >()
                                  .AddStartupTask <Bootstrap <IUsageTrackerGrain> >()
                                  .Configure <ClusterOptions>(options =>
                {
                    options.Configure();
                })
                                  .ConfigureApplicationParts(builder =>
                {
                    builder.AddMyParts();
                })
                                  .ConfigureLogging((hostingContext, builder) =>
                {
                    builder.AddConfiguration(hostingContext.Configuration.GetSection("logging"));
                    builder.AddSemanticLog();
                    builder.AddFilter();
                })
                                  .ConfigureServices((context, services) =>
                {
                    services.AddAppSiloServices(context.Configuration);
                    services.AddAppServices(context.Configuration);

                    services.Configure <ProcessExitHandlingOptions>(options => options.FastKillOnProcessExit = false);
                })
                                  .ConfigureAppConfiguration((hostContext, builder) =>
                {
                    if (config is IConfigurationRoot root)
                    {
                        foreach (var provider in root.Providers)
                        {
                            builder.Add(new Source(provider));
                        }
                    }
                });

                config.ConfigureByOption("orleans:clustering", new Options
                {
                    ["MongoDB"] = () =>
                    {
                        hostBuilder.ConfigureEndpoints(Dns.GetHostName(), 11111, 40000, listenOnAnyHostAddress: true);

                        var mongoConfiguration = config.GetRequiredValue("store:mongoDb:configuration");
                        var mongoDatabaseName  = config.GetRequiredValue("store:mongoDb:database");

                        hostBuilder.UseMongoDBClustering(options =>
                        {
                            options.ConnectionString = mongoConfiguration;
                            options.CollectionPrefix = "Orleans_";
                            options.DatabaseName     = mongoDatabaseName;
                        });
                    },
                    ["Development"] = () =>
                    {
                        hostBuilder.UseLocalhostClustering(gatewayPort: 40000, serviceId: Constants.OrleansClusterId, clusterId: Constants.OrleansClusterId);
                        hostBuilder.Configure <ClusterMembershipOptions>(options => options.ExpectedClusterSize = 1);
                    }
                });

                config.ConfigureByOption("store:type", new Options
                {
                    ["MongoDB"] = () =>
                    {
                        var mongoConfiguration = config.GetRequiredValue("store:mongoDb:configuration");
                        var mongoDatabaseName  = config.GetRequiredValue("store:mongoDb:database");

                        hostBuilder.UseMongoDBReminders(options =>
                        {
                            options.ConnectionString = mongoConfiguration;
                            options.CollectionPrefix = "Orleans_";
                            options.DatabaseName     = mongoDatabaseName;
                        });
                    }
                });

                var silo = hostBuilder.Build();

                silo.Stopped.ContinueWith(x =>
                {
                    if (!isStopping)
                    {
                        lifetime.StopApplication();
                    }
                });

                return(silo);
            });
        }
        private ISiloHost CreateSiloHost()
        {
            var builder = new SiloHostBuilder();

            if (_dashboardOptions.Value.Enable)
            {
                builder.UseDashboard(options =>
                {
                    options.Port = _dashboardOptions.Value.Port;
                });
            }

            builder.Configure <ClusterOptions>(options =>
            {
                options.ClusterId = _siloOptions.Value.ClusterId;
                options.ServiceId = _siloOptions.Value.ServiceId;
            });

            if (string.IsNullOrEmpty(_siloOptions.Value.AdvertisedIp) || "*".Equals(_siloOptions.Value.AdvertisedIp))
            {
                builder.ConfigureEndpoints(_siloOptions.Value.SiloPort, _siloOptions.Value.GatewayPort,
                                           listenOnAnyHostAddress: _siloOptions.Value.ListenOnAnyHostAddress);
            }
            else
            {
                var ip = IPAddress.Parse(_siloOptions.Value.AdvertisedIp);

                builder.ConfigureEndpoints(ip, _siloOptions.Value.SiloPort, _siloOptions.Value.GatewayPort,
                                           listenOnAnyHostAddress: _siloOptions.Value.ListenOnAnyHostAddress);
            }

            if (_providerOptions.Value.DefaultProvider == "MongoDB")
            {
                var mongoDbOption = _providerOptions.Value.MongoDB;
                builder.UseMongoDBClustering(options =>
                {
                    var clusterOption = mongoDbOption.Cluster;

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

                    // see:https://github.com/OrleansContrib/Orleans.Providers.MongoDB/issues/54
                    options.CollectionPrefix = clusterOption.CollectionPrefix;
                })
                .UseMongoDBReminders(options =>
                {
                    var reminderOption = mongoDbOption.Reminder;

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

                    if (!string.IsNullOrEmpty(reminderOption.CollectionPrefix))
                    {
                        options.CollectionPrefix = reminderOption.CollectionPrefix;
                    }
                })
                .AddMongoDBGrainStorageAsDefault(options =>
                {
                    var storageOption = mongoDbOption.Storage;

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

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

            builder.ConfigureServices(services =>
            {
                services
                .AddLogging(loggingBuilder => loggingBuilder.AddSerilog())
                .AddTransient <GameHost>()
                .AddTransient <LeaderBoard>()
                .AddTransient <Player>();
            })
            .ConfigureApplicationParts(parts => { parts.AddFromApplicationBaseDirectory().WithReferences(); })
            .ConfigureLogging(logging => { logging.AddSerilog(dispose: true); });

            return(builder.Build());
        }
Пример #4
0
        public static IServiceProvider AddAndBuildOrleans(this IServiceCollection services, IConfiguration config, Action <IServiceCollection> afterServices)
        {
            services.Configure <ClusterOptions>(options =>
            {
                options.Configure();
            });

            services.Configure <ProcessExitHandlingOptions>(options =>
            {
                options.FastKillOnProcessExit = false;
            });

            services.AddServicesForSelfHostedDashboard(null, options =>
            {
                options.HideTrace = true;
            });

            services.AddHostedService <SiloHost>();

            var hostBuilder = new SiloHostBuilder()
                              .UseDashboardEx()
                              .EnableDirectClient()
                              .AddIncomingGrainCallFilter <LocalCacheFilter>()
                              .AddStartupTask <Bootstrap <IContentSchedulerGrain> >()
                              .AddStartupTask <Bootstrap <IEventConsumerManagerGrain> >()
                              .AddStartupTask <Bootstrap <IRuleDequeuerGrain> >()
                              .AddStartupTask <Bootstrap <IUsageTrackerGrain> >()
                              .ConfigureApplicationParts(builder =>
            {
                builder.AddMyParts();
            });

            var gatewayPort = config.GetOptionalValue("orleans:gatewayPort", 40000);

            var siloPort = config.GetOptionalValue("orleans:siloPort", 11111);

            config.ConfigureByOption("orleans:clustering", new Options
            {
                ["MongoDB"] = () =>
                {
                    hostBuilder.ConfigureEndpoints(Dns.GetHostName(), siloPort, gatewayPort, listenOnAnyHostAddress: true);

                    var mongoConfiguration = config.GetRequiredValue("store:mongoDb:configuration");
                    var mongoDatabaseName  = config.GetRequiredValue("store:mongoDb:database");

                    hostBuilder.UseMongoDBClustering(options =>
                    {
                        options.ConnectionString = mongoConfiguration;
                        options.CollectionPrefix = "Orleans_";
                        options.DatabaseName     = mongoDatabaseName;
                    });
                },
                ["Development"] = () =>
                {
                    hostBuilder.UseLocalhostClustering(siloPort, gatewayPort, null, Constants.OrleansClusterId, Constants.OrleansClusterId);
                    hostBuilder.Configure <ClusterMembershipOptions>(options => options.ExpectedClusterSize = 1);
                }
            });

            config.ConfigureByOption("store:type", new Options
            {
                ["MongoDB"] = () =>
                {
                    var mongoConfiguration = config.GetRequiredValue("store:mongoDb:configuration");
                    var mongoDatabaseName  = config.GetRequiredValue("store:mongoDb:database");

                    hostBuilder.UseMongoDBReminders(options =>
                    {
                        options.ConnectionString = mongoConfiguration;
                        options.CollectionPrefix = "Orleans_";
                        options.DatabaseName     = mongoDatabaseName;
                    });
                }
            });

            IServiceProvider provider = null;

            hostBuilder.UseServiceProviderFactory((siloServices) =>
            {
                foreach (var descriptor in services)
                {
                    siloServices.Add(descriptor);
                }

                afterServices(siloServices);

                provider = siloServices.BuildServiceProvider();

                return(provider);
            }).Build();

            return(provider);
        }