Skip to content

Commit 6430c4b

Browse files
committed
Delete indexes
1 parent 7f214fe commit 6430c4b

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

readthedocs/projects/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ def delete(self, *args, **kwargs): # pylint: disable=arguments-differ
480480
args=[(self.doc_path,)],
481481
)
482482

483+
# Remove extra resources
483484
tasks.clean_project_resources(self)
484485

485486
super().delete(*args, **kwargs)

readthedocs/projects/tasks.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -1621,8 +1621,9 @@ def _sync_imported_files(version, build, changed_files):
16211621
# Remove old HTMLFiles from ElasticSearch
16221622
remove_indexed_files(
16231623
model=HTMLFile,
1624-
version=version,
1625-
build=build,
1624+
project_slug=version.project.slug,
1625+
version_slug=version.slug,
1626+
build_id=build,
16261627
)
16271628

16281629
# Delete SphinxDomain objects from previous versions
@@ -1844,6 +1845,17 @@ def remove_build_storage_paths(paths):
18441845
storage.delete_directory(storage_path)
18451846

18461847

1848+
@app.task(queue='web')
1849+
def remove_search_indexes(project_slug, version_slug=None):
1850+
"""Wrapper around `remove_indexed_files to make it a task."""
1851+
# Remove ES indexes
1852+
remove_indexed_files(
1853+
model=HTMLFile,
1854+
project_slug=project_slug,
1855+
version_slug=version_slug,
1856+
)
1857+
1858+
18471859
def clean_project_resources(project, version=None):
18481860
"""
18491861
Delete all extra resources used by `version` of `project`.
@@ -1853,16 +1865,27 @@ def clean_project_resources(project, version=None):
18531865
- Artifacts from storage.
18541866
- Search indexes from ES.
18551867
1856-
:param version: Version instance. if isn't given,
1868+
:param version: Version instance. If isn't given,
18571869
all resources of `project` will be deleted.
1870+
1871+
.. note::
1872+
This function is usually called just before deleting project.
1873+
Make sure to not depend on the project object inside the tasks.
18581874
"""
1875+
# Remove storage paths
18591876
storage_paths = []
18601877
if version:
18611878
version.get_storage_paths()
18621879
else:
18631880
storage_paths = project.get_storage_paths()
18641881
remove_build_storage_paths.delay(storage_paths)
18651882

1883+
# Remove indexes
1884+
remove_search_indexes.delay(
1885+
project_slug=project.slug,
1886+
version_slug=version.slug if version else None,
1887+
)
1888+
18661889

18671890
@app.task(queue='web')
18681891
def sync_callback(_, version_pk, commit, build, *args, **kwargs):

readthedocs/search/utils.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,41 @@ def index_new_files(model, version, build):
4343
log.exception('Unable to index a subset of files. Continuing.')
4444

4545

46-
def remove_indexed_files(model, version, build):
46+
def remove_indexed_files(model, project_slug, version_slug=None, build_id=None):
4747
"""
48-
Remove files from the version from the search index.
48+
Remove files from `version_slug` of `project_slug` from the search index.
4949
50-
This excludes files from the current build.
50+
:param model: Class of the model to be deleted.
51+
:param project_slug: Project slug.
52+
:param version_slug: Version slug. If isn't given,
53+
all index from `project` are deleted.
54+
:param build_id: Build id. If isn't given, all index from `version` are deleted.
5155
"""
5256

5357
if not DEDConfig.autosync_enabled():
5458
log.info(
5559
'Autosync disabled, skipping removal from the search index for: %s:%s',
56-
version.project.slug,
57-
version.slug,
60+
project_slug,
61+
version_slug,
5862
)
5963
return
6064

6165
try:
6266
document = list(registry.get_documents(models=[model]))[0]
6367
log.info(
6468
'Deleting old files from search index for: %s:%s',
65-
version.project.slug,
66-
version.slug,
69+
project_slug,
70+
version_slug,
6771
)
68-
(
72+
documents = (
6973
document().search()
70-
.filter('term', project=version.project.slug)
71-
.filter('term', version=version.slug)
72-
.exclude('term', build=build)
73-
.delete()
74+
.filter('term', project=project_slug)
7475
)
76+
if version_slug:
77+
documents.filter('term', version=version_slug)
78+
if build_id:
79+
documents.exclude('term', build=build_id)
80+
documents.delete()
7581
except Exception:
7682
log.exception('Unable to delete a subset of files. Continuing.')
7783

0 commit comments

Comments
 (0)