diff --git a/HealthChecks.sln b/HealthChecks.sln index facc21a..9c72ba3 100644 --- a/HealthChecks.sln +++ b/HealthChecks.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26228.9 +VisualStudioVersion = 15.0.27130.2010 MinimumVisualStudioVersion = 15.0.26228.4 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{E05DCF88-F916-4B61-A5DC-A8344C9E2429}" EndProject @@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNet.HealthChec EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleHealthChecker.AspNet", "samples\SampleHealthChecker.AspNet\SampleHealthChecker.AspNet.csproj", "{33FB5967-62C7-4230-B515-780EF63F748E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Extensions.HealthChecks.PostgreSql", "src\Microsoft.Extensions.HealthChecks.PostgreSql\Microsoft.Extensions.HealthChecks.PostgreSql.csproj", "{207D3914-5E0D-4970-BB5F-0ED5E08A11D0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -63,6 +65,10 @@ Global {33FB5967-62C7-4230-B515-780EF63F748E}.Debug|Any CPU.Build.0 = Debug|Any CPU {33FB5967-62C7-4230-B515-780EF63F748E}.Release|Any CPU.ActiveCfg = Release|Any CPU {33FB5967-62C7-4230-B515-780EF63F748E}.Release|Any CPU.Build.0 = Release|Any CPU + {207D3914-5E0D-4970-BB5F-0ED5E08A11D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {207D3914-5E0D-4970-BB5F-0ED5E08A11D0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {207D3914-5E0D-4970-BB5F-0ED5E08A11D0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {207D3914-5E0D-4970-BB5F-0ED5E08A11D0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -76,5 +82,9 @@ Global {13BE838A-200B-4A68-8F58-3EA3BE3A1A8A} = {F9BA869A-7D5F-420F-9505-2D881F7934A7} {2AE82E1C-6CE1-4755-A332-FA359B7CCE72} = {F9BA869A-7D5F-420F-9505-2D881F7934A7} {33FB5967-62C7-4230-B515-780EF63F748E} = {E05DCF88-F916-4B61-A5DC-A8344C9E2429} + {207D3914-5E0D-4970-BB5F-0ED5E08A11D0} = {F9BA869A-7D5F-420F-9505-2D881F7934A7} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {AB38C3CE-2846-4064-A914-D776DFC8F1E0} EndGlobalSection EndGlobal diff --git a/src/Microsoft.Extensions.HealthChecks.PostgreSql/HealthCheckBuilderPostgreSqlExtensions.cs b/src/Microsoft.Extensions.HealthChecks.PostgreSql/HealthCheckBuilderPostgreSqlExtensions.cs new file mode 100644 index 0000000..ba32ce3 --- /dev/null +++ b/src/Microsoft.Extensions.HealthChecks.PostgreSql/HealthCheckBuilderPostgreSqlExtensions.cs @@ -0,0 +1,48 @@ +using System; +using System.Data; +using Npgsql; + +namespace Microsoft.Extensions.HealthChecks.PostgreSql +{ + public static class HealthCheckBuilderPostgreSqlExtensions + { + public static HealthCheckBuilder AddPostgreSqlCheck(this HealthCheckBuilder builder, string name, string connectionString) + { + Guard.ArgumentNotNull(nameof(builder), builder); + + return AddPostgreSqlCheck(builder, name, connectionString, builder.DefaultCacheDuration); + } + + public static HealthCheckBuilder AddPostgreSqlCheck(this HealthCheckBuilder builder, string name, string connectionString, TimeSpan cacheDuration) + { + builder.AddCheck($"PostgreSqlCheck({name})", async () => + { + try + { + using (var connection = new NpgsqlConnection(connectionString)) + { + connection.Open(); + using (var command = connection.CreateCommand()) + { + command.CommandType = CommandType.Text; + command.CommandText = "SELECT 1"; + var result = (int)await command.ExecuteScalarAsync().ConfigureAwait(false); + if (result == 1) + { + return HealthCheckResult.Healthy($"PostgreSqlCheck({name}): Healthy"); + } + + return HealthCheckResult.Unhealthy($"PostgreSqlCheck({name}): Unhealthy"); + } + } + } + catch (Exception ex) + { + return HealthCheckResult.Unhealthy($"PostgreSqlCheck({name}): Exception during check: {ex.GetType().FullName}"); + } + }, cacheDuration); + + return builder; + } + } +} diff --git a/src/Microsoft.Extensions.HealthChecks.PostgreSql/Microsoft.Extensions.HealthChecks.PostgreSql.csproj b/src/Microsoft.Extensions.HealthChecks.PostgreSql/Microsoft.Extensions.HealthChecks.PostgreSql.csproj new file mode 100644 index 0000000..74e778f --- /dev/null +++ b/src/Microsoft.Extensions.HealthChecks.PostgreSql/Microsoft.Extensions.HealthChecks.PostgreSql.csproj @@ -0,0 +1,20 @@ + + + + netstandard1.3 + + + + + + + + + + + + + + + + diff --git a/src/Microsoft.Extensions.HealthChecks.SqlServer/HealthCheckBuilderSqlServerExtensions.cs b/src/Microsoft.Extensions.HealthChecks.SqlServer/HealthCheckBuilderSqlServerExtensions.cs index 1837d96..a9747f9 100644 --- a/src/Microsoft.Extensions.HealthChecks.SqlServer/HealthCheckBuilderSqlServerExtensions.cs +++ b/src/Microsoft.Extensions.HealthChecks.SqlServer/HealthCheckBuilderSqlServerExtensions.cs @@ -27,7 +27,7 @@ public static HealthCheckBuilder AddSqlCheck(this HealthCheckBuilder builder, st //TODO: There is probably a much better way to do this. using (var connection = new SqlConnection(connectionString)) { - connection.Open(); + await connection.OpenAsync(); using (var command = connection.CreateCommand()) { command.CommandType = CommandType.Text;