From 2977847a6c3a306bab2c5407fdba123879293dbd Mon Sep 17 00:00:00 2001 From: "J.-S. Caux" <J.S.Caux@uva.nl> Date: Tue, 19 Mar 2019 22:20:40 +0100 Subject: [PATCH] Renew attempt at haystack reindex via celery --- SciPost_v1/signalprocessors.py | 38 +++++++++++++--------------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/SciPost_v1/signalprocessors.py b/SciPost_v1/signalprocessors.py index aa73771aa..877a2b7a5 100644 --- a/SciPost_v1/signalprocessors.py +++ b/SciPost_v1/signalprocessors.py @@ -4,7 +4,7 @@ __license__ = "AGPL v3" from django.contrib.contenttypes.models import ContentType -from haystack import signals +from haystack import connection_router, connections, signals from haystack.exceptions import NotHandled from SciPost_v1.celery import app @@ -13,14 +13,11 @@ from submissions.models import Submission @app.task(name='signalprocessors.remove_object_indexes') -def remove_objects_indexes(processor_type_id, processor_id, - sender_type_id, object_type_id, object_id): +def remove_objects_indexes(sender_type_id, object_type_id, object_id): """ Given a set of `objects` model instances, remove them from the index as preparation for the new index. """ - processor_type = ContentType.objects.get_for_id(processor_type_id) - processor = processor_type.get_object_for_this_type(pk=processor_id) sender = ContentType.objects.get_for_id(sender_type_id) object_type = ContentType.objects.get_for_id(object_type_id) instance = object_type.get_object_for_this_type(pk=object_id) @@ -37,7 +34,7 @@ def remove_objects_indexes(processor_type_id, processor_id, objects = [instance] try: - using_backends = processor.connection_router.for_write(instance=objects[0]) + using_backends = connection_router.for_write(instance=objects[0]) except IndexError: # No submissions given, stop processing here return None @@ -45,7 +42,7 @@ def remove_objects_indexes(processor_type_id, processor_id, for instance in objects: for using in using_backends: try: - index = processor.connections[using].get_unified_index().get_index(sender) + index = connections[using].get_unified_index().get_index(sender) index.remove_object(instance, using=using) except NotHandled: # TODO: Maybe log it or let the exception bubble? @@ -53,26 +50,23 @@ def remove_objects_indexes(processor_type_id, processor_id, @app.task(name='signalprocessors.update_instance_indexes') -def update_instance_indexes(processor_type_id, processor_id, - sender_type_id, object_type_id, object_id): +def update_instance_indexes(sender_type_id, object_type_id, object_id): """ Given an individual model instance, update its entire indexes. """ - processor_type = ContentType.objects.get_for_id(processor_type_id) - processor = processor_type.get_object_for_this_type(pk=processor_id) sender = ContentType.objects.get_for_id(sender_type_id) object_type = ContentType.objects.get_for_id(object_type_id) instance = object_type.get_object_for_this_type(pk=object_id) try: - using_backends = processor.connection_router.for_write(instance=instance) + using_backends = connection_router.for_write(instance=instance) except IndexError: # No valid instance given, stop processing here return None for using in using_backends: try: - index = processor.connections[using].get_unified_index().get_index(sender) + index = connections[using].get_unified_index().get_index(sender) index.update(using=using) except NotHandled: # TODO: Maybe log it or let the exception bubble? @@ -82,14 +76,10 @@ def update_instance_indexes(processor_type_id, processor_id, class AutoSearchIndexingProcessor(signals.RealtimeSignalProcessor): def handle_save(self, sender, instance, **kwargs): - # if not isinstance(instance, Notification): - # processor_type_id = ContentType.objects.get_for_model(self).id - # sender_type_id = ContentType.objects.get_for_model(sender).id - # instance_type_id = ContentType.objects.get_for_model(instance).id - # chain = ( - # remove_objects_indexes.s(processor_type_id, self.id, - # sender_type_id, instance_type_id, instance.id) - # | update_instance_indexes.s(processor_type_id, self.id, - # sender_type_id, instance_type_id, instance.id)) - # chain() - pass + if not isinstance(instance, Notification): + sender_type_id = ContentType.objects.get_for_model(sender).id + instance_type_id = ContentType.objects.get_for_model(instance).id + chain = ( + remove_objects_indexes.s(sender_type_id, instance_type_id, instance.id) + | update_instance_indexes.s(sender_type_id, instance_type_id, instance.id)) + chain() -- GitLab