Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More reliably disable cachalot for elasticsearch syncing #955

Merged
merged 1 commit into from
Sep 9, 2024
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
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
Loading