예제 #1
0
        public async Task GetEvents_RemovesExpiredItems()
        {
            var window = new SlidingWindow <MyItem>(TimeSpan.FromSeconds(1));

            for (int i = 0; i < 5; i++)
            {
                window.AddEvent(new MyItem {
                    Data = i
                });
                await Task.Delay(100);
            }

            var evts = window.GetEvents().ToArray();

            Assert.Equal(5, evts.Length);
            for (int i = 0; i < 5; i++)
            {
                Assert.Equal(i, evts[i].Data);
            }

            // now let the items expire
            await Task.Delay(1000);

            // add a new event that shouldn't be expired
            var evt = new MyItem {
                Data = 7
            };

            window.AddEvent(evt);

            evts = window.GetEvents().ToArray();
            Assert.Equal(1, evts.Length);
            Assert.Same(evt, evts[0]);
        }
예제 #2
0
        public async Task GetEvents_RemovesExpiredItems()
        {
            var expiry              = TimeSpan.FromSeconds(1);
            var window              = new SlidingWindow <MyItem>(expiry);
            var initialEventCount   = 5;
            var insertionTimestamps = new DateTime[initialEventCount];

            for (int i = 0; i < initialEventCount; i++)
            {
                window.AddEvent(new MyItem {
                    Data = i
                });
                insertionTimestamps[i] = DateTime.Now;
                await Task.Delay(100);
            }

            var evts = window.GetEvents().ToArray();

            // How many items have expired? We're at the mercy of the task scheduler here.
            // In theory we should have only waited 5 * 100 = 500ms and have zero expired items but we might have waited longer.
            var insertionCompleteTimestamp = DateTime.Now;
            var expiredCount  = insertionTimestamps.Count(t => (insertionCompleteTimestamp - t) > expiry);
            var expectedCount = initialEventCount - expiredCount;

            // Check the total count and each of the non expired items
            Assert.Equal(expectedCount, evts.Length);
            for (int i = 0; i < expectedCount; i++)
            {
                Assert.Equal(i + expiredCount, evts[i].Data);
            }

            // now let the items expire
            await Task.Delay(expiry);

            // add a new event that shouldn't be expired
            var evt = new MyItem {
                Data = 7
            };

            window.AddEvent(evt);

            evts = window.GetEvents().ToArray();
            Assert.Equal(1, evts.Length);
            Assert.Same(evt, evts[0]);
        }
예제 #3
0
        private bool ShutdownHostIfUnhealthy()
        {
            if (ShouldMonitorHostHealth && _healthCheckWindow.GetEvents().Count(isHealthy => !isHealthy) > _healthMonitorOptions.Value.HealthCheckThreshold)
            {
                // if the number of times the host has been unhealthy in
                // the current time window exceeds the threshold, recover by
                // initiating shutdown
                _logger.UnhealthyCountExceeded(_healthMonitorOptions.Value.HealthCheckThreshold, _healthMonitorOptions.Value.HealthCheckWindow);
                _applicationLifetime.StopApplication();
                return(true);
            }

            return(false);
        }
        private bool ShutdownHostIfUnhealthy()
        {
            if (ShouldMonitorHostHealth && _healthCheckWindow.GetEvents().Count(isHealthy => !isHealthy) > _healthMonitorOptions.Value.HealthCheckThreshold)
            {
                // if the number of times the host has been unhealthy in
                // the current time window exceeds the threshold, recover by
                // initiating shutdown
                _logger.UnhealthyCountExceeded(_healthMonitorOptions.Value.HealthCheckThreshold, _healthMonitorOptions.Value.HealthCheckWindow);
                var environment = _rootServiceProvider.GetService <IScriptJobHostEnvironment>();
                environment.Shutdown();
                return(true);
            }

            return(false);
        }
예제 #5
0
        private bool ShutdownHostIfUnhealthy()
        {
            if (ShouldMonitorHostHealth && _healthCheckWindow.GetEvents().Where(isHealthy => !isHealthy).Count() > _healthMonitorOptions.Value.HealthCheckThreshold)
            {
                // if the number of times the host has been unhealthy in
                // the current time window exceeds the threshold, recover by
                // initiating shutdown
                var message = $"Host unhealthy count exceeds the threshold of {_healthMonitorOptions.Value.HealthCheckThreshold} for time window {_healthMonitorOptions.Value.HealthCheckWindow}. Initiating shutdown.";
                _logger.LogError(0, message);
                var environment = _rootServiceProvider.GetService <IScriptJobHostEnvironment>();
                environment.Shutdown();
                return(true);
            }

            return(false);
        }