Skip to content

Commit 4567ca0

Browse files
1 parent 4ad65f2 commit 4567ca0

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

crossplane/function/runtime.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import asyncio
1818
import os
19+
import signal
1920

2021
import grpc
2122
from grpc_reflection.v1alpha import reflection
@@ -25,6 +26,7 @@
2526
import crossplane.function.proto.v1beta1.run_function_pb2 as fnv1beta1
2627
import crossplane.function.proto.v1beta1.run_function_pb2_grpc as grpcv1beta1
2728

29+
GRACE_PERIOD = 5
2830
SERVICE_NAMES = (
2931
reflection.SERVICE_NAME,
3032
fnv1.DESCRIPTOR.services_by_name["FunctionRunnerService"].full_name,
@@ -63,6 +65,8 @@ def load_credentials(tls_certs_dir: str) -> grpc.ServerCredentials:
6365
require_client_auth=True,
6466
)
6567

68+
async def stop(server, timeout):
69+
await server.stop(grace=timeout)
6670

6771
def serve(
6872
function: grpcv1.FunctionRunnerService,
@@ -90,6 +94,11 @@ def serve(
9094

9195
server = grpc.aio.server()
9296

97+
loop.add_signal_handler(
98+
signal.SIGTERM,
99+
lambda: asyncio.create_task(stop(server, timeout=GRACE_PERIOD))
100+
)
101+
93102
grpcv1.add_FunctionRunnerServiceServicer_to_server(function, server)
94103
grpcv1beta1.add_FunctionRunnerServiceServicer_to_server(
95104
BetaFunctionRunner(wrapped=function), server
@@ -116,7 +125,7 @@ async def start():
116125
try:
117126
loop.run_until_complete(start())
118127
finally:
119-
loop.run_until_complete(server.stop(grace=5))
128+
loop.run_until_complete(server.stop(grace=GRACE_PERIOD))
120129
loop.close()
121130

122131

0 commit comments

Comments
 (0)