protected override void OnStarted(IVostokHostingEnvironment hostingEnvironment) { hostingEnvironment.MetricScope.SystemMetrics(1.Minutes()); #if DEBUG /* Initialize EntityFramework Profiler. See https://www.hibernatingrhinos.com/products/efprof/learn for details */ HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize(); #endif }
private void SetupEventsLimitMetric(IVostokHostingEnvironment environment, Func <int?> limit) { if (environment.ApplicationReplicationInfo.InstanceIndex == 0) { environment.Metrics.Application.CreateFuncGauge("events", "type") .For("limit") .SetValueProvider(() => limit()); } }
public override Task WarmupAsync(IVostokHostingEnvironment environment, IServiceProvider provider) { #if DEBUG /* Initialize EntityFramework Profiler. See https://www.hibernatingrhinos.com/products/efprof/learn for details */ HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize(); #endif provider.GetService <WebsocketsEventSender>(); return(Task.CompletedTask); }
public Task InitializeAsync(IVostokHostingEnvironment environment) { SetupEventsLimitMetric(environment, () => environment.ConfigurationProvider.Get <AggregatorSettings>().EventsLimitMetric); var settings = environment.ConfigurationProvider.Get <AggregatorSettings>(); Func <string> apiKeyProvider = () => environment.SecretConfigurationProvider.Get <AggregatorSecretSettings>().HerculesApiKey; var binaryWriterSettings = new StreamBinaryWriterSettings( apiKeyProvider, new AdHocClusterProvider(() => null)) { MetricContext = environment.Metrics.Instance, GateClientAdditionalSetup = environment.HostExtensions.Get <ClusterClientSetup>(Constants.GateClientSetupKey) }; var binaryWriter = new StreamBinaryWriter(binaryWriterSettings, environment.Log); var eventsWriterSettings = new StreamBinaryEventsWriterSettings(binaryWriter, settings.TargetStream) { BufferCapacityLimit = settings.EventsWriteBufferCapacityLimit }; var eventsWriter = new StreamBinaryEventsWriter(eventsWriterSettings, environment.Log); var consumerSettings = new WindowedStreamConsumerSettings <MetricEvent, MetricTags>( settings.SourceStream, apiKeyProvider, new AdHocClusterProvider(() => null), s => s.Tags, s => s.Timestamp, _ => new MetricProcessor(environment.HostExtensions.Get <Func <IAggregateFunction> >(Constants.AggregateFunctionKey)(), eventsWriter), r => new HerculesMetricEventReader(r), environment.HostExtensions.Get <IStreamCoordinatesStorage>(Constants.LeftCoordinatesStorageKey), environment.HostExtensions.Get <IStreamCoordinatesStorage>(Constants.RightCoordinatesStorageKey), () => new StreamShardingSettings(environment.ApplicationReplicationInfo.InstanceIndex, environment.ApplicationReplicationInfo.InstancesCount) ) { EventsReadBatchSize = settings.EventsReadBatchSize, Lag = settings.Lag, Period = settings.Period, LagProvider = e => e.AggregationParameters?.GetAggregationLag(), PeriodProvider = e => e.AggregationParameters?.GetAggregationPeriod(), MetricContext = environment.Metrics.Instance, StreamApiClientAdditionalSetup = environment.HostExtensions.Get <ClusterClientSetup>(Constants.StreamClientSetupKey), MaximumDeltaAfterNow = settings.MaximumDeltaAfterNow, OnBatchBegin = _ => writeTask?.GetAwaiter().GetResult(), OnBatchEnd = _ => { writeTask = eventsWriter.WriteAsync().SilentlyContinue(); } }; consumer = new WindowedStreamConsumer <MetricEvent, MetricTags>(consumerSettings, environment.Log); return(Task.CompletedTask); }
private static void CheckPortInternal( Func <bool> requiresPort, IVostokHostingEnvironment environment, List <string> errors) { if (requiresPort() && environment.Port == null) { errors.Add("Application requires a port, which is not provided by host (see 'SetPort' extension when configuring hosting environment)."); } }
protected virtual void ConfigureServices(IServiceCollection services, IVostokHostingEnvironment hostingEnvironment) { services.AddLogging(builder => builder.AddVostok(hostingEnvironment.Log)); configuration = hostingEnvironment.SecretConfigurationProvider.Get <UlearnConfiguration>(hostingEnvironment.SecretConfigurationSource); services.Configure <UlearnConfiguration>(options => options.SetFrom(hostingEnvironment.SecretConfigurationProvider.Get <UlearnConfiguration>(hostingEnvironment.SecretConfigurationSource))); ConfigureDi(services); }
public static Task <IScheduledActionsRunner> InitializeAsync(IVostokHostingEnvironment environment, Action <IScheduledActionsBuilder, IVostokHostingEnvironment> setup) { return(InitializeAsync( environment, (builder, env) => { setup(builder, env); return Task.CompletedTask; })); }
public static async Task <IScheduledActionsRunner> InitializeAsync(IVostokHostingEnvironment environment, Func <IScheduledActionsBuilder, IVostokHostingEnvironment, Task> setup) { environment.HostExtensions.TryGet <IVostokApplicationDiagnostics>(out var diagnostics); var builder = new ScheduledActionsBuilder(environment.Log, environment.Tracer, diagnostics); await setup(builder, environment); return(builder.BuildRunner()); }
public async Task RunAsync(IVostokHostingEnvironment environment) { environment.Log.Info("Hello again from run!"); if (settings.CrashDuringRunning) { throw settings.CrashError; } await SleepAsync(environment, settings.RunSleep); }
protected override IWebHost BuildWebHost(IVostokHostingEnvironment hostingEnvironment) { var loggerConfiguration = new LoggerConfiguration() .Enrich.With <ThreadEnricher>() .Enrich.With <FlowContextEnricher>() .MinimumLevel.Debug() .WriteTo.Airlock(LogEventLevel.Information); if (hostingEnvironment.Log != null) { loggerConfiguration = loggerConfiguration.WriteTo.VostokLog(hostingEnvironment.Log, LogEventLevel.Information); } var logger = loggerConfiguration.CreateLogger(); return(new WebHostBuilder() .UseKestrel() .UseUrls($"http://*:{hostingEnvironment.Configuration["port"]}/") .AddVostokServices() .ConfigureServices(s => ConfigureServices(s, hostingEnvironment, logger)) .UseSerilog(logger) .Configure(app => { var env = app.ApplicationServices.GetRequiredService <IHostingEnvironment>(); app.UseVostok(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } /* Add CORS. Should be before app.UseMvc() */ ConfigureCors(app); /* Add exception handling policy. * See https://github.com/IharYakimush/asp-net-core-exception-handling */ app.UseExceptionHandlingPolicies(); app.UseAuthentication(); app.UseMvc(); /* Configure swagger documentation. Now it's available at /swagger/v1/swagger.json. * See https://github.com/domaindrivendev/Swashbuckle.AspNetCore for details */ app.UseSwagger(c => { c.RouteTemplate = "documentation/{documentName}/swagger.json"; }); /* And add swagger UI, available at /documentation */ app.UseSwaggerUI(c => { c.SwaggerEndpoint("/documentation/v1/swagger.json", "Ulearn API"); c.DocumentTitle = "UlearnApi"; c.RoutePrefix = "documentation"; }); ConfigureWebApplication(app); }) .Build()); }
public async Task InitializeAsync(IVostokHostingEnvironment environment) { environment.Log.Info("Hello from initialize!"); if (settings.CrashDuringInitializing) { throw settings.CrashError; } await SleepAsync(environment, settings.InitializeSleep); }
public VostokWebHostBuilder( IVostokHostingEnvironment environment, VostokKestrelBuilder kestrelBuilder, VostokMiddlewaresBuilder middlewaresBuilder) { this.environment = environment; this.kestrelBuilder = kestrelBuilder; this.middlewaresBuilder = middlewaresBuilder; webHostEnabled = true; webHostCustomization = new Customization <IWebHostBuilder>(); }
public VostokAspNetCoreApplicationBuilder(IVostokHostingEnvironment environment, IVostokApplication application, List <IDisposable> disposables) { this.environment = environment; hostFactory = new HostFactory(environment, application); hostFactory.SetupLogger(s => { s.IgnoredScopePrefixes = new[] { "Microsoft" }; }); kestrelBuilder = new VostokKestrelBuilder(); throttlingBuilder = new VostokThrottlingBuilder(environment, disposables); middlewaresBuilder = new VostokMiddlewaresBuilder(environment, disposables, throttlingBuilder); webHostBuilder = new VostokWebHostBuilder <TStartup>(environment, kestrelBuilder, middlewaresBuilder, disposables); }
public Task InitializeAsync(IVostokHostingEnvironment environment) { var builder = new ScheduledActionsBuilder(environment.Log); Setup(builder, environment); runner = builder.BuildRunnerInternal(); RegisterDiagnosticFeatures(environment); return(Task.CompletedTask); }
protected override void ConfigureServices(IServiceCollection services, IVostokHostingEnvironment hostingEnvironment, ILogger logger) { base.ConfigureServices(services, hostingEnvironment, logger); services.AddDbContext <AntiPlagiarismDb>( options => options.UseSqlServer(hostingEnvironment.Configuration["database"]) ); services.Configure <AntiPlagiarismConfiguration>(options => hostingEnvironment.Configuration.GetSection("antiplagiarism").Bind(options)); services.AddSwaggerExamplesFromAssemblyOf <WebApplication>(); }
public void Build(BuildContext context, IVostokHostingEnvironment environment) { this.environment = environment; if (context.ZooKeeperClient != null && !(context.ZooKeeperClient is DevNullZooKeeperClient)) { HostExtensions.Add(context.ZooKeeperClient); } builderCustomization.Customize(this); context.DisposableHostExtensions = disposable; }
protected override void ConfigureServices(IServiceCollection services, IVostokHostingEnvironment hostingEnvironment) { base.ConfigureServices(services, hostingEnvironment); services.AddDbContext <UlearnDb>( // AddDbContextPool: DbContext Pooling does not dispose LazyLoader https://github.com/dotnet/efcore/issues/11308 options => options .UseLazyLoadingProxies() .UseNpgsql(configuration.Database, o => o.SetPostgresVersion(13, 2)) ); services.Configure <NotificationsConfiguration>(options => options.SetFrom(hostingEnvironment.SecretConfigurationProvider.Get <NotificationsConfiguration>(hostingEnvironment.SecretConfigurationSource))); }
public async Task StartAsync(IVostokHostingEnvironment hostingEnvironment) { var webHost = BuildWebHost(hostingEnvironment); var applicationLifetime = webHost.Services.GetRequiredService <IApplicationLifetime>(); var tcs = new TaskCompletionSource <int>(); applicationLifetime.ApplicationStarted.Register(() => tcs.TrySetResult(0)); workTask = webHost.RunAsync(hostingEnvironment.ShutdownCancellationToken); OnStarted(hostingEnvironment); if (workTask == await Task.WhenAny(tcs.Task, workTask).ConfigureAwait(false)) { await workTask.ConfigureAwait(false); } }
public virtual async Task InitializeAsync(IVostokHostingEnvironment environment) { var log = environment.Log.ForContext <VostokNetCoreApplication>(); var hostBuilder = new GenericHostFactory(environment, this); var applicationBuilder = new VostokNetCoreApplicationBuilder(hostBuilder); Setup(applicationBuilder, environment); manager = new GenericHostManager(hostBuilder.CreateHost(), log); await manager.StartHostAsync(environment.ShutdownToken); }
protected override void SetupGlobal(IVostokAspNetCoreApplicationBuilder builder, IVostokHostingEnvironment env) { environment = env; environment.HostExtensions.TryGet(out diagnostics).Should().BeTrue(); #if ASPNETCORE3_1 builder.SetupGenericHost( host => host.ConfigureServices( (ctx, services) => { services.AddHealthChecks().AddCheck("ms", new MicrosoftHealthCheck()); })); #endif }
public override void Setup(IVostokAspNetCoreApplicationBuilder builder, IVostokHostingEnvironment hostingEnvironment) { builder.SetupWebHost(webHostBuilder => webHostBuilder .UseKestrel() .ConfigureServices(s => ConfigureServices(s, hostingEnvironment)) .UseEnvironment(hostingEnvironment.ApplicationIdentity.Environment) .Configure(app => { var env = app.ApplicationServices.GetRequiredService <IWebHostEnvironment>(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } /* Add CORS. Should be before app.UseMvc() */ ConfigureCors(app); /* Add exception handling policy. * See https://github.com/IharYakimush/asp-net-core-exception-handling */ app.UseExceptionHandlingPolicies(); app.UseAuthentication(); app.UseAuthorization(); app.UseMvc(); /* Configure swagger documentation. Now it's available at /swagger/v1/swagger.json. * See https://github.com/domaindrivendev/Swashbuckle.AspNetCore for details */ app.UseSwagger(c => { c.RouteTemplate = "documentation/{documentName}/swagger.json"; }); /* And add swagger UI, available at /documentation */ app.UseSwaggerUI(c => { c.SwaggerEndpoint("/documentation/v1/swagger.json", "Ulearn API"); c.DocumentTitle = "UlearnApi"; c.RoutePrefix = "documentation"; }); ConfigureWebApplication(app); }) ).SetupLogging(s => { s.LogRequests = true; s.LogResponses = true; s.LogRequestHeaders = false; s.LogResponseCompletion = true; s.LogResponseHeaders = false; s.LogQueryString = new LoggingCollectionSettings(_ => true); }) .SetupThrottling(b => b.DisableThrottling()); }
protected override void ConfigureServices(IServiceCollection services, IVostokHostingEnvironment hostingEnvironment) { base.ConfigureServices(services, hostingEnvironment); var configuration = hostingEnvironment.SecretConfigurationProvider.Get <UlearnConfiguration>(hostingEnvironment.SecretConfigurationSource); services.AddDbContext <AntiPlagiarismDb>( options => options.UseSqlServer(configuration.Database) ); services.Configure <AntiPlagiarismConfiguration>(options => options.SetFrom(hostingEnvironment.SecretConfigurationProvider.Get <AntiPlagiarismConfiguration>(hostingEnvironment.SecretConfigurationSource))); services.AddSwaggerExamplesFromAssemblyOf <WebApplication>(); }
private static string TranslateEnvironmentName(IVostokHostingEnvironment vostokHostingEnvironment) { if (vostokHostingEnvironment.IsProduction()) { return(EnvironmentName.Production); } if (vostokHostingEnvironment.IsDevelopment()) { return(EnvironmentName.Development); } if (vostokHostingEnvironment.IsStaging()) { return(EnvironmentName.Staging); } return(vostokHostingEnvironment.Environment); }
private async Task SleepAsync(IVostokHostingEnvironment environment, TimeSpan sleep) { if (settings.GracefulShutdown || settings.CrashDuringStopping) { await Task.Delay(sleep, environment.ShutdownToken).SilentlyContinue(); if (settings.CrashDuringStopping && environment.ShutdownToken.IsCancellationRequested) { throw settings.CrashError; } } else { await Task.Delay(sleep); } }
public void Build(BuildContext context, IVostokHostingEnvironment environment) { this.environment = environment; if (context.ZooKeeperClient != null && !(context.ZooKeeperClient is DevNullZooKeeperClient)) { HostExtensions.Add(context.ZooKeeperClient); } HostExtensions.Add <IVostokApplicationDiagnostics>(context.DiagnosticsHub); HostExtensions.Add(context.Logs.LogEventLevelCounterFactory); builderCustomization.Customize(this); context.DisposableHostExtensions = disposable; context.HostExtensions = HostExtensions; }
public void Build(BuildContext context, IVostokHostingEnvironment environment) { this.environment = environment; var settings = settingsCustomization.Customize(new SystemMetricsSettings()); var processMetricsContext = context.Metrics.Instance.WithTag("SystemMetricsType", "Process"); var hostMetricsContext = context.Metrics.Instance.WithTag("SystemMetricsType", "Host"); if (RuntimeDetector.IsDotNetCore30AndNewer) { RegisterGcMonitor(settings, context, processMetricsContext); } RegisterProcessMonitor(settings, context, processMetricsContext); RegisterHostMonitor(settings, context, hostMetricsContext); }
private void RegisterDiagnosticFeatures(IVostokHostingEnvironment environment) { if (!environment.HostExtensions.TryGet <IVostokApplicationDiagnostics>(out var diagnostics)) { return; } foreach (var actionRunner in runner.Runners) { var info = actionRunner.GetInfo(); var infoEntry = new DiagnosticEntry("scheduled", info.Name); var infoProvider = new ScheduledActionsInfoProvider(actionRunner); var healthCheck = new ScheduledActionsHealthCheck(actionRunner); disposables.Add(diagnostics.Info.RegisterProvider(infoEntry, infoProvider)); disposables.Add(diagnostics.HealthTracker.RegisterCheck($"scheduled ({info.Name})", healthCheck)); } }
public void TestSetup() { application = new Application(); hostExtensions = new TestHostExtensions(); environment = Substitute.For <IVostokHostingEnvironment>(); environment.HostExtensions.Returns(_ => hostExtensions); environment.ConfigurationProvider.Returns(configProvider = Substitute.For <IConfigurationProvider>()); environment.SecretConfigurationProvider.Returns(secretConfigProvider = Substitute.For <IConfigurationProvider>()); environment.Port.Returns(123); hostExtensions.Add(Guid.Empty); hostExtensions.Add("key", Guid.Empty); configProvider.Get <string>().Returns(string.Empty); secretConfigProvider.Get <string>().Returns(string.Empty); }
private void ConfigureServices(IServiceCollection services, IVostokHostingEnvironment hostingEnvironment, Logger logger) { services.AddDbContextPool <UlearnDb>( options => options.UseSqlServer(hostingEnvironment.Configuration["database"]) ); var configuration = ApplicationConfiguration.Read <WebApiConfiguration>(); /* DI */ services.AddSingleton <ILogger>(logger); services.AddSingleton <ULearnUserManager>(); services.AddSingleton <InitialDataCreator>(); services.AddSingleton <WebCourseManager>(); services.AddSingleton(configuration); services.AddScoped <IAuthorizationHandler, CourseRoleAuthorizationHandler>(); services.AddScoped <IAuthorizationHandler, CourseAccessAuthorizationHandler>(); /* DI for database repos */ services.AddScoped <UsersRepo>(); services.AddScoped <CommentsRepo>(); services.AddScoped <UserRolesRepo>(); services.AddScoped <CoursesRepo>(); services.AddScoped <SlideCheckingsRepo>(); services.AddScoped <GroupsRepo>(); services.AddScoped <UserSolutionsRepo>(); services.AddScoped <UserQuizzesRepo>(); services.AddScoped <VisitsRepo>(); services.AddScoped <TextsRepo>(); /* Asp.NET Core MVC */ services.AddMvc(options => { /* Add binder for passing Course object to actions */ options.ModelBinderProviders.Insert(0, new CourseBinderProvider()); /* Disable model checking because in other case stack overflow raised on course model binding. * See https://github.com/aspnet/Mvc/issues/7357 for details */ options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(Course))); } ); ConfigureAuthServices(services, configuration, logger); }
private async Task WarmupMiddlewares(IVostokHostingEnvironment environment) { if (environment.ServiceBeacon.ReplicaInfo.TryGetUrl(out var url)) { var client = new ClusterClient( environment.Log, s => { s.ClusterProvider = new FixedClusterProvider(url); s.SetupUniversalTransport(); }); await client.SendAsync(Request.Get("_status/ping"), 20.Seconds()); } else { environment.Log.Warn("Unable to warmup middlewares. Couldn't obtain replica url."); } }