Skip to content

Commit 3931d50

Browse files
authored
Merge pull request #140 from negz/terminal
Shutdown gracefully on SIGTERM
2 parents 57df02a + cdaf669 commit 3931d50

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

crossplane/function/resource.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def update(r: fnv1.Resource, source: dict | structpb.Struct | pydantic.BaseModel
4545
# apiVersion is set to its default value 's3.aws.upbound.io/v1beta2'
4646
# (and not explicitly provided during initialization), it will be
4747
# excluded from the serialized output.
48-
data['apiVersion'] = source.apiVersion
49-
data['kind'] = source.kind
48+
data["apiVersion"] = source.apiVersion
49+
data["kind"] = source.kind
5050
r.resource.update(data)
5151
case structpb.Struct():
5252
# TODO(negz): Use struct_to_dict and update to match other semantics?

crossplane/function/runtime.py

+9-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
@@ -31,6 +32,8 @@
3132
fnv1beta1.DESCRIPTOR.services_by_name["FunctionRunnerService"].full_name,
3233
)
3334

35+
SHUTDOWN_GRACE_PERIOD_SECONDS = 5
36+
3437

3538
def load_credentials(tls_certs_dir: str) -> grpc.ServerCredentials:
3639
"""Load TLS credentials for a composition function gRPC server.
@@ -90,6 +93,11 @@ def serve(
9093

9194
server = grpc.aio.server()
9295

96+
loop.add_signal_handler(
97+
signal.SIGTERM,
98+
lambda: asyncio.ensure_future(server.stop(grace=SHUTDOWN_GRACE_PERIOD_SECONDS)),
99+
)
100+
93101
grpcv1.add_FunctionRunnerServiceServicer_to_server(function, server)
94102
grpcv1beta1.add_FunctionRunnerServiceServicer_to_server(
95103
BetaFunctionRunner(wrapped=function), server
@@ -116,7 +124,7 @@ async def start():
116124
try:
117125
loop.run_until_complete(start())
118126
finally:
119-
loop.run_until_complete(server.stop(grace=5))
127+
loop.run_until_complete(server.stop(grace=SHUTDOWN_GRACE_PERIOD_SECONDS))
120128
loop.close()
121129

122130

0 commit comments

Comments
 (0)