File tree 1 file changed +11
-1
lines changed
1 file changed +11
-1
lines changed Original file line number Diff line number Diff line change 16
16
17
17
import asyncio
18
18
import os
19
+ import signal
19
20
20
21
import grpc
21
22
from grpc_reflection .v1alpha import reflection
25
26
import crossplane .function .proto .v1beta1 .run_function_pb2 as fnv1beta1
26
27
import crossplane .function .proto .v1beta1 .run_function_pb2_grpc as grpcv1beta1
27
28
29
+ GRACE_PERIOD = 5
28
30
SERVICE_NAMES = (
29
31
reflection .SERVICE_NAME ,
30
32
fnv1 .DESCRIPTOR .services_by_name ["FunctionRunnerService" ].full_name ,
@@ -64,6 +66,10 @@ def load_credentials(tls_certs_dir: str) -> grpc.ServerCredentials:
64
66
)
65
67
66
68
69
+ async def _stop (server , timeout ): # noqa: ASYNC109
70
+ await server .stop (grace = timeout )
71
+
72
+
67
73
def serve (
68
74
function : grpcv1 .FunctionRunnerService ,
69
75
address : str ,
@@ -90,6 +96,10 @@ def serve(
90
96
91
97
server = grpc .aio .server ()
92
98
99
+ loop .add_signal_handler (
100
+ signal .SIGTERM , lambda : asyncio .create_task (_stop (server , timeout = GRACE_PERIOD ))
101
+ )
102
+
93
103
grpcv1 .add_FunctionRunnerServiceServicer_to_server (function , server )
94
104
grpcv1beta1 .add_FunctionRunnerServiceServicer_to_server (
95
105
BetaFunctionRunner (wrapped = function ), server
@@ -116,7 +126,7 @@ async def start():
116
126
try :
117
127
loop .run_until_complete (start ())
118
128
finally :
119
- loop .run_until_complete (server .stop (grace = 5 ))
129
+ loop .run_until_complete (server .stop (grace = GRACE_PERIOD ))
120
130
loop .close ()
121
131
122
132
You can’t perform that action at this time.
0 commit comments