Skip to content

Commit 1f60819

Browse files
Simon Zeltserahmetb
Simon Zeltser
authored andcommitted
Introducing super basic health check for cart service (GoogleCloudPlatform#44)
* Introducing super basic health check for cart service - Generated C# proto implementation for grpc health check - Moved all C# protos to a dedicated folder - Implemented basic health checking to ping CartStore (which is Redis in default implementation) - Base plumbing for health checks * Introducing super basic health check for cart service - Generated C# proto implementation for grpc health check - Moved all C# protos to a dedicated folder - Implemented basic health checking to ping CartStore (which is Redis in default implementation) - Base plumbing for health checks * Changing Ping health probe to call Redis Cache Ping method
1 parent 1bab006 commit 1f60819

File tree

9 files changed

+736
-861
lines changed

9 files changed

+736
-861
lines changed

src/cartservice/HealthImpl.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using cartservice.interfaces;
3+
using Grpc.Health.V1;
4+
using StackExchange.Redis;
5+
using static Grpc.Health.V1.Health;
6+
7+
namespace cartservice {
8+
internal class HealthImpl : HealthBase {
9+
private ICartStore dependency { get; }
10+
public HealthImpl (ICartStore dependency) {
11+
this.dependency = dependency;
12+
}
13+
14+
public HealthCheckResponse Check (HealthCheckRequest request) {
15+
Console.WriteLine ("Checking CartService Health");
16+
17+
return new HealthCheckResponse {
18+
Status = dependency.Ping() ? HealthCheckResponse.Types.ServingStatus.Serving : HealthCheckResponse.Types.ServingStatus.NotServing
19+
};
20+
}
21+
}
22+
}

src/cartservice/Program.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ static object StartServer(string host, int port, ICartStore cartStore)
5656
Console.WriteLine($"Trying to start a grpc server at {host}:{port}");
5757
Server server = new Server
5858
{
59-
Services = { Hipstershop.CartService.BindService(new CartServiceImpl(cartStore)) },
59+
Services =
60+
{
61+
// Cart Service Endpoint
62+
Hipstershop.CartService.BindService(new CartServiceImpl(cartStore)),
63+
64+
// Health Endpoint
65+
Grpc.Health.V1.Health.BindService(new HealthImpl(cartStore))
66+
},
6067
Ports = { new ServerPort(host, port, ServerCredentials.Insecure) }
6168
};
6269

src/cartservice/cartservice.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
<ItemGroup>
99
<PackageReference Include="CommandLineParser" Version="2.2.1" />
10-
<PackageReference Include="Google.Protobuf" Version="3.5.1" />
10+
<PackageReference Include="Google.Protobuf" Version="3.6.1" />
1111
<PackageReference Include="Google.Protobuf.Tools" Version="3.5.1" />
1212
<PackageReference Include="grpc" Version="1.12.0" />
13+
<PackageReference Include="Grpc.HealthCheck" Version="1.15.0" />
1314
<PackageReference Include="grpc.tools" Version="1.12.0" />
1415
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
1516
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />

src/cartservice/cartstore/LocalCartStore.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,10 @@ public Task EmptyCartAsync(string userId)
8282

8383
return Task.FromResult(cart);
8484
}
85+
86+
public bool Ping()
87+
{
88+
return true;
89+
}
8590
}
8691
}

src/cartservice/cartstore/RedisCartStore.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,20 @@ public async Task EmptyCartAsync(string userId)
197197
throw new RpcException(new Status(StatusCode.FailedPrecondition, $"Can't access cart storage. {ex}"));
198198
}
199199
}
200+
201+
public bool Ping()
202+
{
203+
try
204+
{
205+
var redis = ConnectionMultiplexer.Connect(redisConnectionOptions);
206+
var cache = redis.GetDatabase();
207+
var res = cache.Ping();
208+
return res != TimeSpan.Zero;
209+
}
210+
catch (Exception)
211+
{
212+
return false;
213+
}
214+
}
200215
}
201216
}

src/cartservice/generate_protos.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ cd /d %~dp0
2222
set NUGET_PATH=%UserProfile%\.nuget\packages
2323
set TOOLS_PATH=%NUGET_PATH%\Grpc.Tools\1.12.0\tools\windows_x64
2424

25-
%TOOLS_PATH%\protoc.exe -I%~dp0/../../pb;%NUGET_PATH%\google.protobuf.tools\3.5.1\tools\ --csharp_out %~dp0 %~dp0\..\..\pb\demo.proto --grpc_out %~dp0 --plugin=protoc-gen-grpc=%TOOLS_PATH%\grpc_csharp_plugin.exe
25+
%TOOLS_PATH%\protoc.exe -I%~dp0/../../pb;%NUGET_PATH%\google.protobuf.tools\3.5.1\tools\ --csharp_out %~dp0\grpc_generated %~dp0\..\..\pb\demo.proto --grpc_out %~dp0\grpc_generated --plugin=protoc-gen-grpc=%TOOLS_PATH%\grpc_csharp_plugin.exe
2626

2727
endlocal

src/cartservice/Demo.cs renamed to src/cartservice/grpc_generated/Demo.cs

Lines changed: 586 additions & 803 deletions
Large diffs are not rendered by default.

src/cartservice/DemoGrpc.cs renamed to src/cartservice/grpc_generated/DemoGrpc.cs

Lines changed: 95 additions & 55 deletions
Large diffs are not rendered by default.

src/cartservice/interfaces/ICartStore.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ internal interface ICartStore
2424
Task EmptyCartAsync(string userId);
2525

2626
Task<Hipstershop.Cart> GetCartAsync(string userId);
27+
28+
bool Ping();
2729
}
2830
}

0 commit comments

Comments
 (0)