diff --git a/SciPost_v1/signalprocessors.py b/SciPost_v1/signalprocessors.py index cfab275a7cab394a367deb183abefb8a20818d0b..faac5943c7473fd53ca5a932483cc7451b5f1c57 100644 --- a/SciPost_v1/signalprocessors.py +++ b/SciPost_v1/signalprocessors.py @@ -5,10 +5,14 @@ __license__ = "AGPL v3" from haystack import signals from haystack.exceptions import NotHandled +from SciPost_v1.celery import app from submissions.models import Submission class AutoSearchIndexingProcessor(signals.RealtimeSignalProcessor): + + @app.task(bind=True, name='signalprocessors.remove_object_indexes', + serializer='pickle') def remove_objects_indexes(self, sender, objects): """ Given a set of `objects` model instances, remove them from the index as preparation @@ -29,6 +33,8 @@ class AutoSearchIndexingProcessor(signals.RealtimeSignalProcessor): # TODO: Maybe log it or let the exception bubble? pass + @app.task(bind=True, name='signalprocessors.update_instance_indexes', + serializer='pickle') def update_instance_indexes(self, sender, instance): """ Given an individual model instance, update its entire indexes. @@ -51,11 +57,20 @@ class AutoSearchIndexingProcessor(signals.RealtimeSignalProcessor): if isinstance(instance, Submission): # Submission have complex status handling, so a status change should lead to # more drastic reindexing. - self.remove_objects_indexes(sender, instance.thread.public()) - self.update_instance_indexes(sender, instance) + chain = ( + self.remove_objects_indexes.s(sender, instance.thread.public()) + | + self.update_instance_indexes.s(sender, instance) + ) + chain() + else: # Objects such as Reports, Comments, Commentaries, etc. may get rejected. This # does not remove them from the index. Therefore, do a complete rebuild_index # action on that specific instance every time the index signal is triggered. - self.remove_objects_indexes(sender, [instance]) - self.update_instance_indexes(sender, instance) + chain = ( + self.remove_objects_indexes.s(sender, [instance]) + | + self.update_instance_indexes.s(sender, instance) + ) + chain()