/// <summary>
 ///     Creates a new resolution scope from the default service provider and executes the checks in the given group.
 /// </summary>
 public async Task <CompositeHealthCheckResult> RunGroupAsync(HealthCheckGroup group,
                                                              CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var scope = GetServiceScope())
     {
         return(await RunGroupAsync(scope.ServiceProvider, group, cancellationToken).ConfigureAwait(false));
     }
 }
 public HealthCheckService(HealthCheckBuilder builder, IServiceProvider serviceProvider,
                           IServiceScopeFactory serviceScopeFactory)
 {
     _builder             = builder;
     _groups              = GetGroups().Where(group => group.GroupName != string.Empty).ToList();
     _root                = GetGroup(string.Empty);
     _serviceProvider     = serviceProvider;
     _serviceScopeFactory = serviceScopeFactory;
 }
Esempio n. 3
0
        /// <summary>
        ///     This constructor should only be used when creating a grouped health check builder.
        /// </summary>
        public HealthCheckBuilder(HealthCheckBuilder rootBuilder, HealthCheckGroup currentGroup)
        {
            Guard.ArgumentNotNull(nameof(rootBuilder), rootBuilder);
            Guard.ArgumentNotNull(nameof(currentGroup), currentGroup);

            _checksByName = rootBuilder._checksByName;
            _currentGroup = currentGroup;
            _groups       = rootBuilder._groups;

            DefaultCacheDuration = rootBuilder.DefaultCacheDuration;
        }
Esempio n. 4
0
        public HealthCheckBuilder()
        {
            _checksByName = new Dictionary <string, CachedHealthCheck>(StringComparer.OrdinalIgnoreCase);
            _currentGroup = new HealthCheckGroup(string.Empty, CheckStatus.Unhealthy);
            _groups       = new Dictionary <string, HealthCheckGroup>(StringComparer.OrdinalIgnoreCase)
            {
                [string.Empty] = _currentGroup
            };

            DefaultCacheDuration = TimeSpan.FromMinutes(5);
        }
        /// <summary>
        ///     Uses the provided service provider and executes the checks in the given group.
        /// </summary>
        public async Task <CompositeHealthCheckResult> RunGroupAsync(IServiceProvider serviceProvider,
                                                                     HealthCheckGroup group, CancellationToken cancellationToken = default(CancellationToken))
        {
            var result     = new CompositeHealthCheckResult(group.PartiallyHealthyStatus);
            var checkTasks = group.Checks.Select(check =>
                                                 new { Check = check, Task = check.RunAsync(serviceProvider, cancellationToken).AsTask() }).ToList();
            await Task.WhenAll(checkTasks.Select(checkTask => checkTask.Task));

            foreach (var checkTask in checkTasks)
            {
                result.Add(checkTask.Check.Name, checkTask.Task.Result);
            }

            return(result);
        }
Esempio n. 6
0
        /// <summary>
        ///     Creates a new health check group, to which you can add one or more health
        ///     checks.
        /// </summary>
        public HealthCheckBuilder AddHealthCheckGroup(string groupName, Action <HealthCheckBuilder> groupChecks,
                                                      CheckStatus partialSuccessStatus)
        {
            Guard.ArgumentNotNullOrEmpty(nameof(groupName), groupName);
            Guard.ArgumentNotNull(nameof(groupChecks), groupChecks);
            Guard.ArgumentValid(partialSuccessStatus != CheckStatus.Unknown, nameof(partialSuccessStatus),
                                "Check status 'Unknown' is not valid for partial success.");
            Guard.ArgumentValid(!_groups.ContainsKey(groupName), nameof(groupName),
                                $"A group with name '{groupName}' has already been registered.");
            Guard.OperationValid(_currentGroup.GroupName == string.Empty,
                                 "Nested groups are not supported by HealthCheckBuilder.");

            var group = new HealthCheckGroup(groupName, partialSuccessStatus);

            _groups.Add(groupName, group);

            var innerBuilder = new HealthCheckBuilder(this, group);

            groupChecks(innerBuilder);

            return(this);
        }