SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 2977847a authored by Jean-Sébastien Caux's avatar Jean-Sébastien Caux
Browse files

Renew attempt at haystack reindex via celery

parent b4e118ea
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ __license__ = "AGPL v3" ...@@ -4,7 +4,7 @@ __license__ = "AGPL v3"
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from haystack import signals from haystack import connection_router, connections, signals
from haystack.exceptions import NotHandled from haystack.exceptions import NotHandled
from SciPost_v1.celery import app from SciPost_v1.celery import app
...@@ -13,14 +13,11 @@ from submissions.models import Submission ...@@ -13,14 +13,11 @@ from submissions.models import Submission
@app.task(name='signalprocessors.remove_object_indexes') @app.task(name='signalprocessors.remove_object_indexes')
def remove_objects_indexes(processor_type_id, processor_id, def remove_objects_indexes(sender_type_id, object_type_id, object_id):
sender_type_id, object_type_id, object_id):
""" """
Given a set of `objects` model instances, remove them from the index as preparation Given a set of `objects` model instances, remove them from the index as preparation
for the new index. 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) sender = ContentType.objects.get_for_id(sender_type_id)
object_type = ContentType.objects.get_for_id(object_type_id) object_type = ContentType.objects.get_for_id(object_type_id)
instance = object_type.get_object_for_this_type(pk=object_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, ...@@ -37,7 +34,7 @@ def remove_objects_indexes(processor_type_id, processor_id,
objects = [instance] objects = [instance]
try: try:
using_backends = processor.connection_router.for_write(instance=objects[0]) using_backends = connection_router.for_write(instance=objects[0])
except IndexError: except IndexError:
# No submissions given, stop processing here # No submissions given, stop processing here
return None return None
...@@ -45,7 +42,7 @@ def remove_objects_indexes(processor_type_id, processor_id, ...@@ -45,7 +42,7 @@ def remove_objects_indexes(processor_type_id, processor_id,
for instance in objects: for instance in objects:
for using in using_backends: for using in using_backends:
try: 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) index.remove_object(instance, using=using)
except NotHandled: except NotHandled:
# TODO: Maybe log it or let the exception bubble? # TODO: Maybe log it or let the exception bubble?
...@@ -53,26 +50,23 @@ def remove_objects_indexes(processor_type_id, processor_id, ...@@ -53,26 +50,23 @@ def remove_objects_indexes(processor_type_id, processor_id,
@app.task(name='signalprocessors.update_instance_indexes') @app.task(name='signalprocessors.update_instance_indexes')
def update_instance_indexes(processor_type_id, processor_id, def update_instance_indexes(sender_type_id, object_type_id, object_id):
sender_type_id, object_type_id, object_id):
""" """
Given an individual model instance, update its entire indexes. 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) sender = ContentType.objects.get_for_id(sender_type_id)
object_type = ContentType.objects.get_for_id(object_type_id) object_type = ContentType.objects.get_for_id(object_type_id)
instance = object_type.get_object_for_this_type(pk=object_id) instance = object_type.get_object_for_this_type(pk=object_id)
try: try:
using_backends = processor.connection_router.for_write(instance=instance) using_backends = connection_router.for_write(instance=instance)
except IndexError: except IndexError:
# No valid instance given, stop processing here # No valid instance given, stop processing here
return None return None
for using in using_backends: for using in using_backends:
try: try:
index = processor.connections[using].get_unified_index().get_index(sender) index = connections[using].get_unified_index().get_index(sender)
index.update(using=using) index.update(using=using)
except NotHandled: except NotHandled:
# TODO: Maybe log it or let the exception bubble? # TODO: Maybe log it or let the exception bubble?
...@@ -82,14 +76,10 @@ def update_instance_indexes(processor_type_id, processor_id, ...@@ -82,14 +76,10 @@ def update_instance_indexes(processor_type_id, processor_id,
class AutoSearchIndexingProcessor(signals.RealtimeSignalProcessor): class AutoSearchIndexingProcessor(signals.RealtimeSignalProcessor):
def handle_save(self, sender, instance, **kwargs): def handle_save(self, sender, instance, **kwargs):
# if not isinstance(instance, Notification): 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
# sender_type_id = ContentType.objects.get_for_model(sender).id instance_type_id = ContentType.objects.get_for_model(instance).id
# instance_type_id = ContentType.objects.get_for_model(instance).id chain = (
# chain = ( remove_objects_indexes.s(sender_type_id, instance_type_id, instance.id)
# remove_objects_indexes.s(processor_type_id, self.id, | update_instance_indexes.s(sender_type_id, instance_type_id, instance.id))
# sender_type_id, instance_type_id, instance.id) chain()
# | update_instance_indexes.s(processor_type_id, self.id,
# sender_type_id, instance_type_id, instance.id))
# chain()
pass
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment