Esempio n. 1
0
        private ISiloHost AddClusteredSiloHost()
        {
            var silo = new SiloHostBuilder()
                       .Configure <ClusterOptions>(options =>
            {
                options.ClusterId = orleansConfig.ClusterId;
                options.ServiceId = orleansConfig.ServiceId;
            });


            if (string.IsNullOrEmpty(orleansConfig.DataConnectionString))
            {
                silo.AddMemoryGrainStorage("store");
            }
            else if (orleansConfig.DataConnectionString.Contains("6379") ||
                     orleansConfig.DataConnectionString.Contains("6380"))
            {
                silo.UseRedisMembership(options => options.ConnectionString            = orleansConfig.DataConnectionString);
                silo.AddRedisGrainStorage("store", options => options.ConnectionString = orleansConfig.DataConnectionString);
            }
            else
            {
                silo.UseAzureStorageClustering(options => options.ConnectionString         = orleansConfig.DataConnectionString);
                silo.AddAzureBlobGrainStorage("store", options => options.ConnectionString = orleansConfig.DataConnectionString);
            }

            silo.ConfigureEndpoints(siloPort: 11111, gatewayPort: 30000);

            LogLevel orleansLogLevel = Enum.Parse <LogLevel>(orleansConfig.LogLevel);
            var      loggers         = orleansConfig.GetLoggerTypes();

            silo.ConfigureLogging(builder =>
            {
                if (loggers.HasFlag(LoggerType.Console))
                {
                    builder.AddConsole();
                }
                if (loggers.HasFlag(LoggerType.Debug))
                {
                    builder.AddDebug();
                }
                builder.SetMinimumLevel(orleansLogLevel);
            });

            if (!string.IsNullOrEmpty(orleansConfig.AppInsightsKey))
            {
                silo.AddApplicationInsightsTelemetryConsumer(orleansConfig.AppInsightsKey);
            }

            return(silo.Build());
        }
        private ISiloHostBuilder InitBuilder()
        {
            var hostBuilder = new SiloHostBuilder();

            hostBuilder.Configure <SerializationProviderOptions>(options =>
            {
                options.SerializationProviders.Add(typeof(OrleansCustomSerialization));

                // A workaround for an Orleans issue
                // to ensure the stack trace properly de/serialized
                // Gigya.Microdot.UnitTests.Serialization.ExceptionSerializationTests
                options.SerializationProviders.Add(typeof(NonSerializedExceptionsSerializer));

                options.FallbackSerializationProvider = typeof(OrleansCustomSerialization);
            })
            .UsePerfCounterEnvironmentStatistics()
            // We paid attention that AddFromApplicationBaseDirectory making issues of non-discovering grain types.
            .ConfigureApplicationParts(parts => parts.AddFromAppDomain())
            .Configure <SiloOptions>(options => options.SiloName = _appInfo.Name);

            if (_orleansConfig.Dashboard.Enable)
            {
                hostBuilder.UseDashboard(o =>
                {
                    o.Port = _endPointDefinition.SiloDashboardPort;
                    o.CounterUpdateIntervalMs = (int)TimeSpan.Parse(_orleansConfig.Dashboard.WriteInterval).TotalMilliseconds;
                    o.HideTrace = _orleansConfig.Dashboard.HideTrace;
                });
            }

            SetGrainCollectionOptions(hostBuilder);

            hostBuilder.Configure <PerformanceTuningOptions>(options =>
            {
                options.DefaultConnectionLimit = ServicePointManager.DefaultConnectionLimit;
            });
            hostBuilder.AddMemoryGrainStorage(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME, options => options.NumStorageGrains = 10);
            hostBuilder.Configure <TelemetryOptions>(o => o.AddConsumer <MetricsStatisticsConsumer>());
            hostBuilder.Configure <SchedulingOptions>(options =>
            {
                options.PerformDeadlockDetection = true;
                options.AllowCallChainReentrancy = true;
                options.MaxActiveThreads         = Process.GetCurrentProcess().ProcessorAffinityList().Count();
            });

            hostBuilder.Configure <ClusterMembershipOptions>(options =>
            {
            });

            if (_orleansConfig.EnableTls)
            {
                var localCertificate = _certificateLocator.GetCertificate("Service");
                hostBuilder.UseTls(localCertificate, tlsOptions =>
                {
                    tlsOptions.LocalCertificate      = localCertificate;
                    tlsOptions.ClientCertificateMode = RemoteCertificateMode.AllowCertificate;
                    tlsOptions.RemoteCertificateMode = RemoteCertificateMode.AllowCertificate;

                    tlsOptions.SslProtocols = SslProtocols.Tls12;

                    tlsOptions.OnAuthenticateAsClient = OnAuthenticateAsClient;
                });
            }

            SetReminder(hostBuilder);
            SetSiloSource(hostBuilder);

            hostBuilder.Configure <StatisticsOptions>(o =>
            {
                o.LogWriteInterval          = TimeSpan.FromDays(1);
                o.PerfCountersWriteInterval = TimeSpan.Parse(_orleansConfig.MetricsTableWriteInterval);
            });

            return(hostBuilder);
        }
