Skip to content

Commit 55c20f6

Browse files
authored
[Platform] Add drain callback (#57)
1 parent 24f2927 commit 55c20f6

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

nuclio_sdk/platform.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def __init__(
3535

3636
self._control_callback = on_control_callback
3737
self._termination_callback = None
38+
self._drain_callback = None
3839

3940
async def explicit_ack(self, qualified_offset):
4041
"""
@@ -51,6 +52,16 @@ async def explicit_ack(self, qualified_offset):
5152
"Cannot send explicit ack since control callback was not initialized"
5253
)
5354

55+
def set_drain_callback(self, callback):
56+
"""
57+
Register a callback to be called when the platform is draining (rebalance happening in stream).
58+
If already registered, the callback will be replaced.
59+
When called, the callback will be called with zero arguments.
60+
61+
:param callback: the callback to call when terminating
62+
"""
63+
self._drain_callback = callback
64+
5465
def set_termination_callback(self, callback):
5566
"""
5667
Register a callback to be called when the platform is terminating.
@@ -64,7 +75,6 @@ def set_termination_callback(self, callback):
6475
def call_function(
6576
self, function_name, event, node=None, timeout=None, service_name_override=None
6677
):
67-
6878
# get connection from provider
6979
connection = self._connection_provider(
7080
self._get_function_url(function_name, service_name_override),
@@ -106,7 +116,7 @@ def call_function(
106116
response_headers = {}
107117

108118
# get response headers as lowercase
109-
for (name, value) in response.getheaders():
119+
for name, value in response.getheaders():
110120
response_headers[name.lower()] = value
111121

112122
# if content type exists, use it
@@ -124,7 +134,6 @@ def call_function(
124134
)
125135

126136
def _get_function_url(self, function_name, service_name_override=None):
127-
128137
# local envs prefix namespace
129138
if self.kind == "local":
130139
service_name = service_name_override or "nuclio-{0}-{1}".format(
@@ -134,10 +143,14 @@ def _get_function_url(self, function_name, service_name_override=None):
134143
service_name = service_name_override or "nuclio-{0}".format(function_name)
135144
return "{0}:8080".format(service_name)
136145

137-
def _on_signal(self):
146+
def _on_signal(self, callback_type="termination"):
138147
"""
139-
When a signal is received, call the termination callback as a hook before exiting
148+
When a signal is received, call the termination/drain callback as a hook before exiting
140149
If not set, the callback will be a no-op
150+
151+
:arg callback_type:str - callback type, can be "termination" or "drain"
141152
"""
142-
if self._termination_callback:
153+
if callback_type == "termination" and self._termination_callback:
143154
self._termination_callback()
155+
elif callback_type == "drain" and self._drain_callback:
156+
self._drain_callback()

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def get_version():
4747
version = f.read().strip()
4848
if version.startswith("v"):
4949
version = version[1:]
50+
if version == "":
51+
return "0.0.0-dev0"
5052
return version
5153

5254

0 commit comments

Comments
 (0)