Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rele/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.16.0b3"
__version__ = "1.16.0b4"

try:
import django
Expand Down
4 changes: 4 additions & 0 deletions rele/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ def consume(self, subscription_name, callback, scheduler):
subscription_path, callback=callback, scheduler=scheduler
)

def close(self):
"""Close the SubscriberClient."""
self._client.close()


class Publisher:
"""The Publisher Class
Expand Down
6 changes: 5 additions & 1 deletion rele/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def stop(self, signal=None, frame=None):

This function has two purposes:

1. Cancel all the futures created.
1. Cancel all the futures created and terminate the subscriber.
2. And close all the database connections
opened by Django. Even though we cancel the connections
for every execution of the callback, we want to be sure
Expand All @@ -138,10 +138,14 @@ def stop(self, signal=None, frame=None):
:param frame: Needed for `signal.signal <https://docs.python.org/3/library/signal.html#signal.signal>`_ # noqa
"""
run_middleware_hook("pre_worker_stop", self._subscriptions)
logger.debug(f"[stop] cancel all futures")
for future in self._futures.values():
future.cancel()
future.result()

logger.debug(f"[stop] close subscriber")
self._subscriber.close()

run_middleware_hook("post_worker_stop")
sys.exit(0)

Expand Down
12 changes: 12 additions & 0 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ def test_wait_forevers_for_custom_time_period_when_called_with_argument(

mock_wait_forever.assert_called_once()

@patch.object(Worker, "_wait_forever")
def test_stop_cancels_futures_and_closes_subscriber(
self, mock_wait_forever, mock_consume, mock_create_subscription, worker
):
worker.run_forever()

with pytest.raises(SystemExit):
worker.stop()

assert worker._futures[sub_stub]._state == FINISHED
assert worker._subscriber._client._closed is True

@patch("rele.contrib.django_db_middleware.db.connections.close_all")
def test_stop_closes_db_connections(self, mock_db_close_all, config, worker):
config.middleware = ["rele.contrib.DjangoDBMiddleware"]
Expand Down