Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/health check #276

Merged
merged 7 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 9 additions & 0 deletions django_socio_grpc/management/commands/grpcrunaioserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from django_socio_grpc.settings import grpc_settings
from django_socio_grpc.utils.ssl_credentials import get_server_credentials

from grpc_health.v1 import health_pb2_grpc
from grpc_health.v1 import health

logger = logging.getLogger("django_socio_grpc.internal")


Expand Down Expand Up @@ -81,6 +84,12 @@ async def _serve(self):
interceptors=grpc_settings.SERVER_INTERCEPTORS,
options=grpc_settings.SERVER_OPTIONS,
)

if grpc_settings.ENABLE_HEALTH_CHECK:
health_pb2_grpc.add_HealthServicer_to_server(
health.aio.HealthServicer(), server
)

# INFO - AM - 05/04/202 - Make sure that ROOT_HANDLERS_HOOK is called with correct context to be able to use SynchronousOnlyOperation in it
if asyncio.iscoroutinefunction(grpc_settings.ROOT_HANDLERS_HOOK):
await grpc_settings.ROOT_HANDLERS_HOOK(server)
Expand Down
8 changes: 8 additions & 0 deletions django_socio_grpc/management/commands/grpcrunserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

from django_socio_grpc.settings import grpc_settings

from grpc_health.v1 import health
from grpc_health.v1 import health_pb2_grpc

logger = logging.getLogger("django_socio_grpc.internal")


Expand Down Expand Up @@ -92,6 +95,11 @@ def _serve(self):
options=grpc_settings.SERVER_OPTIONS,
)

if grpc_settings.ENABLE_HEALTH_CHECK:
health_pb2_grpc.add_HealthServicer_to_server(
health.HealthServicer(), server
)

# ------------------------------------------------------------
# --- add PB2 GRPC handler (Services) to the gRPC server ---
grpc_settings.ROOT_HANDLERS_HOOK(server)
Expand Down
2 changes: 2 additions & 0 deletions django_socio_grpc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class FilterAndPaginationBehaviorOptions(str, Enum):
"DEFAULT_MESSAGE_NAME_CONSTRUCTOR": "django_socio_grpc.protobuf.message_name_constructor.DefaultMessageNameConstructor",
# Variable that indicate the plugins used in proto generation by default
"DEFAULT_GENERATION_PLUGINS": [],
# Enable the healthcheck service
"ENABLE_HEALTH_CHECK": False,
}


Expand Down
19 changes: 19 additions & 0 deletions docs/features/health-check.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.. _health_check:

Health Check
============

DSG has support for health checking through the standard service API `health/v1 <https://grpc.io/docs/guides/health-checking/>`_.

Usage
-----

The service is automatically added to the server if you enable it in the gRPC settings :

.. code-block:: python

GRPC_FRAMEWORK = {
"ENABLE_HEALTH_CHECK": True,
}

The default value is False, if you don't need it, no action is needed.
1 change: 1 addition & 0 deletions docs/features/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Features
logging
streaming
commands
health-check
16 changes: 15 additions & 1 deletion docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ See the documentation on django settings if your not familiar with it: `Django s
"FILTER_BEHAVIOR": "METADATA_STRICT",
"PAGINATION_BEHAVIOR": "METADATA_STRICT",
"DEFAULT_MESSAGE_NAME_CONSTRUCTOR": "django_socio_grpc.protobuf.message_name_constructor.DefaultMessageNameConstructor",
"ENABLE_HEALTH_CHECK": False,
}

.. _root-handler-hook-setting:
Expand Down Expand Up @@ -405,4 +406,17 @@ to specify plugin that are used globally for all actions.

.. code-block:: python

"DEFAULT_GENERATION_PLUGINS": ["django_socio_grpc.protobuf.generation_plugin.FilterGenerationPlugin"]
"DEFAULT_GENERATION_PLUGINS": ["django_socio_grpc.protobuf.generation_plugin.FilterGenerationPlugin"]

.. _settings-enable-health-check:

ENABLE_HEALTH_CHECK
^^^^^^^^^^^^^^^^^^^

A boolean indicating wether or not the health checking is enabled. Default is False.

For more informations see :ref:`the documentation <health_check>`

.. code-block:: python

"ENABLE_HEALTH_CHECK": False
18 changes: 16 additions & 2 deletions poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ django = ">=2.2"
djangorestframework = "*"
grpcio-tools = "^1.50.0"
lark-parser = "^0.12.0"
grpcio-health-checking = "*"

[tool.poetry.group.dev.dependencies]
pytest = "*"
Expand Down
Loading