Skip to content
This repository has been archived by the owner on Apr 13, 2020. It is now read-only.

Add MySQL liveness #9

Merged
merged 2 commits into from
Mar 26, 2018
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
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ REDIS_PORT=6379
POSTGRES_USER=postgres
POSTGRES_PASSWORD=Password12!
POSTGRES_PORT=8010
MONGODB_PORT=27017
MONGODB_PORT=27017
MYSQL_PORT=3306
MYSQL_PASSWORD=Password12!
9 changes: 8 additions & 1 deletion BeatPulse.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BeatPulse.NpgSql", "src\Bea
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BeatPulse.MongoDb", "src\BeatPulse.MongoDb\BeatPulse.MongoDb.csproj", "{86E77F50-5306-42DE-9500-E56F3AD3F69C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeatPulse.AzureStorage", "src\BeatPulse.AzureStorage\BeatPulse.AzureStorage.csproj", "{924EC204-A2E9-4B74-9AE4-6C7C096BC2D9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BeatPulse.AzureStorage", "src\BeatPulse.AzureStorage\BeatPulse.AzureStorage.csproj", "{924EC204-A2E9-4B74-9AE4-6C7C096BC2D9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeatPulse.MySql", "src\BeatPulse.MySql\BeatPulse.MySql.csproj", "{868D83D9-8F44-4CC9-890B-435C843BD30D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -76,6 +78,10 @@ Global
{924EC204-A2E9-4B74-9AE4-6C7C096BC2D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{924EC204-A2E9-4B74-9AE4-6C7C096BC2D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{924EC204-A2E9-4B74-9AE4-6C7C096BC2D9}.Release|Any CPU.Build.0 = Release|Any CPU
{868D83D9-8F44-4CC9-890B-435C843BD30D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{868D83D9-8F44-4CC9-890B-435C843BD30D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{868D83D9-8F44-4CC9-890B-435C843BD30D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{868D83D9-8F44-4CC9-890B-435C843BD30D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -90,6 +96,7 @@ Global
{DAFB2EB8-5645-433A-8555-3C6ED11260C2} = {1C706B20-C9D9-40ED-A6A8-5007DF1FD2BA}
{86E77F50-5306-42DE-9500-E56F3AD3F69C} = {1C706B20-C9D9-40ED-A6A8-5007DF1FD2BA}
{924EC204-A2E9-4B74-9AE4-6C7C096BC2D9} = {1C706B20-C9D9-40ED-A6A8-5007DF1FD2BA}
{868D83D9-8F44-4CC9-890B-435C843BD30D} = {1C706B20-C9D9-40ED-A6A8-5007DF1FD2BA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9B397864-402E-48A9-A8DC-0B98151CA4BE}
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ The [Microsoft HealthCheck](https://github.com/dotnet-architecture/HealthChecks)
Install-Package BeatPulse
```

2. Install the liveness libraries that you need on your project. At this moment *BeatPulse* contains libraries for *Redis, SqlServer, MongoDb, Postgress Sql, Azure Storage (Blobs, Tables and Queues) and custom lambda liveness*.
2. Install the liveness libraries that you need on your project. At this moment *BeatPulse* contains libraries for *Redis, SqlServer, MongoDb, Postgress Sql, Azure Storage (Blobs, Tables and Queues), MySQL and custom lambda liveness*.

``` PowerShell
Install-Package BeatPulse.SqlServer
Install-Package BeatPulse.MongoDb
Install-Package BeatPulse.Npgsql
Install-Package BeatPulse.Redis
Install-Package BeatPulse.AzureStorage
Install-Package BeatPulse.MySql
```

3. Add *BeatPulse* into your ASP.NET Core project. *UseBeatPulse* is a new IWebHostBuilder extension method for register and configure BeatPulse.
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
- mssql2016
- postgresql101
- mongodb
- mysql
nuget:
disable_publish_on_pr: true
build_script:
Expand Down
2 changes: 2 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ if ($suffix -eq "") {
exec { & dotnet pack .\src\BeatPulse.MongoDb\BeatPulse.MongoDb.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
exec { & dotnet pack .\src\BeatPulse.NpgSql\BeatPulse.NpgSql.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
exec { & dotnet pack .\src\BeatPulse.AzureStorage\BeatPulse.AzureStorage.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
exec { & dotnet pack .\src\BeatPulse.MySql\BeatPulse.MySql.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
} else {
exec { & dotnet pack .\src\BeatPulse\BeatPulse.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\BeatPulse.SqlServer\BeatPulse.SqlServer.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\BeatPulse.Redis\BeatPulse.Redis.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\BeatPulse.MongoDb\BeatPulse.MongoDb.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\BeatPulse.NpgSql\BeatPulse.NpgSql.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\BeatPulse.AzureStorage\BeatPulse.AzureStorage.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\BeatPulse.MySql\BeatPulse.MySql.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
}

1 change: 1 addition & 0 deletions build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<XUnitPackageVersion>2.3.1</XUnitPackageVersion>
<XUnitRunnerVisualstudio>2.3.1</XUnitRunnerVisualstudio>
<WindowsAzureStorageVersion>9.1.0</WindowsAzureStorageVersion>
<MySqlDataVersion>6.10.6</MySqlDataVersion>
</PropertyGroup>

<PropertyGroup Label="CLI Tools Versions">
Expand Down
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ services:
mongodb:
image: mongo
ports:
- ${MONGODB_PORT}:27017
- ${MONGODB_PORT}:27017
mysql:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
ports:
- ${MYSQL_PORT}:3306
27 changes: 27 additions & 0 deletions src/BeatPulse.MySql/BeatPulse.MySql.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetStandardTargetVersion)</TargetFramework>
<PackageLicenseUrl>$(PackageLicenseUrl)</PackageLicenseUrl>
<PackageProjectUrl>$(PackageProjectUrl)</PackageProjectUrl>
<Description>BeatPulse.MySql is the health check package for MySQL on BeatPulse </Description>
<PackageTags>BeatPulse;HealthCheck;Beat;Health;MySQL</PackageTags>
<Version>$(Version)</Version>
<RepositoryUrl>$(RepositoryUrl)</RepositoryUrl>
<Company>$(Company)</Company>
<Authors>$(Authors)</Authors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MySql.Data" Version="$(MySqlDataVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BeatPulse\BeatPulse.csproj" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions src/BeatPulse.MySql/BeatPulseContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using BeatPulse.Core;
using BeatPulse.MySql;

namespace BeatPulse
{
public static class BeatPulseContextExtensions
{
public static BeatPulseContext AddMySql(this BeatPulseContext context, string connectionString)
{
context.Add(new MySqlLiveness(connectionString));
return context;
}
}
}
41 changes: 41 additions & 0 deletions src/BeatPulse.MySql/MySqlLiveness.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using BeatPulse.Core;
using Microsoft.AspNetCore.Http;
using MySql.Data.MySqlClient;
using System;
using System.Threading;

namespace BeatPulse.MySql
{
public class MySqlLiveness : IBeatPulseLiveness
{
private readonly string connectionString;

public string Name => nameof(MySqlLiveness);

public string DefaultPath => "mysql";

public MySqlLiveness(string connectionString)
{
this.connectionString = connectionString;
}

public async System.Threading.Tasks.Task<(string, bool)> IsHealthy(HttpContext context, bool isDevelopment, CancellationToken cancellationToken = default)
{
try
{
using (var connection = new MySqlConnection(connectionString))
{
await connection.OpenAsync(cancellationToken);
return (BeatPulseKeys.BEATPULSE_HEALTHCHECK_DEFAULT_OK_MESSAGE, true);
}
}
catch (Exception ex)
{
var message = !isDevelopment ? string.Format(BeatPulseKeys.BEATPULSE_HEALTHCHECK_DEFAULT_ERROR_MESSAGE, Name)
: $"Exception {ex.GetType().Name} with message ('{ex.Message}')";

return (message, false);
}
}
}
}
72 changes: 72 additions & 0 deletions tests/FunctionalTests/BeatPulse.MySql/MySqlLivenessTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using FunctionalTests.Base;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
using Xunit;
using BeatPulse;
using Microsoft.AspNetCore.TestHost;
using FluentAssertions;
using System.Net;

namespace FunctionalTests.BeatPulse.MySql
{
[Collection("execution")]
public class mysql_liveness_should
{
private readonly ExecutionFixture _fixture;

public mysql_liveness_should(ExecutionFixture fixture)
{
_fixture = fixture ?? throw new ArgumentException(nameof(fixture));
}

[Fact]
public async Task be_healthy_when_mysql_server_is_available()
{
var connectionString = "server=localhost;port=3306;database=information_schema;uid=root;password=Password12!";

var webHostBuilder = new WebHostBuilder()
.UseStartup<DefaultStartup>()
.UseBeatPulse()
.ConfigureServices(services =>
{
services.AddBeatPulse(context =>
{
context.AddMySql(connectionString);
});
});

var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest(BeatPulseKeys.BEATPULSE_DEFAULT_PATH)
.GetAsync();

response.EnsureSuccessStatusCode();
}

[Fact]
public async Task be_unhealthy_when_mysql_server_is_unavailable()
{
var connectionString = "server=255.255.255.255;port=3306;database=information_schema;uid=root;password=Password12!";

var webHostBuilder = new WebHostBuilder()
.UseStartup<DefaultStartup>()
.UseBeatPulse()
.ConfigureServices(services =>
{
services.AddBeatPulse(context =>
{
context.AddMySql(connectionString);
});
});

var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest(BeatPulseKeys.BEATPULSE_DEFAULT_PATH)
.GetAsync();

response.StatusCode.Should().Be(HttpStatusCode.ServiceUnavailable);
}
}
}
1 change: 1 addition & 0 deletions tests/FunctionalTests/FunctionalTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\BeatPulse.MongoDb\BeatPulse.MongoDb.csproj" />
<ProjectReference Include="..\..\src\BeatPulse.MySql\BeatPulse.MySql.csproj" />
<ProjectReference Include="..\..\src\BeatPulse.NpgSql\BeatPulse.NpgSql.csproj" />
<ProjectReference Include="..\..\src\BeatPulse.Redis\BeatPulse.Redis.csproj" />
<ProjectReference Include="..\..\src\BeatPulse.SqlServer\BeatPulse.SqlServer.csproj" />
Expand Down