Esempio n. 3
0
        void BuildSiloHost()
        {
            var instrumentationKey           = _configuration.GetValue <string>("ApplicationInsights:InstrumentationKey");
            var hostname                     = _configuration.GetValue <string>("HOSTNAME");
            var azureStorageConnectionString = _configuration.GetValue <string>("Storage:AzureStorageConnectionString");
            var redisClusteringUrl           = _configuration.GetValue <string>("REDIS_URL");

            var builder = new SiloHostBuilder()
                          .ConfigureLogging(loggingBuilder =>
            {
                if (!string.IsNullOrEmpty(instrumentationKey))
                {
                    loggingBuilder.AddApplicationInsights(instrumentationKey);
                }
                loggingBuilder.AddConsole();
            })
                          // Configure ClusterId and ServiceId
                          .Configure <ClusterOptions>(options =>
            {
                options.ClusterId = "dev";
                options.ServiceId = "TwitchServices";
            })
                          .AddStartupTask <LoadConfigurationStartupTask>()
                          .ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(ChannelGrain).Assembly).WithReferences())
                          // Configure connectivity
                          .ConfigureEndpoints(hostname: hostname, siloPort: 11111, gatewayPort: 30000);

            if (!string.IsNullOrEmpty(azureStorageConnectionString))
            {
                builder.AddAzureTableGrainStorage("profileStore", (AzureTableStorageOptions options) =>
                {
                    options.ConnectionString = azureStorageConnectionString;
                    options.TableName        = "profiles";
                    options.UseJson          = true;
                    options.IndentJson       = false;
                });
                builder.AddAzureTableGrainStorage("channelStore", (AzureTableStorageOptions options) =>
                {
                    options.ConnectionString = azureStorageConnectionString;
                    options.TableName        = "channels";
                    options.UseJson          = true;
                    options.IndentJson       = false;
                });
                builder.AddAzureTableGrainStorage("botSettingsStore", (AzureTableStorageOptions options) =>
                {
                    options.ConnectionString = azureStorageConnectionString;
                    options.TableName        = "botsettings";
                    options.UseJson          = true;
                    options.IndentJson       = false;
                });
                builder.AddCustomCategoriesStorage("customCategoriesStore", (CustomCategoriesStorageOptions options) =>
                {
                    options.ConnectionString = azureStorageConnectionString;
                    options.TableName        = "customcategories";
                });
            }
            else
            {
                builder.AddMemoryGrainStorage("profileStore");
                builder.AddMemoryGrainStorage("channelStore");
                builder.AddMemoryGrainStorage("botSettingsStore");
                builder.AddMemoryGrainStorage("customCategoriesStore");
            }

            if (!string.IsNullOrEmpty(redisClusteringUrl))
            {
                // Use redis clustering when available
                builder.UseRedisClustering(redisClusteringUrl);
            }
            else
            {
                // Use localhost clustering for a single local silo
                builder.UseLocalhostClustering(11111, 30000, null, "TwitchServices", "dev");
            }

            // Temp

            builder.ConfigureServices((context, services) =>
            {
                // Load channels and command configuration from static json file, and inject
                var channelsConfig = new ConfigurationBuilder().AddJsonFile("channels.json").Build();
                IEnumerable <ChannelOptions> channelOptions = new List <ChannelOptions>();
                channelsConfig.GetSection("channels").Bind(channelOptions);
                services.AddTransient <IEnumerable <ChannelOptions> >((_) => channelOptions);

                // Configure services
                services.AddHttpClient();
                services.Configure <TwitchApplicationOptions>(_configuration.GetSection("twitch"));
                services.Configure <TwitchChatClientOptions>(_configuration.GetSection("twitch").GetSection("IrcOptions"));
                services.Configure <AzureGameLocalizationStoreOptions>(_configuration.GetSection("loc:azure"));
                services.AddSingleton <IMessageProcessor, TracingMessageProcessor>();
                services.AddTransient <TwitchChatClient>();
                services.AddTransient <TwitchAPIClient>();
                services.AddTransient <IGDBClient>();
                services.AddSingleton <IMemoryCache, MemoryCache>();
                services.AddSingleton <SteamStoreClient>();
                services.AddSingleton <IAuthenticated>(s =>
                                                       Twitch.Authenticate()
                                                       .FromAppCredentials(
                                                           s.GetService <IOptions <TwitchApplicationOptions> >().Value.ClientId,
                                                           s.GetService <IOptions <TwitchApplicationOptions> >().Value.ClientSecret)
                                                       .Build()
                                                       );
                services.AddTransient <ITwitchCategoryProvider, GrainTwitchCategoryProvider>();
                services.AddSingleton <IGameLocalizationStore, AzureStorageGameLocalizationStore>();
                services.PostConfigure <TwitchChatClientOptions>(options =>
                {
                    var oauth = Twitch.Authenticate()
                                .FromOAuthToken(options.OAuthToken)
                                .Build();
                    var loggerFactory = new LoggerFactory();
                    using (var httpClient = new HttpClient())
                        using (var apiClient = TwitchAPIClient.Create(oauth))
                        {
                            options.TokenInfo = apiClient.ValidateToken().Result;
                        }
                });

                // Configure commands
                services.AddCommand <GameSynopsisCommand>("GameSynopsis");
                services.AddCommand <TracingMessageProcessor>("Logger");
                services.AddCommand <ResponseCommandProcessor>("Response");
            });
            _siloHost = builder.Build();
        }