Skip to content

Commit 880ee16

Browse files
authored
grpc: add health checks to python services (GoogleCloudPlatform#28)
also converted line endings for recommendationservice/requirements.txt from dos to unix.
1 parent fc6df2d commit 880ee16

File tree

10 files changed

+306
-234
lines changed

10 files changed

+306
-234
lines changed

kubernetes-manifests/emailservice.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ spec:
3030
- containerPort: 8080
3131
readinessProbe:
3232
periodSeconds: 5
33-
tcpSocket:
34-
port: 8080
33+
exec:
34+
command: ["/bin/grpc_health_probe", "-addr=:8080"]
3535
livenessProbe:
3636
periodSeconds: 5
37-
tcpSocket:
38-
port: 8080
37+
exec:
38+
command: ["/bin/grpc_health_probe", "-addr=:8080"]
3939
resources:
4040
requests:
4141
cpu: 100m

kubernetes-manifests/recommendationservice.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ spec:
3030
- containerPort: 8080
3131
readinessProbe:
3232
periodSeconds: 5
33-
tcpSocket:
34-
port: 8080
33+
exec:
34+
command: ["/bin/grpc_health_probe", "-addr=:8080"]
3535
livenessProbe:
3636
periodSeconds: 5
37-
tcpSocket:
38-
port: 8080
37+
exec:
38+
command: ["/bin/grpc_health_probe", "-addr=:8080"]
3939
env:
4040
- name: PRODUCT_CATALOG_SERVICE_ADDR
4141
value: "productcatalogservice:3550"

src/emailservice/Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
2+
13
# Use the grpc.io provided Python image as the base image
24
FROM grpc/python:1.0
35

6+
# download the grpc health probe
7+
RUN GRPC_HEALTH_PROBE_VERSION=v0.1.0-alpha.1 && \
8+
wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \
9+
chmod +x /bin/grpc_health_probe
10+
411
# show python logs as they occur
512
ENV PYTHONUNBUFFERED=0
613

src/emailservice/email_server.py

+26-9
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
import os
2020
import sys
2121
import time
22-
22+
import grpc
2323
from jinja2 import Environment, FileSystemLoader, select_autoescape, TemplateError
2424
from google.api_core.exceptions import GoogleAPICallError
25-
import grpc
2625

2726
import demo_pb2
2827
import demo_pb2_grpc
28+
from grpc_health.v1 import health_pb2
29+
from grpc_health.v1 import health_pb2_grpc
2930

3031
# from opencensus.trace.ext.grpc import server_interceptor
3132
# from opencensus.trace.samplers import always_on
@@ -56,7 +57,12 @@
5657
)
5758
template = env.get_template('confirmation.html')
5859

59-
class EmailService(demo_pb2_grpc.EmailServiceServicer):
60+
class BaseEmailService(demo_pb2_grpc.EmailServiceServicer):
61+
def Check(self, request, context):
62+
return health_pb2.HealthCheckResponse(
63+
status=health_pb2.HealthCheckResponse.SERVING)
64+
65+
class EmailService(BaseEmailService):
6066
def __init__(self):
6167
raise Exception('cloud mail client not implemented')
6268
super().__init__()
@@ -79,7 +85,6 @@ def send_email(client, email_address, content):
7985
"html_body": content
8086
}
8187
)
82-
8388
print("Message sent: {}".format(response.rfc822_message_id))
8489

8590
def SendOrderConfirmation(self, request, context):
@@ -104,18 +109,30 @@ def SendOrderConfirmation(self, request, context):
104109

105110
return demo_pb2.Empty()
106111

107-
class DummyEmailService(demo_pb2_grpc.EmailServiceServicer):
112+
class DummyEmailService(BaseEmailService):
108113
def SendOrderConfirmation(self, request, context):
109114
print('A request to send order confirmation email to {} has been received.'.format(request.email))
110115
return demo_pb2.Empty()
111116

117+
class HealthCheck():
118+
def Check(self, request, context):
119+
return health_pb2.HealthCheckResponse(
120+
status=health_pb2.HealthCheckResponse.SERVING)
121+
112122
def start(dummy_mode):
113123
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))#, interceptors=(tracer_interceptor,))
124+
service = None
114125
if dummy_mode:
115-
demo_pb2_grpc.add_EmailServiceServicer_to_server(DummyEmailService(), server)
126+
service = DummyEmailService()
116127
else:
117-
raise Exception('non-dummy mode not implemented')
118-
server.add_insecure_port('[::]:8080')
128+
raise Exception('non-dummy mode not implemented yet')
129+
130+
demo_pb2_grpc.add_EmailServiceServicer_to_server(service, server)
131+
health_pb2_grpc.add_HealthServicer_to_server(service, server)
132+
133+
port = os.environ.get('PORT', "8080")
134+
print("listening on port: "+port)
135+
server.add_insecure_port('[::]:'+port)
119136
server.start()
120137
try:
121138
while True:
@@ -125,5 +142,5 @@ def start(dummy_mode):
125142

126143

127144
if __name__ == '__main__':
128-
print('Starting the email service in dummy mode.')
145+
print('starting the email service in dummy mode.')
129146
start(dummy_mode = True)

src/emailservice/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ google-cloud-trace==0.19.0
99
googleapis-common-protos==1.5.3
1010
grpc-google-iam-v1==0.11.4
1111
grpcio==1.12.1
12+
grpcio-health-checking==1.14.1
1213
grpcio-tools==1.12.1
1314
idna==2.7
1415
Jinja2==2.10

src/recommendationservice/Dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ FROM grpc/python:1.0
33
# show python logs as they occur
44
ENV PYTHONUNBUFFERED=0
55

6+
# download the grpc health probe
7+
RUN GRPC_HEALTH_PROBE_VERSION=v0.1.0-alpha.1 && \
8+
wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \
9+
chmod +x /bin/grpc_health_probe
10+
611
# get packages
712
WORKDIR /recommendationservice
813
COPY requirements.txt requirements.txt

0 commit comments

Comments
 (0)