Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v14: Async healthchecks #17090

Open
wants to merge 6 commits into
base: contrib
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task<ActionResult<HealthCheckResultResponseModel>> ExecuteAction(
return BadRequest(invalidModelProblem);
}

HealthCheckStatus result = healthCheck.ExecuteAction(_umbracoMapper.Map<HealthCheckAction>(action)!);
HealthCheckStatus result = await healthCheck.ExecuteActionAsync(_umbracoMapper.Map<HealthCheckAction>(action)!);

return await Task.FromResult(Ok(_umbracoMapper.Map<HealthCheckResultResponseModel>(result)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public async Task<IActionResult> ByNameWithResult(
return HealthCheckGroupNotFound();
}

return await Task.FromResult(Ok(_healthCheckGroupPresentationFactory.CreateHealthCheckGroupWithResultViewModel(group)));
return Ok(await _healthCheckGroupPresentationFactory.CreateHealthCheckGroupWithResultViewModelAsync(group));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@ public HealthCheckGroupPresentationFactory(
return groups;
}

public HealthCheckGroupWithResultResponseModel CreateHealthCheckGroupWithResultViewModel(IGrouping<string?, HealthCheck> healthCheckGroup)
[Obsolete("Use CreateHealthCheckGroupWithResultViewModelAsync instead. Will be removed in v16")]
public HealthCheckGroupWithResultResponseModel CreateHealthCheckGroupWithResultViewModel(IGrouping<string?, HealthCheck> healthCheckGroup) =>
throw new NotImplementedException("Use CreateHealthCheckGroupWithResultViewModelAsync instead");

[Obsolete("Use CreateHealthCheckGroupWithResultViewModelAsync instead. Will be removed in v16")]
public HealthCheckWithResultPresentationModel CreateHealthCheckWithResultViewModel(HealthCheck healthCheck) =>
throw new NotImplementedException("Use CreateHealthCheckGroupWithResultViewModelAsync instead");

public async Task<HealthCheckGroupWithResultResponseModel> CreateHealthCheckGroupWithResultViewModelAsync(IGrouping<string?, HealthCheck> healthCheckGroup)
{
var healthChecks = new List<HealthCheckWithResultPresentationModel>();

foreach (HealthCheck healthCheck in healthCheckGroup)
{
healthChecks.Add(CreateHealthCheckWithResultViewModel(healthCheck));
healthChecks.Add(await CreateHealthCheckWithResultViewModelAsync(healthCheck));
}

var healthCheckGroupViewModel = new HealthCheckGroupWithResultResponseModel
Expand All @@ -57,11 +65,11 @@ public HealthCheckGroupWithResultResponseModel CreateHealthCheckGroupWithResultV
return healthCheckGroupViewModel;
}

public HealthCheckWithResultPresentationModel CreateHealthCheckWithResultViewModel(HealthCheck healthCheck)
public async Task<HealthCheckWithResultPresentationModel> CreateHealthCheckWithResultViewModelAsync(HealthCheck healthCheck)
{
_logger.LogDebug($"Running health check: {healthCheck.Name}");

IEnumerable<HealthCheckStatus> results = healthCheck.GetStatus().Result;
IEnumerable<HealthCheckStatus> results = await healthCheck.GetStatusAsync();

var healthCheckViewModel = new HealthCheckWithResultPresentationModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@ public interface IHealthCheckGroupPresentationFactory
{
IEnumerable<IGrouping<string?, HealthCheck>> CreateGroupingFromHealthCheckCollection();

[Obsolete("Use CreateHealthCheckGroupWithResultViewModelAsync instead. Will be removed in v16")]
HealthCheckGroupWithResultResponseModel CreateHealthCheckGroupWithResultViewModel(IGrouping<string?, HealthCheck> healthCheckGroup);

[Obsolete("Use CreateHealthCheckGroupWithResultViewModelAsync instead. Will be removed in v16")]
HealthCheckWithResultPresentationModel CreateHealthCheckWithResultViewModel(HealthCheck healthCheck);

Task<HealthCheckGroupWithResultResponseModel> CreateHealthCheckGroupWithResultViewModelAsync(IGrouping<string?, HealthCheck> healthCheckGroup)
#pragma warning disable CS0618 // Type or member is obsolete
=> Task.FromResult(CreateHealthCheckGroupWithResultViewModel(healthCheckGroup));
#pragma warning restore CS0618 // Type or member is obsolete

Task<HealthCheckWithResultPresentationModel> CreateHealthCheckWithResultViewModelAsync(HealthCheck healthCheck)
#pragma warning disable CS0618 // Type or member is obsolete
=> Task.FromResult(CreateHealthCheckWithResultViewModel(healthCheck));
#pragma warning restore CS0618 // Type or member is obsolete
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.

Check notice on line 1 in src/Umbraco.Core/HealthChecks/Checks/AbstractSettingsCheck.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (contrib)

✅ No longer an issue: Complex Conditional

GetStatus no longer has a complex conditional. A complex conditional is an expression inside a branch (e.g. if, for, while) which consists of multiple, logical operators such as AND/OR. The more logical operators in an expression, the more severe the code smell.

Check notice on line 1 in src/Umbraco.Core/HealthChecks/Checks/AbstractSettingsCheck.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (contrib)

ℹ New issue: Complex Conditional

GetStatusAsync has 1 complex conditionals with 3 branches, threshold = 2. A complex conditional is an expression inside a branch (e.g. if, for, while) which consists of multiple, logical operators such as AND/OR. The more logical operators in an expression, the more severe the code smell.
// See LICENSE for more details.

using Umbraco.Cms.Core.Services;
Expand Down Expand Up @@ -63,7 +63,7 @@
"healthcheck", "checkErrorMessageUnexpectedValue", new[] { CurrentValue, Values.First(v => v.IsRecommended).Value, ItemPath });

/// <inheritdoc />
public override Task<IEnumerable<HealthCheckStatus>> GetStatus()
public override Task<IEnumerable<HealthCheckStatus>> GetStatusAsync()
{
// update the successMessage with the CurrentValue
var successMessage = string.Format(CheckSuccessMessage, ItemPath, Values, CurrentValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public DatabaseIntegrityCheck(
/// <summary>
/// Get the status for this health check
/// </summary>
public override Task<IEnumerable<HealthCheckStatus>> GetStatus() =>
public override Task<IEnumerable<HealthCheckStatus>> GetStatusAsync() =>
Task.FromResult((IEnumerable<HealthCheckStatus>)new[] { CheckDocuments(false), CheckMedia(false) });

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public FolderAndFilePermissionsCheck(
/// <summary>
/// Get the status for this health check
/// </summary>
public override Task<IEnumerable<HealthCheckStatus>> GetStatus()
public override Task<IEnumerable<HealthCheckStatus>> GetStatusAsync()
{
_filePermissionHelper.RunFilePermissionTestSuite(
out Dictionary<FilePermissionTest, IEnumerable<string>> errors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected BaseHttpHeaderCheck(
/// <summary>
/// Get the status for this health check
/// </summary>
public override async Task<IEnumerable<HealthCheckStatus>> GetStatus() =>
public override async Task<IEnumerable<HealthCheckStatus>> GetStatusAsync() =>
await Task.WhenAll(CheckForHeader());

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ExcessiveHeadersCheck(ILocalizedTextService textService, IHostingEnvironm
/// <summary>
/// Get the status for this health check
/// </summary>
public override async Task<IEnumerable<HealthCheckStatus>> GetStatus() =>
public override async Task<IEnumerable<HealthCheckStatus>> GetStatusAsync() =>
await Task.WhenAll(CheckForHeaders());

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/HealthChecks/Checks/Security/HstsCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public HstsCheck(IHostingEnvironment hostingEnvironment, ILocalizedTextService t
protected override string ReadMoreLink => Constants.HealthChecks.DocumentationLinks.Security.HstsCheck;

/// <inheritdoc />
public override async Task<IEnumerable<HealthCheckStatus>> GetStatus() =>
public override async Task<IEnumerable<HealthCheckStatus>> GetStatusAsync() =>
new HealthCheckStatus[] { await CheckForHeader() };

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public HttpsCheck(
_hostingEnvironment = hostingEnvironment;
}
/// <inheritdoc />
public override async Task<IEnumerable<HealthCheckStatus>> GetStatus() =>
public override async Task<IEnumerable<HealthCheckStatus>> GetStatusAsync() =>
await Task.WhenAll(
CheckIfCurrentSchemeIsHttps(),
CheckHttpsConfigurationSetting(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override HealthCheckStatus ExecuteAction(HealthCheckAction action) =>
/// <summary>
/// Get the status for this health check
/// </summary>
public override Task<IEnumerable<HealthCheckStatus>> GetStatus() =>
public override Task<IEnumerable<HealthCheckStatus>> GetStatusAsync() =>
Task.FromResult(CheckUmbracoApplicationUrl().Yield());

private HealthCheckStatus CheckUmbracoApplicationUrl()
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/HealthChecks/Checks/Services/SmtpCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SmtpCheck(ILocalizedTextService textService, IOptionsMonitor<GlobalSettin
/// <summary>
/// Get the status for this health check
/// </summary>
public override Task<IEnumerable<HealthCheckStatus>> GetStatus() =>
public override Task<IEnumerable<HealthCheckStatus>> GetStatusAsync() =>
Task.FromResult(CheckSmtpSettings().Yield());

/// <summary>
Expand Down
26 changes: 24 additions & 2 deletions src/Umbraco.Core/HealthChecks/HealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,34 @@ protected HealthCheck()
/// If there are possible actions to take to rectify this check, this method must be overridden by a sub class
/// in order to explicitly provide those actions.
/// </remarks>
public abstract Task<IEnumerable<HealthCheckStatus>> GetStatus();
[Obsolete("Use GetStatusAsync instead. Will be removed in v16")]
public virtual Task<IEnumerable<HealthCheckStatus>> GetStatus() => Task.FromResult(Enumerable.Empty<HealthCheckStatus>());

/// <summary>
/// Get the status for this health check
/// </summary>
/// <returns></returns>
/// <remarks>
/// If there are possible actions to take to rectify this check, this method must be overridden by a sub class
/// in order to explicitly provide those actions.
/// </remarks>
#pragma warning disable CS0618 // Type or member is obsolete
public virtual Task<IEnumerable<HealthCheckStatus>> GetStatusAsync() => GetStatus();
#pragma warning restore CS0618 // Type or member is obsolete

/// <summary>
/// Executes the action and returns it's status
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
public virtual HealthCheckStatus ExecuteAction(HealthCheckAction action) =>
new HealthCheckStatus("Not implemented");

/// <summary>
/// Executes the action and returns it's status
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
public abstract HealthCheckStatus ExecuteAction(HealthCheckAction action);
public virtual Task<HealthCheckStatus> ExecuteActionAsync(HealthCheckAction action) =>
Task.FromResult(ExecuteAction(action));
}
2 changes: 1 addition & 1 deletion src/Umbraco.Core/HealthChecks/HealthCheckResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static async Task<HealthCheckResults> Create(IEnumerable<HealthCheck> che
{
try
{
return await t.GetStatus();
return await t.GetStatusAsync();
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ private class TestHealthCheck : HealthCheck
{
public override HealthCheckStatus ExecuteAction(HealthCheckAction action) => new("Check message");

public override async Task<IEnumerable<HealthCheckStatus>> GetStatus() => Enumerable.Empty<HealthCheckStatus>();
public override async Task<IEnumerable<HealthCheckStatus>> GetStatusAsync() => Enumerable.Empty<HealthCheckStatus>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public StubHealthCheck(StatusResultType resultType, string message)
public override HealthCheckStatus ExecuteAction(HealthCheckAction action) =>
throw new NotImplementedException();

public override async Task<IEnumerable<HealthCheckStatus>> GetStatus() =>
public override async Task<IEnumerable<HealthCheckStatus>> GetStatusAsync() =>
new List<HealthCheckStatus> { new(_message) { ResultType = _resultType } };
}

Expand Down Expand Up @@ -56,7 +56,7 @@ public StubHealthCheck3(StatusResultType resultType, string message)
{
}

public override async Task<IEnumerable<HealthCheckStatus>> GetStatus() =>
public override async Task<IEnumerable<HealthCheckStatus>> GetStatusAsync() =>
throw new Exception("Check threw exception");
}

Expand Down
Loading