Skip to content

Commit 4fc0560

Browse files
authored
Add a method for waiting for unpublished events (#42)
Also update the README to reflect this update.
1 parent b4fe63e commit 4fc0560

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Publishing an event
8181
ACTIVITY_TRIGGERED = EiffelActivityTriggeredEvent()
8282
ACTIVITY_TRIGGERED.data.add("name", "Test activity")
8383
PUBLISHER.send_event(ACTIVITY_TRIGGERED)
84+
PUBLISHER.wait_for_unpublished_events()
8485
8586
Deprecation of routing key
8687
--------------------------

docs/examples.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ This code snippet will subscribe to an ActivityStarted in order to publish an Ac
2424

2525
.. code-block:: python
2626
27-
import time
2827
from eiffellib.subscribers import RabbitMQSubscriber
2928
from eiffellib.publishers import RabbitMQPublisher
3029
from eiffellib.events import (EiffelActivityTriggeredEvent,
@@ -68,7 +67,7 @@ This code snippet will subscribe to an ActivityStarted in order to publish an Ac
6867
PUBLISHER.send_event(ACTIVITY_STARTED)
6968
7069
# Wait for event to be received by 'callback'.
71-
time.sleep(1)
70+
PUBLISHER.wait_for_unpublished_events()
7271
7372
Activity
7473
--------
@@ -80,7 +79,6 @@ An activity is just a callable which will send ActivityTriggered, Started and Fi
8079
.. code-block:: python
8180
8281
import os
83-
import time
8482
from eiffellib.subscribers import RabbitMQSubscriber
8583
from eiffellib.publishers import RabbitMQPublisher
8684
from eiffellib.events import EiffelAnnouncementPublishedEvent
@@ -117,4 +115,4 @@ An activity is just a callable which will send ActivityTriggered, Started and Fi
117115
PUBLISHER.send_event(ANNOUNCEMENT)
118116
119117
# Wait for event to be received by 'callback'.
120-
time.sleep(1)
118+
PUBLISHER.wait_for_unpublished_events()

eiffellib/publishers/eiffel_publisher.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020 Axis Communications AB.
1+
# Copyright 2020-2022 Axis Communications AB.
22
#
33
# For a full list of individual contributors, please see the commit history.
44
#
@@ -46,6 +46,15 @@ def send(self, msg):
4646
"""
4747
raise NotImplementedError
4848

49+
def wait_for_unpublished_events(timeout=60):
50+
"""Wait for all unpublished events to become published.
51+
52+
:raises TimeoutError: If the timeout is reached, this will be raised.
53+
:param timeout: A timeout, in seconds, to wait before exiting.
54+
:type timeout: int
55+
"""
56+
raise NotImplementedError
57+
4958
def close(self):
5059
"""Close down publisher. Override if special actions are required."""
5160
self.running = False

eiffellib/publishers/rabbitmq_publisher.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,27 @@ def _confirm_delivery(self, method_frame):
184184
len(self._deliveries), self._acks, self._nacks)
185185
_LOG.debug(f"[{current_thread().getName()}] '_confirm_delivery' Lock released")
186186

187+
def wait_for_unpublished_events(self, timeout=60):
188+
"""Wait for all unpublished events to become published.
189+
190+
For the RabbitMQ publisher an event becomes published if the
191+
broker (not the consumer) responds with an ACK.
192+
193+
:raises TimeoutError: If the timeout is reached, this will be raised.
194+
:param timeout: A timeout, in seconds, to wait before exiting.
195+
:type timeout: int
196+
"""
197+
end = time.time() + timeout
198+
deliveries = 0
199+
while time.time() < end:
200+
time.sleep(0.1)
201+
deliveries = len(self._deliveries) + len(self._nacked_deliveries)
202+
if deliveries == 0:
203+
break
204+
else:
205+
raise TimeoutError("Timeout (%0.2fs) while waiting for events to publish"
206+
" (%d still unpublished)" % (timeout, deliveries))
207+
187208
def send_event(self, event, block=True):
188209
"""Validate and send an eiffel event to the rabbitmq server.
189210

0 commit comments

Comments
 (0)