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
28 changes: 15 additions & 13 deletions h/services/annotation_authority_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dataclasses import dataclass

from celery import Celery
from kombu import Connection

from h.services.annotation_read import AnnotationReadService

Expand Down Expand Up @@ -54,19 +55,20 @@ def publish(self, event_action: str, annotation_id: str) -> None:
}

authority_celery = Celery(annotation.authority)
authority_celery.conf.broker_url = authority_queue_config.broker_url

authority_celery.send_task(
authority_queue_config.task_name,
queue=authority_queue_config.queue_name,
kwargs={"event": payload},
)
LOG.info(
"Published event %s for annotation %s to %s",
event_action,
annotation.id,
annotation.authority,
)
with Connection(authority_queue_config.broker_url) as connection:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the context manager here to call .release()

authority_celery.send_task(
authority_queue_config.task_name,
queue=authority_queue_config.queue_name,
# We need to pass the connection explicitly to avoid using the default connection / broker
connection=connection,
kwargs={"event": payload},
)
LOG.info(
"Published event %s for annotation %s to %s",
event_action,
annotation.id,
annotation.authority,
)

def _parse_authority_queue_config(
self, config_json: str | None
Expand Down
17 changes: 14 additions & 3 deletions tests/unit/h/services/annotation_authority_queue_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ def test_publish_with_no_mentions(self, svc, Celery, annotation_read_service):
Celery.assert_not_called()

def test_publish(
self, svc, Celery, annotation_read_service, annotation_json_service, annotation
self,
svc,
Celery,
annotation_read_service,
annotation_json_service,
annotation,
Connection,
):
annotation_read_service.get_annotation_by_id.return_value = annotation

Expand All @@ -51,12 +57,13 @@ def test_publish(
with_metadata=True,
)
Celery.assert_called_once_with(
annotation_read_service.get_annotation_by_id.return_value.authority
annotation_read_service.get_annotation_by_id.return_value.authority,
)
Celery.return_value.conf.broker_url = "broker_url"
Connection.assert_called_once_with("url")
Celery.return_value.send_task.assert_called_once_with(
"task",
queue="queue",
connection=Connection.return_value.__enter__.return_value,
kwargs={
"event": {
"action": "create",
Expand Down Expand Up @@ -87,6 +94,10 @@ def test_parse_config_with_config(self, svc, valid_config):
def Celery(self, patch):
return patch("h.services.annotation_authority_queue.Celery")

@pytest.fixture
def Connection(self, patch):
return patch("h.services.annotation_authority_queue.Connection")

@pytest.fixture
def valid_config(self):
return json.dumps(
Expand Down
Loading