public WebScriptHostManager(ScriptHostConfiguration config,
                                    ISecretManagerFactory secretManagerFactory,
                                    IScriptEventManager eventManager,
                                    ScriptSettingsManager settingsManager,
                                    WebHostSettings webHostSettings,
                                    IWebJobsRouter router,
                                    ILoggerFactory loggerFactory,
                                    IScriptHostFactory scriptHostFactory = null,
                                    ISecretsRepositoryFactory secretsRepositoryFactory = null,
                                    HostPerformanceManager hostPerformanceManager      = null,
                                    ILoggerProviderFactory loggerProviderFactory       = null,
                                    int hostTimeoutSeconds = 30,
                                    int hostPollingIntervalMilliseconds = 500)
            : base(config, settingsManager, scriptHostFactory, eventManager, environment: null,
                   hostPerformanceManager: hostPerformanceManager, loggerProviderFactory: loggerProviderFactory)
        {
            _config = config;

            _metricsLogger      = new WebHostMetricsLogger();
            _exceptionHandler   = new WebScriptHostExceptionHandler(this);
            _webHostSettings    = webHostSettings;
            _settingsManager    = settingsManager;
            _hostTimeoutSeconds = hostTimeoutSeconds;
            _hostRunningPollIntervalMilliseconds = hostPollingIntervalMilliseconds;
            _router = router;

            config.IsSelfHost = webHostSettings.IsSelfHost;

            secretsRepositoryFactory = secretsRepositoryFactory ?? new DefaultSecretsRepositoryFactory();
            var secretsRepository = secretsRepositoryFactory.Create(settingsManager, webHostSettings, config);

            _secretManager = secretManagerFactory.Create(settingsManager, loggerFactory.CreateLogger(ScriptConstants.LogCategoryHostGeneral), secretsRepository);

            _bindingWebHookProvider = new WebJobsSdkExtensionHookProvider(_secretManager);
        }
        public void GetCounters_ReturnsExpectedResult()
        {
            var    mockSettings = new Mock <ScriptSettingsManager>(MockBehavior.Strict);
            string value        = string.Empty;
            var    traceWriter  = new TestTraceWriter(TraceLevel.Verbose);

            mockSettings.Setup(p => p.GetSetting(EnvironmentSettingNames.AzureWebsiteAppCountersName)).Returns(() => value);
            var performanceManager = new HostPerformanceManager(mockSettings.Object, traceWriter);

            value = "{\"userTime\": 30000000,\"kernelTime\": 16562500,\"pageFaults\": 131522,\"processes\": 1,\"processLimit\": 32,\"threads\": 32,\"threadLimit\": 512,\"connections\": 4,\"connectionLimit\": 300,\"sections\": 3,\"sectionLimit\": 256,\"namedPipes\": 0,\"namedPipeLimit\": 128,\"readIoOperations\": 675,\"writeIoOperations\": 18,\"otherIoOperations\": 9721,\"readIoBytes\": 72585119,\"writeIoBytes\": 5446,\"otherIoBytes\": 393926,\"privateBytes\": 33759232,\"handles\": 987,\"contextSwitches\": 15535,\"remoteOpens\": 250}";
            var counters = performanceManager.GetPerformanceCounters();

            Assert.Equal(counters.PageFaults, 131522);

            value    = "{\"userTime\": 30000000,\"kernelTime\": 16562500,\"pageFaults\": 131522,\"processes\": 1,\"processLimit\": 32,\"threads\": 32,\"threadLimit\": 512,\"connections\": 4,\"connectionLimit\": 300,\"sections\": 3,\"sectionLimit\": 256,\"namedPipes\": 0,\"namedPipeLimit\": 128,\"readIoOperations\": 675,\"writeIoOperations\": 18,\"otherIoOperations\": 9721,\"readIoBytes\": 72585119,\"writeIoBytes\": 5446,\"otherIoBytes\": 393926,\"privateBytes\": 33759232,\"handles\": 987,\"contextSwitches\": 15535,\"remoteOpens\": 250}猅";
            counters = performanceManager.GetPerformanceCounters();
            Assert.Equal(counters.PageFaults, 131522);

            value    = "{}";
            counters = performanceManager.GetPerformanceCounters();
            Assert.Equal(counters.PageFaults, 0);

            value    = "this is not json";
            counters = performanceManager.GetPerformanceCounters();
            Assert.Null(counters);
            var error = traceWriter.Traces.Last();

            Assert.Equal("Failed to deserialize application performance counters. JSON Content: \"this is not json\"", error.Message);
        }
