Skip to content

Commit ca7e1f8

Browse files
committed
receive_event: Make it optionally semi-nonblocking
Right now receive_event is blocking, and even on receiving keep alive it stay inside its own busy loop, which makes impossible to implement proper scheduling, watchdogs, health monitoring. We make optional parameter, where on keep alive message, (sent each 45 sec by kernelci-api) it will return None. We keep backward compatible defaults, as lot of code is not expecting None in return. Signed-off-by: Denys Fedoryshchenko <[email protected]>
1 parent 799c9e7 commit ca7e1f8

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

kernelci/api/helper.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ def unsubscribe_filters(self, sub_id):
6464
self._filters.pop(sub_id)
6565
self.api.unsubscribe(sub_id)
6666

67-
def receive_event_data(self, sub_id):
68-
"""Receive CloudEvent from Pub/Sub and return its data payload"""
69-
return self.api.receive_event(sub_id).data
67+
def receive_event_data(self, sub_id, keep_alive=False):
68+
"""Receive CloudEvent from Pub/Sub and return its data payload
69+
If keep_alive is True, on receiving an "keep-alive" event,
70+
such as "BEEP" ping, it will return None instead of the data.
71+
Without this, it will block until an event is received.
72+
"""
73+
return self.api.receive_event(sub_id, keep_alive=keep_alive).data
7074

7175
def pop_event_data(self, list_name):
7276
"""Receive CloudEvent from Redis list and return its data payload"""

kernelci/api/latest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def unsubscribe(self, sub_id: int):
163163
def send_event(self, channel: str, data):
164164
self._post('/'.join(['publish', channel]), data)
165165

166-
def receive_event(self, sub_id: int):
166+
def receive_event(self, sub_id: int, keep_alive: bool = False):
167167
path = '/'.join(['listen', str(sub_id)])
168168
while True:
169169
resp = self._get(path)
@@ -172,6 +172,10 @@ def receive_event(self, sub_id: int):
172172
continue
173173
event = from_json(data)
174174
if event.data == 'BEEP':
175+
if keep_alive:
176+
# If keep_alive is True, return None
177+
# for "keep-alive" events
178+
return None
175179
continue
176180
return event
177181

0 commit comments

Comments
 (0)