diff --git a/kernelci/api/helper.py b/kernelci/api/helper.py index 3828d5a341..59adc9a1e4 100644 --- a/kernelci/api/helper.py +++ b/kernelci/api/helper.py @@ -64,9 +64,16 @@ def unsubscribe_filters(self, sub_id): self._filters.pop(sub_id) self.api.unsubscribe(sub_id) - def receive_event_data(self, sub_id): - """Receive CloudEvent from Pub/Sub and return its data payload""" - return self.api.receive_event(sub_id).data + def receive_event_data(self, sub_id, block=True): + """Receive CloudEvent from Pub/Sub and return its data payload + If block is False, on receiving an "keep-alive" event, + such as "BEEP" ping, it will return None instead of the data. + Without this, it will block until an event is received. + """ + event = self.api.receive_event(sub_id, block=block) + if event is None: + return None + return event.data def pop_event_data(self, list_name): """Receive CloudEvent from Redis list and return its data payload""" diff --git a/kernelci/api/latest.py b/kernelci/api/latest.py index 3765c9c7e0..e233719d9a 100644 --- a/kernelci/api/latest.py +++ b/kernelci/api/latest.py @@ -163,7 +163,7 @@ def unsubscribe(self, sub_id: int): def send_event(self, channel: str, data): self._post('/'.join(['publish', channel]), data) - def receive_event(self, sub_id: int): + def receive_event(self, sub_id: int, block: bool = True): path = '/'.join(['listen', str(sub_id)]) while True: resp = self._get(path) @@ -172,6 +172,10 @@ def receive_event(self, sub_id: int): continue event = from_json(data) if event.data == 'BEEP': + if not block: + # If block is False, return None + # for semi-nonblocking operation + return None continue return event