Ejemplo n.º 3
0
        public WebScriptHostManager(ScriptHostConfiguration config, ISecretManagerFactory secretManagerFactory, ScriptSettingsManager settingsManager, WebHostSettings webHostSettings, IScriptHostFactory scriptHostFactory = null, ISecretsRepositoryFactory secretsRepositoryFactory = null)
            : base(config, settingsManager, scriptHostFactory)
        {
            _config           = config;
            _metricsLogger    = new WebHostMetricsLogger();
            _exceptionHandler = new WebScriptHostExceptionHandler(this);
            _webHostSettings  = webHostSettings;

            var systemEventGenerator = config.HostConfig.GetService <IEventGenerator>() ?? new EventGenerator();
            var systemTraceWriter    = new SystemTraceWriter(systemEventGenerator, settingsManager, TraceLevel.Verbose);

            if (config.TraceWriter != null)
            {
                config.TraceWriter = new CompositeTraceWriter(new TraceWriter[] { config.TraceWriter, systemTraceWriter });
            }
            else
            {
                config.TraceWriter = systemTraceWriter;
            }

            config.IsSelfHost = webHostSettings.IsSelfHost;

            _performanceManager     = new HostPerformanceManager(settingsManager, config.TraceWriter);
            _swaggerDocumentManager = new SwaggerDocumentManager(config);

            var secretsRepository = secretsRepositoryFactory.Create(settingsManager, webHostSettings, config);

            _secretManager = secretManagerFactory.Create(settingsManager, config.TraceWriter, secretsRepository);
        }
Ejemplo n.º 4
0
        public WebJobsScriptHostService(IOptionsMonitor <ScriptApplicationHostOptions> applicationHostOptions, IScriptHostBuilder scriptHostBuilder, ILoggerFactory loggerFactory,
                                        IScriptWebHostEnvironment scriptWebHostEnvironment, IEnvironment environment,
                                        HostPerformanceManager hostPerformanceManager, IOptions <HostHealthMonitorOptions> healthMonitorOptions,
                                        IMetricsLogger metricsLogger, IApplicationLifetime applicationLifetime, IConfiguration config, IScriptEventManager eventManager)
        {
            ArgumentNullException.ThrowIfNull(loggerFactory);

            // This will no-op if already initialized.
            InitializeApplicationInsightsRequestTracking();

            _applicationLifetime = applicationLifetime;
            RegisterApplicationLifetimeEvents();

            _metricsLogger            = metricsLogger;
            _applicationHostOptions   = applicationHostOptions ?? throw new ArgumentNullException(nameof(applicationHostOptions));
            _scriptWebHostEnvironment = scriptWebHostEnvironment ?? throw new ArgumentNullException(nameof(scriptWebHostEnvironment));
            _scriptHostBuilder        = scriptHostBuilder ?? throw new ArgumentNullException(nameof(scriptHostBuilder));
            _environment          = environment ?? throw new ArgumentNullException(nameof(environment));
            _performanceManager   = hostPerformanceManager ?? throw new ArgumentNullException(nameof(hostPerformanceManager));
            _healthMonitorOptions = healthMonitorOptions ?? throw new ArgumentNullException(nameof(healthMonitorOptions));
            _logger       = loggerFactory.CreateLogger(ScriptConstants.LogCategoryHostGeneral);
            _config       = config ?? throw new ArgumentNullException(nameof(config));
            _eventManager = eventManager;

            _hostStarted = _hostStartedSource.Task;

            State = ScriptHostState.Default;

            if (ShouldMonitorHostHealth)
            {
                _healthCheckWindow    = new SlidingWindow <bool>(_healthMonitorOptions.Value.HealthCheckWindow);
                _hostHealthCheckTimer = new Timer(OnHostHealthCheckTimer, null, TimeSpan.Zero, _healthMonitorOptions.Value.HealthCheckInterval);
            }
        }
