Skip to content

Commit

Permalink
Add healthcheck for gRPC
Browse files Browse the repository at this point in the history
  • Loading branch information
radovanZRasa committed Jun 18, 2024
1 parent b49f874 commit 5410a90
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 10 deletions.
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@ cleanup-generated-changelog:
release:
poetry run python scripts/release.py

download-protoc-compiler:
curl -0L https://github.com/protocolbuffers/protobuf/releases/download/v25.0/protoc-25.0-osx-aarch_64.zip --output protoc-25.0-osx-aarch_64.zip

generate-grpc:
python -m grpc_tools.protoc \
-Irasa_sdk/grpc_py=./proto \
--python_out=. \
--grpc_python_out=. \
--pyi_out=. \
proto/action_webhook.proto
proto/action_webhook.proto \
proto/health.proto
11 changes: 11 additions & 0 deletions proto/health.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package grpc.health.v1;

message HealthCheckRequest {}

message HealthCheckResponse {}

service Health {
rpc Check(HealthCheckRequest) returns (HealthCheckResponse);
}
30 changes: 30 additions & 0 deletions rasa_sdk/grpc_py/health_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions rasa_sdk/grpc_py/health_pb2.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from typing import ClassVar as _ClassVar

DESCRIPTOR: _descriptor.FileDescriptor

class HealthCheckRequest(_message.Message):
__slots__ = []
def __init__(self) -> None: ...

class HealthCheckResponse(_message.Message):
__slots__ = []
def __init__(self) -> None: ...
66 changes: 66 additions & 0 deletions rasa_sdk/grpc_py/health_pb2_grpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc

from rasa_sdk.grpc_py import health_pb2 as rasa__sdk_dot_grpc__py_dot_health__pb2


class HealthStub(object):
"""Missing associated documentation comment in .proto file."""

def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.Check = channel.unary_unary(
'/grpc.health.v1.Health/Check',
request_serializer=rasa__sdk_dot_grpc__py_dot_health__pb2.HealthCheckRequest.SerializeToString,
response_deserializer=rasa__sdk_dot_grpc__py_dot_health__pb2.HealthCheckResponse.FromString,
)


class HealthServicer(object):
"""Missing associated documentation comment in .proto file."""

def Check(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')


def add_HealthServicer_to_server(servicer, server):
rpc_method_handlers = {
'Check': grpc.unary_unary_rpc_method_handler(
servicer.Check,
request_deserializer=rasa__sdk_dot_grpc__py_dot_health__pb2.HealthCheckRequest.FromString,
response_serializer=rasa__sdk_dot_grpc__py_dot_health__pb2.HealthCheckResponse.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'grpc.health.v1.Health', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))


# This class is part of an EXPERIMENTAL API.
class Health(object):
"""Missing associated documentation comment in .proto file."""

@staticmethod
def Check(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/grpc.health.v1.Health/Check',
rasa__sdk_dot_grpc__py_dot_health__pb2.HealthCheckRequest.SerializeToString,
rasa__sdk_dot_grpc__py_dot_health__pb2.HealthCheckResponse.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
14 changes: 8 additions & 6 deletions rasa_sdk/grpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import signal

import asyncio
import ssl

import grpc
import logging
Expand Down Expand Up @@ -171,12 +172,13 @@ async def run_grpc(
)
if ssl_certificate and ssl_keyfile:
# Use SSL/TLS if certificate and key are provided
# ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
# ssl_context.load_cert_chain(
# ssl_certificate,
# keyfile=ssl_keyfile,
# password=ssl_password if ssl_password else None,
# )
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(
ssl_certificate,
keyfile=ssl_keyfile,
password=ssl_password if ssl_password else None,
)
grpc.ssl_channel_credentials()
private_key = open(ssl_keyfile, "rb").read()
certificate_chain = open(ssl_certificate, "rb").read()
logger.info(f"Starting gRPC server with SSL support on port {port}")
Expand Down

0 comments on commit 5410a90

Please sign in to comment.