Skip to content

Commit d2607e0

Browse files
authored
Merge pull request #5023 from safwanrahman/async_project
index project asynchronously
2 parents 14a1c66 + 2e36570 commit d2607e0

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

readthedocs/search/documents.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ProjectDocument(RTDDocTypeMixin, DocType):
2121
class Meta(object):
2222
model = Project
2323
fields = ('name', 'slug', 'description')
24+
ignore_signals = settings.ES_PROJECT_IGNORE_SIGNALS
2425

2526
url = fields.TextField(attr='get_absolute_url')
2627
users = fields.NestedField(properties={

readthedocs/search/signals.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"""We define custom Django signals to trigger before executing searches."""
22
from __future__ import absolute_import
33
import django.dispatch
4+
from django.db.models.signals import post_save, pre_delete
45
from django.dispatch import receiver
56
from django_elasticsearch_dsl.apps import DEDConfig
67
from django_elasticsearch_dsl.registries import registry
78

8-
from readthedocs.projects.models import HTMLFile
9+
from readthedocs.projects.models import HTMLFile, Project
910
from readthedocs.projects.signals import bulk_post_create, bulk_post_delete
10-
from readthedocs.search.documents import PageDocument
11+
from readthedocs.search.documents import PageDocument, ProjectDocument
1112
from readthedocs.search.tasks import index_objects_to_es
1213

1314
before_project_search = django.dispatch.Signal(providing_args=["body"])
@@ -35,3 +36,25 @@ def remove_html_file(instance_list, **_):
3536
# Do not index if autosync is disabled globally
3637
if DEDConfig.autosync_enabled():
3738
registry.delete(instance_list)
39+
40+
41+
@receiver(post_save, sender=Project)
42+
def index_project(instance, *args, **kwargs):
43+
kwargs = {
44+
'app_label': Project._meta.app_label,
45+
'model_name': Project.__name__,
46+
'document_class': str(ProjectDocument),
47+
'index_name': None, # No need to change the index name
48+
'objects_id': [instance.id],
49+
}
50+
51+
# Do not index if autosync is disabled globally
52+
if DEDConfig.autosync_enabled():
53+
index_objects_to_es.delay(**kwargs)
54+
55+
56+
@receiver(pre_delete, sender=Project)
57+
def remove_project(instance, *args, **kwargs):
58+
# Do not index if autosync is disabled globally
59+
if DEDConfig.autosync_enabled():
60+
registry.delete(instance)

readthedocs/settings/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ def USE_PROMOS(self): # noqa
329329
# Chunk size for elasticsearch reindex celery tasks
330330
ES_TASK_CHUNK_SIZE = 100
331331
ES_PAGE_IGNORE_SIGNALS = True
332+
ES_PROJECT_IGNORE_SIGNALS = True
332333

333334
# ANALYZER = 'analysis': {
334335
# 'analyzer': {

readthedocs/settings/test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class CommunityTestSettings(CommunityDevSettings):
1717
DEBUG = False
1818
TEMPLATE_DEBUG = False
1919
ES_PAGE_IGNORE_SIGNALS = False
20+
ES_PROJECT_IGNORE_SIGNALS = False
2021
ELASTICSEARCH_DSL_AUTOSYNC = False
2122
ELASTICSEARCH_DSL_AUTO_REFRESH = True
2223

0 commit comments

Comments
 (0)