Skip to content
Open
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
10 changes: 9 additions & 1 deletion mayan/apps/dynamic_search/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ def task_deindex_instance(self, app_label, model_name, object_id):
logger.info('Executing')

Model = apps.get_model(app_label=app_label, model_name=model_name)
instance = Model._meta.default_manager.get(pk=object_id)

try:
instance = Model._meta.default_manager.get(pk=object_id)
except Model.DoesNotExist:
logger.warning(
'Object %s with pk=%s no longer exists; '
'skipping de-indexing.', Model, object_id
)
return

try:
SearchBackend.get_instance().deindex_instance(instance=instance)
Expand Down
27 changes: 27 additions & 0 deletions mayan/apps/dynamic_search/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from mayan.apps.testing.tests.base import BaseTestCase

from ..classes import SearchBackend, SearchModel
from ..tasks import task_deindex_instance

from .mixins import SearchTaskTestMixin, SearchTestMixin

Expand Down Expand Up @@ -60,3 +61,29 @@ def test_task_reindex_backend(self):
search_terms=self._test_objects[0].test_field
)
self.assertTrue(self._test_objects[0] in queryset)

def test_task_deindex_instance_object_already_deleted(self):
"""
task_deindex_instance must not crash when the object no longer exists.

This is a race condition that occurs when a document is deleted (e.g.
from the trash) and the de-index task executes after the DELETE has
already committed. Before this fix the task raised DoesNotExist and
logged a CRITICAL traceback; now it logs a warning and returns cleanly.
Regression test for GitLab issue #1185.
"""
obj = self._test_objects[0]
app_label = obj._meta.app_label
model_name = obj._meta.model_name
pk = obj.pk

obj.delete()

# Must complete without raising any exception.
task_deindex_instance.apply_async(
kwargs={
'app_label': app_label,
'model_name': model_name,
'object_id': pk,
}
).get()