Ejemplo n.º 5
0
        public WebJobsScriptHostService(IOptionsMonitor <ScriptApplicationHostOptions> applicationHostOptions, IScriptHostBuilder scriptHostBuilder, ILoggerFactory loggerFactory, IServiceProvider rootServiceProvider,
                                        IServiceScopeFactory rootScopeFactory, IScriptWebHostEnvironment scriptWebHostEnvironment, IEnvironment environment,
                                        HostPerformanceManager hostPerformanceManager, IOptions <HostHealthMonitorOptions> healthMonitorOptions)
        {
            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            _applicationHostOptions   = applicationHostOptions ?? throw new ArgumentNullException(nameof(applicationHostOptions));
            _scriptWebHostEnvironment = scriptWebHostEnvironment ?? throw new ArgumentNullException(nameof(scriptWebHostEnvironment));
            _rootServiceProvider      = rootServiceProvider;
            _scriptHostBuilder        = scriptHostBuilder ?? throw new ArgumentNullException(nameof(scriptHostBuilder));
            _environment          = environment ?? throw new ArgumentNullException(nameof(environment));
            _performanceManager   = hostPerformanceManager ?? throw new ArgumentNullException(nameof(hostPerformanceManager));
            _healthMonitorOptions = healthMonitorOptions ?? throw new ArgumentNullException(nameof(healthMonitorOptions));
            _logger = loggerFactory.CreateLogger(ScriptConstants.LogCategoryHostGeneral);

            State = ScriptHostState.Default;

            if (ShouldMonitorHostHealth)
            {
                _healthCheckWindow    = new SlidingWindow <bool>(_healthMonitorOptions.Value.HealthCheckWindow);
                _hostHealthCheckTimer = new Timer(OnHostHealthCheckTimer, null, TimeSpan.Zero, _healthMonitorOptions.Value.HealthCheckInterval);
            }
        }
        public void GetCounters_ReturnsExpectedResult()
        {
            var    mockEnvironment = new Mock <IEnvironment>(MockBehavior.Strict);
            string value           = string.Empty;
            var    logger          = new TestLogger("Test");

            mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteAppCountersName)).Returns(() => value);
            var options            = new HostHealthMonitorOptions();
            var performanceManager = new HostPerformanceManager(mockEnvironment.Object, new OptionsWrapper <HostHealthMonitorOptions>(options));

            value = "{\"userTime\": 30000000,\"kernelTime\": 16562500,\"pageFaults\": 131522,\"processes\": 1,\"processLimit\": 32,\"threads\": 32,\"threadLimit\": 512,\"connections\": 4,\"connectionLimit\": 300,\"activeConnections\": 25,\"activeConnectionLimit\": 600,\"sections\": 3,\"sectionLimit\": 256,\"namedPipes\": 0,\"namedPipeLimit\": 128,\"remoteDirMonitors\": 5,\"remoteDirMonitorLimit\": 500,\"readIoOperations\": 675,\"writeIoOperations\": 18,\"otherIoOperations\": 9721,\"readIoBytes\": 72585119,\"writeIoBytes\": 5446,\"otherIoBytes\": 393926,\"privateBytes\": 33759232,\"handles\": 987,\"contextSwitches\": 15535,\"remoteOpens\": 250}";
            var counters = performanceManager.GetPerformanceCounters(logger);

            Assert.Equal(counters.PageFaults, 131522);

            // verify garbage characters are trimmed
            value    = value + "猅";
            counters = performanceManager.GetPerformanceCounters(logger);
            Assert.Equal(counters.PageFaults, 131522);

            value    = "{}";
            counters = performanceManager.GetPerformanceCounters(logger);
            Assert.Equal(counters.PageFaults, 0);

            value    = "this is not json";
            counters = performanceManager.GetPerformanceCounters(logger);
            Assert.Null(counters);
            var error = logger.GetLogMessages().Last();

            Assert.Equal("Failed to deserialize application performance counters. JSON Content: \"this is not json\"", error.FormattedMessage);
        }
 public ScriptHostManager(
     ScriptHostConfiguration config,
     IScriptEventManager eventManager              = null,
     IScriptHostEnvironment environment            = null,
     ILoggerProviderFactory loggerProviderFactory  = null,
     HostPerformanceManager hostPerformanceManager = null)
     : this(config, ScriptSettingsManager.Instance, new ScriptHostFactory(), eventManager, environment, loggerProviderFactory, hostPerformanceManager)
 {
 }
        public void IsUnderHighLoad_Connections_ReturnsExpectedResults(int currentValue, bool expected)
        {
            var counters = new ApplicationPerformanceCounters
            {
                Connections     = currentValue,
                ConnectionLimit = 300
            };

            Assert.Equal(expected, HostPerformanceManager.IsUnderHighLoad(counters));
        }
        public static void IsUnderHighLoad_Processes_ReturnsExpectedResults(int currentValue, bool expected)
        {
            var counters = new ApplicationPerformanceCounters
            {
                Processes    = currentValue,
                ProcessLimit = 32
            };

            Assert.Equal(expected, HostPerformanceManager.IsUnderHighLoad(counters));
        }
        public void IsUnderHighLoad_Threads_ReturnsExpectedResults(int currentValue, bool expected)
        {
            var counters = new ApplicationPerformanceCounters
            {
                Threads     = currentValue,
                ThreadLimit = 512
            };

            Assert.Equal(expected, HostPerformanceManager.IsUnderHighLoad(counters));
        }
        public void IsUnderHighLoad_NamedPipes_ReturnsExpectedResults(int currentValue, bool expected)
        {
            var counters = new ApplicationPerformanceCounters
            {
                NamedPipes     = currentValue,
                NamedPipeLimit = 128
            };

            Assert.Equal(expected, HostPerformanceManager.IsUnderHighLoad(counters));
        }
