src/Sample.APIInfo/APIBackgroundProcessor.cs
/// <summary>
/// Main Processing loop for background health checks
/// </summary>
/// <param name="stoppingToken"></param>
/// <returns></returns>
Main Processing loop for background health checks
Main Processing loop for background health checks
protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
			stoppingToken.Register(StopService);
			TimeSpan sleepTime = TimeSpan.FromSeconds(5);
			while ( !stoppingToken.IsCancellationRequested ) {
				_healthCheckProcessor.CheckHealth();
				await Task.Delay(1000, stoppingToken);
				EnumHealthStatus currentStatus = _healthCheckProcessor.Status;
				if ( currentStatus != _lastHealthStatus ) {
					string msg = "  It was previously in a " + _lastHealthStatus.ToString() + " state for " + _lastStatusCount + " Health Cycle Checks.";
					if (currentStatus == EnumHealthStatus.Healthy) _logger.LogWarning("Health Status has returned to a Healthy State." + msg);
					else if (currentStatus == EnumHealthStatus.Failed) _logger.LogCritical("Health status has changed to a Failed State.  The service will likely not operate correctly." + msg);
					else if (currentStatus == EnumHealthStatus.Degraded) _logger.LogError("Health Status has changed to a Degraded State.  This may or may not have impacts on the service.  Investigation should immediately be looked into." + msg);
					else if (currentStatus == EnumHealthStatus.Unknown) _logger.LogError("Health Status is unknown.  This should be short term upon initial application start.  If it does not change shortly, then something is wrong.");
					_lastHealthStatus = currentStatus;
					_lastStatusCount = 1;
				}
				await Task.Delay(sleepTime, stoppingToken);
			}
		}
protected override async Task ExecuteAsync ( CancellationToken stoppingToken ) { stoppingToken . Register ( StopService ) ; TimeSpan sleepTime = TimeSpan . FromSeconds ( 5 ) ; while ( ! stoppingToken . IsCancellationRequested ) { _healthCheckProcessor . CheckHealth ( ) ; await Task . Delay ( 1000 , stoppingToken ) ; EnumHealthStatus currentStatus = _healthCheckProcessor . Status ; if ( currentStatus != _lastHealthStatus ) { string msg = " " + _lastHealthStatus . ToString ( ) + " " + _lastStatusCount + " " ; if ( currentStatus == EnumHealthStatus . Healthy ) _logger . LogWarning ( " " + msg ) ; else if ( currentStatus == EnumHealthStatus . Failed ) _logger . LogCritical ( " " + msg ) ; else if ( currentStatus == EnumHealthStatus . Degraded ) _logger . LogError ( " " + msg ) ; else if ( currentStatus == EnumHealthStatus . Unknown ) _logger . LogError ( " " ) ; _lastHealthStatus = currentStatus ; _lastStatusCount = 1 ; } await Task . Delay ( sleepTime , stoppingToken ) ; } }
Main Processing loop for background health checks
Main Processing loop for background health checks
// We sleep for a second to allow health checks to run then check the status.  Note, some may still be running, especially if they are erroring, there status will be updated next run. // Sleep for cycle time.
None
stoppingToken
CancellationToken
None