Skip to content

Commit

Permalink
Collection reference remove timing out
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Feb 26, 2024
1 parent ef2527f commit a67d5c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
16 changes: 12 additions & 4 deletions core/collections/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from core.common.models import ConceptContainerModel, BaseResourceModel
from core.common.search import CustomESSearch
from core.common.tasks import seed_children_to_expansion, batch_index_resources, index_expansion_concepts, \
index_expansion_mappings
index_expansion_mappings, readd_references_to_expansion_on_references_removal
from core.common.utils import drop_version, to_owner_uri, generate_temp_version, es_id_in, \
get_resource_class_from_resource_name, to_snake_case, \
es_to_pks, batch_qs, split_list_by_condition, decode_string, is_canonical_uri, encode_string, \
Expand Down Expand Up @@ -1055,15 +1055,18 @@ def delete_references(self, references):

index_concepts = False
index_mappings = False
any_ref_with_resources = False
for reference in refs:
concepts = reference.concepts
if concepts.exists():
any_ref_with_resources = True
index_concepts = True
filters = {'versioned_object_id__in': list(concepts.values_list('versioned_object_id', flat=True))}
self.concepts.set(self.concepts.exclude(**filters))
batch_index_resources.apply_async(('concept', filters), queue='indexing')
mappings = reference.mappings
if mappings.exists():
any_ref_with_resources = True
index_mappings = True
filters = {'versioned_object_id__in': list(mappings.values_list('versioned_object_id', flat=True))}
self.mappings.set(self.mappings.exclude(**filters))
Expand All @@ -1074,9 +1077,14 @@ def delete_references(self, references):
if index_mappings:
self.index_mappings()

references_to_readd = self.collection_version.references.exclude(
id__in=[ref.id for ref in self.to_ref_list(references)])
self.add_references(references_to_readd, True, True, False)
if any_ref_with_resources:
reference_ids_to_readd = self.collection_version.references.exclude(
id__in=[ref.id for ref in self.to_ref_list(references)]).values_list('id', flat=True)
readd_task = readd_references_to_expansion_on_references_removal
if get(settings, 'TEST_MODE', False):
readd_task.apply_async((self.id, reference_ids_to_readd), queue='default')
else:
readd_task(self.id, reference_ids_to_readd)

def delete_expressions(self, expressions): # Deprecated: Old way, must use delete_references instead
concepts_filters = None
Expand Down
9 changes: 9 additions & 0 deletions core/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,3 +720,12 @@ def calculate_checksums(resource_type, resource_id):
instance.get_latest_version().set_checksums()
if not instance.is_versioned_object:
instance.versioned_object.set_checksums()


@app.task(ignore_result=True)
def readd_references_to_expansion_on_references_removal(expansion_id, reference_ids):
from core.collections.models import Expansion, CollectionReference
expansion = Expansion.objects.filter(id=expansion_id).first()
if expansion:
references = CollectionReference.objects.filter(id__in=reference_ids)
expansion.add_references(references, True, True, False)

0 comments on commit a67d5c4

Please sign in to comment.