Skip to content

Commit

Permalink
Merge pull request #955 from ImageMarkup/disable-cachalot
Browse files Browse the repository at this point in the history
More reliably disable cachalot for elasticsearch syncing
  • Loading branch information
danlamanna authored Sep 9, 2024
2 parents c473c70 + b1e9520 commit c2822b8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 9 additions & 5 deletions isic/core/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from typing import TYPE_CHECKING, Any

from cachalot.api import cachalot_disabled
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.query import QuerySet
Expand Down Expand Up @@ -108,13 +109,16 @@ def add_to_search_index(image: Image) -> None:
def bulk_add_to_search_index(qs: QuerySet[Image], chunk_size: int = 2_000) -> None:
from opensearchpy.helpers import parallel_bulk

# qs must be generated with with_elasticsearch_properties
# Use a generator for lazy evaluation
image_documents = (image.to_elasticsearch_document() for image in qs)

# The opensearch logger is very noisy when updating records,
# set it to warning during this operation.
with LoggingContext(logging.getLogger("opensearch"), level=logging.WARNING):
with (
LoggingContext(logging.getLogger("opensearch"), level=logging.WARNING),
cachalot_disabled(),
):
# qs must be generated with with_elasticsearch_properties
# Use a generator for lazy evaluation
image_documents = (image.to_elasticsearch_document() for image in qs.iterator())

for success, info in parallel_bulk(
client=get_elasticsearch_client(),
index=settings.ISIC_ELASTICSEARCH_INDEX,
Expand Down
4 changes: 1 addition & 3 deletions isic/core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import cast
import uuid

from cachalot.api import cachalot_disabled
from celery import shared_task
from django.conf import settings
from django.contrib.auth.models import User
Expand Down Expand Up @@ -60,8 +59,7 @@ def share_collection_with_users_task(collection_pk: int, grantor_pk: int, user_p
retry_kwargs={"max_retries": 15},
)
def sync_elasticsearch_index_task():
with cachalot_disabled():
bulk_add_to_search_index(Image.objects.with_elasticsearch_properties().iterator())
bulk_add_to_search_index(Image.objects.with_elasticsearch_properties())


@shared_task(soft_time_limit=1800, time_limit=1810)
Expand Down

0 comments on commit c2822b8

Please sign in to comment.