Skip to content

Refactored Health Checks #172

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

Merged
merged 2 commits into from
Aug 16, 2019
Merged
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
30 changes: 30 additions & 0 deletions src/Volvox.Helios.Web/HealthChecks/SqlServerHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Data.SqlClient;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Volvox.Helios.Web.HealthChecks
{
public class SqlServerHealthCheck : IHealthCheck
{
SqlConnection _connection;

public SqlServerHealthCheck(SqlConnection connection)
{
_connection = connection;
}

public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext healthCheckContext, CancellationToken cancellationToken = default(CancellationToken))
{
try
{
_connection.Open();
}
catch (SqlException)
{
return Task.FromResult(HealthCheckResult.Unhealthy());
}
return Task.FromResult(HealthCheckResult.Healthy());
}
}
}
18 changes: 2 additions & 16 deletions src/Volvox.Helios.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,23 +181,9 @@ public void ConfigureServices(IServiceCollection services)
});
});

//TODO: Refactor to make cleaner
// Health Checks
services.AddHealthChecks()
.AddCheck("sql", () =>
{
using (var connection = new SqlConnection(Configuration.GetConnectionString("VolvoxHeliosDatabase")))
{
try
{
connection.Open();
}
catch (SqlException)
{
return HealthCheckResult.Unhealthy();
}
return HealthCheckResult.Healthy();
}
});
.AddSqlServer(Configuration["ConnectionStrings:VolvoxHeliosDatabase"]);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
1 change: 1 addition & 0 deletions src/Volvox.Helios.Web/Volvox.Helios.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AspNet.Security.OAuth.Discord" Version="2.0.0" />
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="2.2.1" />
<PackageReference Include="AWS.Logger.AspNetCore" Version="1.4.1" />
<PackageReference Include="AWS.Logger.Core" Version="1.3.1" />
<PackageReference Include="BuildBundlerMinifier" Version="2.8.391" />
Expand Down