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"
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()
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