Ejemplo n.º 12
0
        public HostPerformanceManagerTests()
        {
            var mockEnvironment = new Mock <IEnvironment>(MockBehavior.Strict);

            _performanceCountersValue = string.Empty;
            _logger = new TestLogger("Test");
            mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteSku)).Returns(ScriptConstants.DynamicSku);
            mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.RoleInstanceId)).Returns((string)null);
            mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteAppCountersName)).Returns(() => _performanceCountersValue);
            var options = new HostHealthMonitorOptions();

            _serviceProviderMock = new Mock <IServiceProvider>(MockBehavior.Strict);
            _performanceManager  = new HostPerformanceManager(mockEnvironment.Object, new OptionsWrapper <HostHealthMonitorOptions>(options), _serviceProviderMock.Object);
        }
Ejemplo n.º 13
0
 public HostController(IOptions <ScriptApplicationHostOptions> applicationHostOptions,
                       ILoggerFactory loggerFactory,
                       IEnvironment environment,
                       IScriptHostManager scriptHostManager,
                       IFunctionsSyncManager functionsSyncManager,
                       HostPerformanceManager performanceManager)
 {
     _applicationHostOptions = applicationHostOptions;
     _logger               = loggerFactory.CreateLogger(ScriptConstants.LogCategoryHostController);
     _environment          = environment;
     _scriptHostManager    = scriptHostManager;
     _functionsSyncManager = functionsSyncManager;
     _performanceManager   = performanceManager;
 }
        public void PerformanceCounterThresholdsExceeded_NamedPipes_ReturnsExpectedResults(int currentValue, bool expected)
        {
            var counters = new ApplicationPerformanceCounters
            {
                NamedPipes     = currentValue,
                NamedPipeLimit = 128
            };
            Collection <string> exceededCounters = new Collection <string>();

            Assert.Equal(expected, HostPerformanceManager.PerformanceCounterThresholdsExceeded(counters, exceededCounters: exceededCounters));
            if (expected)
            {
                Assert.Equal(1, exceededCounters.Count);
                Assert.Equal("NamedPipes", exceededCounters[0]);
            }
        }
        public void IsUnderHighLoad_ActiveConnections_ReturnsExpectedResults(int currentValue, bool expected)
        {
            var counters = new ApplicationPerformanceCounters
            {
                ActiveConnections     = currentValue,
                ActiveConnectionLimit = 600
            };
            Collection <string> exceededCounters = new Collection <string>();

            Assert.Equal(expected, HostPerformanceManager.IsUnderHighLoad(counters, exceededCounters: exceededCounters));
            if (expected)
            {
                Assert.Equal(1, exceededCounters.Count);
                Assert.Equal("ActiveConnections", exceededCounters[0]);
            }
        }
        public void PerformanceCounterThresholdsExceeded_RemoteDirMonitors_ReturnsExpectedResults(int currentValue, bool expected)
        {
            var counters = new ApplicationPerformanceCounters
            {
                RemoteDirMonitors     = currentValue,
                RemoteDirMonitorLimit = 500
            };
            Collection <string> exceededCounters = new Collection <string>();

            Assert.Equal(expected, HostPerformanceManager.PerformanceCounterThresholdsExceeded(counters, exceededCounters: exceededCounters));
            if (expected)
            {
                Assert.Equal(1, exceededCounters.Count);
                Assert.Equal("RemoteDirMonitors", exceededCounters[0]);
            }
        }
        public void IsUnderHighLoad_MultipleExceededThrottles_ReturnsExpectedResults()
        {
            var counters = new ApplicationPerformanceCounters
            {
                NamedPipes     = 130,
                NamedPipeLimit = 128,
                Processes      = 40,
                ProcessLimit   = 32,
                Threads        = 600,
                ThreadLimit    = 512
            };
            Collection <string> exceededCounters = new Collection <string>();

            Assert.Equal(true, HostPerformanceManager.IsUnderHighLoad(counters, exceededCounters));
            Assert.Equal(3, exceededCounters.Count);
            Assert.Equal("Threads, Processes, NamedPipes", string.Join(", ", exceededCounters));
        }
 public ScriptHostManager(
     ScriptHostConfiguration config,
     IScriptEventManager eventManager              = null,
     IScriptHostEnvironment environment            = null,
     ILoggerFactoryBuilder loggerFactoryBuilder    = null,
     HostPerformanceManager hostPerformanceManager = null)
     : this(config, ScriptSettingsManager.Instance, new ScriptHostFactory(), eventManager, environment, loggerFactoryBuilder, hostPerformanceManager)
 {
     if (config.FileWatchingEnabled)
     {
         // We only setup a subscription here as the actual ScriptHost will create the publisher
         // when initialized.
         _fileEventSubscription = EventManager.OfType <FileEvent>()
                                  .Where(f => string.Equals(f.Source, EventSources.ScriptFiles, StringComparison.Ordinal))
                                  .Subscribe(e => OnScriptFileChanged(null, e.FileChangeArguments));
     }
 }
        public ScriptHostManager(ScriptHostConfiguration config,
                                 ScriptSettingsManager settingsManager,
                                 IScriptHostFactory scriptHostFactory,
                                 IScriptEventManager eventManager              = null,
                                 IScriptHostEnvironment environment            = null,
                                 ILoggerProviderFactory loggerProviderFactory  = null,
                                 HostPerformanceManager hostPerformanceManager = null)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (settingsManager == null)
            {
                throw new ArgumentNullException(nameof(settingsManager));
            }

            scriptHostFactory      = scriptHostFactory ?? new ScriptHostFactory();
            _environment           = environment ?? this;
            _config                = config;
            _settingsManager       = settingsManager;
            _scriptHostFactory     = scriptHostFactory;
            _loggerProviderFactory = loggerProviderFactory;

            EventManager = eventManager ?? new ScriptEventManager();

            _structuredLogWriter = new StructuredLogWriter(EventManager, config.RootLogPath);
            _performanceManager  = hostPerformanceManager ?? new HostPerformanceManager(settingsManager, _config.HostHealthMonitor);

            if (config.FileWatchingEnabled && !settingsManager.FileSystemIsReadOnly)
            {
                // We only setup a subscription here as the actual ScriptHost will create the publisher
                // when initialized.
                _fileEventSubscription = EventManager.OfType <FileEvent>()
                                         .Where(f => string.Equals(f.Source, EventSources.ScriptFiles, StringComparison.Ordinal))
                                         .Subscribe(e => OnScriptFileChanged(null, e.FileChangeArguments));
            }

            if (ShouldMonitorHostHealth)
            {
                _healthCheckWindow    = new SlidingWindow <bool>(_config.HostHealthMonitor.HealthCheckWindow);
                _hostHealthCheckTimer = new Timer(OnHostHealthCheckTimer, null, TimeSpan.Zero, _config.HostHealthMonitor.HealthCheckInterval);
            }
        }
        public WebScriptHostManager(ScriptHostConfiguration config,
                                    ISecretManagerFactory secretManagerFactory,
                                    IScriptEventManager eventManager,
                                    ScriptSettingsManager settingsManager,
                                    WebHostSettings webHostSettings,
                                    IWebJobsRouter router,
                                    IScriptHostFactory scriptHostFactory = null,
                                    ISecretsRepositoryFactory secretsRepositoryFactory = null,
                                    ILoggerFactoryBuilder loggerFactoryBuilder         = null,
                                    HostPerformanceManager hostPerformanceManager      = null,
                                    int hostTimeoutSeconds = 30,
                                    int hostPollingIntervalMilliseconds = 500)
            : base(config, settingsManager, scriptHostFactory, eventManager, environment: null, loggerFactoryBuilder: loggerFactoryBuilder, hostPerformanceManager: hostPerformanceManager)
        {
            _config = config;

            _metricsLogger      = new WebHostMetricsLogger();
            _exceptionHandler   = new WebScriptHostExceptionHandler(this);
            _webHostSettings    = webHostSettings;
            _hostTimeoutSeconds = hostTimeoutSeconds;
            _hostRunningPollIntervalMilliseconds = hostPollingIntervalMilliseconds;
            _router = router;

            var systemEventGenerator = config.HostConfig.GetService <IEventGenerator>() ?? new EventGenerator();
            var systemTraceWriter    = new SystemTraceWriter(systemEventGenerator, settingsManager, TraceLevel.Verbose);

            if (config.TraceWriter != null)
            {
                config.TraceWriter = new CompositeTraceWriter(new TraceWriter[] { config.TraceWriter, systemTraceWriter });
            }
            else
            {
                config.TraceWriter = systemTraceWriter;
            }

            config.IsSelfHost = webHostSettings.IsSelfHost;

            secretsRepositoryFactory = secretsRepositoryFactory ?? new DefaultSecretsRepositoryFactory();
            var secretsRepository = secretsRepositoryFactory.Create(settingsManager, webHostSettings, config);

            _secretManager = secretManagerFactory.Create(settingsManager, config.HostConfig.LoggerFactory, secretsRepository);

            _bindingWebHookProvider = new WebJobsSdkExtensionHookProvider(_secretManager);
        }
        public async Task HostInitialization_OnInitializationException_MaintainsErrorInformation()
        {
            var options = new ScriptApplicationHostOptions
            {
                ScriptPath = @"c:\tests",
                LogPath    = @"c:\tests\logs",
            };

            var monitor = new ScriptApplicationHostOptionsMonitor(options);

            var services = new ServiceCollection()
                           .AddLogging()
                           .BuildServiceProvider();

            var host = new Mock <IHost>();

            host.Setup(h => h.Services)
            .Returns(services);
            host.SetupSequence(h => h.StartAsync(It.IsAny <CancellationToken>()))
            .Throws(new HostInitializationException("boom"))
            .Returns(Task.CompletedTask);

            var hostBuilder = new Mock <IScriptHostBuilder>();

            hostBuilder.Setup(b => b.BuildHost(It.IsAny <bool>(), It.IsAny <bool>()))
            .Returns(host.Object);

            var mockRootServiceProvider      = new Mock <IServiceProvider>();
            var mockRootScopeFactory         = new Mock <IServiceScopeFactory>();
            var mockScriptWebHostEnvironment = new Mock <IScriptWebHostEnvironment>();
            var mockEnvironment        = new Mock <IEnvironment>();
            var healthMonitorOptions   = new OptionsWrapper <HostHealthMonitorOptions>(new HostHealthMonitorOptions());
            var hostPerformanceManager = new HostPerformanceManager(mockEnvironment.Object, healthMonitorOptions);

            var hostService = new WebJobsScriptHostService(
                monitor, hostBuilder.Object, NullLoggerFactory.Instance, mockRootServiceProvider.Object, mockRootScopeFactory.Object,
                mockScriptWebHostEnvironment.Object, mockEnvironment.Object, hostPerformanceManager, healthMonitorOptions);

            await hostService.StartAsync(CancellationToken.None);

            Assert.Equal(ScriptHostState.Error, hostService.State);
            Assert.IsType <HostInitializationException>(hostService.LastError);
        }
        public static void GetCounters_ReturnsExpectedResult()
        {
            var    mockSettings = new Mock <ScriptSettingsManager>(MockBehavior.Strict);
            string value        = string.Empty;

            mockSettings.Setup(p => p.GetSetting(EnvironmentSettingNames.AzureWebsiteAppCountersName)).Returns(() => value);
            var performanceManager = new HostPerformanceManager(mockSettings.Object);

            value = "{\"userTime\": 30000000,\"kernelTime\": 16562500,\"pageFaults\": 131522,\"processes\": 1,\"processLimit\": 32,\"threads\": 32,\"threadLimit\": 512,\"connections\": 4,\"connectionLimit\": 300,\"sections\": 3,\"sectionLimit\": 256,\"namedPipes\": 0,\"namedPipeLimit\": 128,\"readIoOperations\": 675,\"writeIoOperations\": 18,\"otherIoOperations\": 9721,\"readIoBytes\": 72585119,\"writeIoBytes\": 5446,\"otherIoBytes\": 393926,\"privateBytes\": 33759232,\"handles\": 987,\"contextSwitches\": 15535,\"remoteOpens\": 250}";
            var counters = performanceManager.GetPerformanceCounters();

            Assert.Equal(counters.PageFaults, 131522);

            value    = "{\"userTime\": 30000000,\"kernelTime\": 16562500,\"pageFaults\": 131522,\"processes\": 1,\"processLimit\": 32,\"threads\": 32,\"threadLimit\": 512,\"connections\": 4,\"connectionLimit\": 300,\"sections\": 3,\"sectionLimit\": 256,\"namedPipes\": 0,\"namedPipeLimit\": 128,\"readIoOperations\": 675,\"writeIoOperations\": 18,\"otherIoOperations\": 9721,\"readIoBytes\": 72585119,\"writeIoBytes\": 5446,\"otherIoBytes\": 393926,\"privateBytes\": 33759232,\"handles\": 987,\"contextSwitches\": 15535,\"remoteOpens\": 250}猅";
            counters = performanceManager.GetPerformanceCounters();
            Assert.Equal(counters.PageFaults, 131522);

            value    = "{}";
            counters = performanceManager.GetPerformanceCounters();
            Assert.Equal(counters.PageFaults, 0);
        }
        public WebJobsScriptHostServiceTests()
        {
            var options = new ScriptApplicationHostOptions
            {
                ScriptPath = @"c:\tests",
                LogPath    = @"c:\tests\logs",
            };

            _monitor       = new ScriptApplicationHostOptionsMonitor(options);
            _loggerFactory = new LoggerFactory();
            _loggerFactory.AddProvider(_webHostLoggerProvider);

            _host = CreateMockHost();

            _mockRootServiceProvider      = new Mock <IServiceProvider>();
            _mockRootScopeFactory         = new Mock <IServiceScopeFactory>();
            _mockScriptWebHostEnvironment = new Mock <IScriptWebHostEnvironment>();
            _mockEnvironment        = new Mock <IEnvironment>();
            _healthMonitorOptions   = new OptionsWrapper <HostHealthMonitorOptions>(new HostHealthMonitorOptions());
            _hostPerformanceManager = new HostPerformanceManager(_mockEnvironment.Object, _healthMonitorOptions);
        }
        public WebJobsScriptHostServiceTests()
        {
            var options = new ScriptApplicationHostOptions
            {
                ScriptPath = @"c:\tests",
                LogPath    = @"c:\tests\logs",
            };

            _monitor       = new ScriptApplicationHostOptionsMonitor(options);
            _loggerFactory = new LoggerFactory();
            _loggerFactory.AddProvider(_webHostLoggerProvider);

            _host = CreateMockHost();

            _mockScriptWebHostEnvironment = new Mock <IScriptWebHostEnvironment>();
            _mockEnvironment      = new Mock <IEnvironment>();
            _healthMonitorOptions = new OptionsWrapper <HostHealthMonitorOptions>(new HostHealthMonitorOptions());
            var serviceProviderMock = new Mock <IServiceProvider>(MockBehavior.Strict);

            _hostPerformanceManager = new HostPerformanceManager(_mockEnvironment.Object, _healthMonitorOptions, serviceProviderMock.Object);
            _mockConfig             = new Mock <IConfiguration>().Object;
        }
        public ScriptHostManager(ScriptHostConfiguration config,
                                 ScriptSettingsManager settingsManager,
                                 IScriptHostFactory scriptHostFactory,
                                 IScriptEventManager eventManager              = null,
                                 IScriptHostEnvironment environment            = null,
                                 ILoggerFactoryBuilder loggerFactoryBuilder    = null,
                                 HostPerformanceManager hostPerformanceManager = null)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (settingsManager == null)
            {
                throw new ArgumentNullException(nameof(settingsManager));
            }

            scriptHostFactory     = scriptHostFactory ?? new ScriptHostFactory();
            _environment          = environment ?? this;
            _config               = config;
            _settingsManager      = settingsManager;
            _scriptHostFactory    = scriptHostFactory;
            _loggerFactoryBuilder = loggerFactoryBuilder;

            EventManager = eventManager ?? new ScriptEventManager();

            _structuredLogWriter = new StructuredLogWriter(EventManager, config.RootLogPath);
            _performanceManager  = hostPerformanceManager ?? new HostPerformanceManager(settingsManager);

            // TEMP : temporarily disabling this until the feature is improved
            bool periodicHealthCheckEnabled = false;

            if (periodicHealthCheckEnabled && config.HostHealthMonitorEnabled && settingsManager.IsAzureEnvironment)
            {
                _hostHealthCheckTimer = new Timer(OnHostHealthCheckTimer, null, TimeSpan.Zero, hostHealthCheckInterval);
            }
        }
Ejemplo n.º 26
0
        public async Task Invoke(HttpContext httpContext, IOptions <HttpOptions> httpOptions, HttpRequestQueue requestQueue, HostPerformanceManager performanceManager, IMetricsLogger metricsLogger)
        {
            if (httpOptions.Value.DynamicThrottlesEnabled &&
                ((DateTime.UtcNow - _lastPerformanceCheck) > _performanceCheckInterval))
            {
                // only check host status periodically
                Collection <string> exceededCounters = new Collection <string>();
                _rejectRequests = await performanceManager.IsUnderHighLoadAsync(exceededCounters);

                _lastPerformanceCheck = DateTime.UtcNow;
                if (_rejectRequests)
                {
                    _logger.LogWarning($"Thresholds for the following counters have been exceeded: [{string.Join(", ", exceededCounters)}]");
                }
            }

            if (_rejectRequests)
            {
                // we're currently in reject mode, so reject the request and
                // call the next delegate without calling base
                RejectRequest(httpContext, metricsLogger);
                return;
            }

            if (requestQueue.Enabled)
            {
                var success = await requestQueue.Post(httpContext, _next);

                if (!success)
                {
                    _logger?.LogInformation($"Http request queue limit of {httpOptions.Value.MaxOutstandingRequests} has been exceeded.");
                    RejectRequest(httpContext, metricsLogger);
                }
            }
            else
            {
                // queue is not enabled, so just dispatch the request directly
                await _next.Invoke(httpContext);
            }
        }
Ejemplo n.º 27
0
 public WebScriptHostRequestManager(HttpConfiguration config, HostPerformanceManager performanceManager, IMetricsLogger metricsLogger, TraceWriter traceWriter, int performanceCheckPeriodSeconds = 15) : base(config, traceWriter)
 {
     _performanceManager            = performanceManager;
     _metricsLogger                 = metricsLogger;
     _performanceCheckPeriodSeconds = performanceCheckPeriodSeconds;
 }
 public void ThresholdExceeded_ReturnsExpectedValue(int currentValue, int limit, float threshold, bool expected)
 {
     Assert.Equal(expected, HostPerformanceManager.ThresholdExceeded("Test", currentValue, limit, threshold));
 }