public IHttpActionResult HealthCheck() { HealthCheckResponse response = new HealthCheckResponse(); HealthCheckerContainer container = TrendyolApp.Instance.DataStore.GetData <HealthCheckerContainer>(Constants.HealthCheckerContainerDataKey); if (container == null || container.HealthCheckers.IsEmpty()) { return(Ok(response)); } IHealthCheckerActivator healthCheckerActivator = TrendyolApp.Instance.GetHealthCheckerActivator(); response.Results = new List <HealthCheckResult>(); foreach (HealthChecker checker in container.HealthCheckers) { IHealthChecker checkerInstance = healthCheckerActivator.CreateHealthCheckerInstance(checker.HealthCheckerType); if (checkerInstance == null) { throw new ConfigurationErrorsException($"There was a problem while creating healthchecker instance for type:{checker.HealthCheckerType.FullName}. Check if your type implements IHealthChecker interface."); } HealthCheckResult result = new HealthCheckResult(); result.Key = checkerInstance.Key; result.IsCtirical = checkerInstance.IsCritical; try { result.Success = checkerInstance.CheckHealth(); } catch (Exception ex) { Logger.Error(ex); result.Message = ex.Message; result.Success = false; } response.Results.Add(result); } HttpStatusCode statusCode = HttpStatusCode.OK; if (response.Results.HasElements() && response.Results.Any(r => r.IsCtirical && r.Success == false)) { statusCode = HttpStatusCode.InternalServerError; } return(Content(statusCode, response)); }
public TrendyolWebApiBuilder WithHealthChecker<T>() where T : IHealthChecker { HealthCheckerContainer container = _appBuilder.DataStore.GetData<HealthCheckerContainer>(Constants.HealthCheckerContainerDataKey); if (container == null) { container = new HealthCheckerContainer(); } container.AddHealthChecker(typeof(T)); _appBuilder.DataStore.SetData(Constants.HealthCheckerContainerDataKey, container); return this; }
private void RegisterHealthCheckers() { HealthCheckerContainer container = _sillycoreAppBuilder.DataStore.Get <HealthCheckerContainer>(Constants.HealthCheckerContainerDataKey); if (container == null) { container = new HealthCheckerContainer(); } Assembly ass = Assembly.GetEntryAssembly(); foreach (TypeInfo ti in ass.DefinedTypes) { if (ti.ImplementedInterfaces.Contains(typeof(IHealthChecker))) { container.AddHealthChecker(ti); _sillycoreAppBuilder.Services.AddTransient(ti); _sillycoreAppBuilder.DataStore.Set(Constants.HealthCheckerContainerDataKey, container); } } }
public async Task <IActionResult> HealthCheck() { using (var scope = _scopeFactory.CreateScope()) { HealthCheckResponse response = new HealthCheckResponse(); response.DockerImageName = Environment.GetEnvironmentVariable("Sillycore.DockerImageName"); if (SillycoreApp.Instance.DataStore.Get <bool>(Constants.IsShuttingDown)) { response.AddErrorMessage("Shutting down..."); return(StatusCode(503, response)); } HealthCheckerContainer container = SillycoreApp.Instance.DataStore.Get <HealthCheckerContainer>(Constants.HealthCheckerContainerDataKey); if (container != null && container.HealthCheckers.HasElements()) { response.Results = new List <HealthCheckResult>(); foreach (HealthChecker checker in container.HealthCheckers) { IHealthChecker checkerInstance = (IHealthChecker)scope.ServiceProvider.GetService(checker.HealthCheckerType); if (checkerInstance == null) { throw new ApplicationException($"There was a problem while creating healthchecker instance for type:{checker.HealthCheckerType.FullName}. Check if your type implements IHealthChecker interface."); } HealthCheckResult result = new HealthCheckResult(); result.Key = checkerInstance.Key; result.IsCtirical = checkerInstance.IsCritical; try { result.Success = await checkerInstance.CheckHealth(); } catch (Exception ex) { _logger.LogError(ex, $"There was a problem while running healthcheckher:{checker.HealthCheckerType.FullName}"); result.Message = ex.Message; result.Success = false; } response.Results.Add(result); } } HttpStatusCode statusCode = HttpStatusCode.OK; if (response.Results.HasElements() && response.Results.Any(r => r.IsCtirical && r.Success == false)) { statusCode = HttpStatusCode.InternalServerError; } if (_backgroundJobManager.IsActive) { response.BackgroundJobManager = _backgroundJobManager; if (response.BackgroundJobManager.JobTimers.Any(t => t.Status == BackgroundJobStatus.Failing)) { statusCode = HttpStatusCode.InternalServerError; response.AddErrorMessage("At least one of background jobs is failing."); } } return(StatusCode((int)statusCode, response)); } }