Skip to content

Commit c042e70

Browse files
authored
Avoid unnecessary task calls for contentfiles if none exist (#3017)
1 parent 8f97082 commit c042e70

2 files changed

Lines changed: 37 additions & 12 deletions

File tree

learning_resources_search/plugins.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ def resource_run_unpublished(self, run):
169169
Args:
170170
run(LearningResourceRun): The Learning Resource run that was removed
171171
"""
172+
if not run.content_files.exists():
173+
return
172174
deindex_tasks = [
173175
tasks.deindex_run_content_files.si(run.id, unpublished_only=False),
174176
]
@@ -193,6 +195,8 @@ def content_files_loaded(self, run):
193195
Args:
194196
run(LearningResourceRun): The LearningResourceRun that was upserted
195197
"""
198+
if not run.content_files.exists():
199+
return
196200
if run.published:
197201
index_tasks = []
198202
if django_settings.QDRANT_ENABLE_INDEXING_PLUGIN_HOOKS:

learning_resources_search/plugins_test.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from learning_resources.factories import (
8+
ContentFileFactory,
89
LearningResourceFactory,
910
LearningResourceRunFactory,
1011
)
@@ -71,19 +72,23 @@ def test_search_index_plugin_resource_upserted(
7172

7273
@pytest.mark.django_db
7374
@pytest.mark.parametrize("resource_type", [COURSE_TYPE, PROGRAM_TYPE])
75+
@pytest.mark.parametrize("has_content_files", [True, False])
7476
def test_search_index_plugin_resource_unpublished(
75-
mocker, mock_search_index_helpers, resource_type
77+
mocker, mock_search_index_helpers, resource_type, has_content_files
7678
):
7779
"""The plugin function should remove a resource from the search index"""
7880
resource = LearningResourceFactory.create(resource_type=resource_type)
81+
if resource_type == COURSE_TYPE and has_content_files:
82+
for run in resource.runs.all():
83+
ContentFileFactory.create(run=run)
7984
unpublish_run_mock = mocker.patch(
8085
"learning_resources_search.plugins.tasks.deindex_run_content_files.si"
8186
)
8287
SearchIndexPlugin().resource_unpublished(resource)
8388
mock_search_index_helpers.mock_remove_learning_resource_immutable_signature.assert_called_once_with(
8489
resource.id, resource.resource_type
8590
)
86-
if resource_type == COURSE_TYPE:
91+
if resource_type == COURSE_TYPE and has_content_files:
8792
assert unpublish_run_mock.call_count == resource.runs.count()
8893
for run in resource.runs.all():
8994
unpublish_run_mock.assert_any_call(run.id, unpublished_only=False)
@@ -107,26 +112,42 @@ def test_search_index_plugin_resource_before_delete(
107112

108113

109114
@pytest.mark.django_db
110-
def test_search_index_plugin_resource_run_unpublished(mock_search_index_helpers):
115+
@pytest.mark.parametrize("has_content_files", [True, False])
116+
def test_search_index_plugin_resource_run_unpublished(
117+
mock_search_index_helpers, has_content_files
118+
):
111119
"""The plugin function should remove a run's contenfiles from the search index"""
112120
run = LearningResourceRunFactory.create()
121+
if has_content_files:
122+
ContentFileFactory.create(run=run)
113123
SearchIndexPlugin().resource_run_unpublished(run)
114-
mock_search_index_helpers.mock_remove_contentfiles_immutable_signature.assert_called_once_with(
115-
run.id,
116-
unpublished_only=False,
117-
)
124+
if has_content_files:
125+
mock_search_index_helpers.mock_remove_contentfiles_immutable_signature.assert_called_once_with(
126+
run.id,
127+
unpublished_only=False,
128+
)
129+
else:
130+
mock_search_index_helpers.mock_remove_contentfiles_immutable_signature.assert_not_called()
118131

119132

120133
@pytest.mark.django_db
121-
def test_search_index_plugin_resource_run_delete(mock_search_index_helpers):
134+
@pytest.mark.parametrize("has_content_files", [True, False])
135+
def test_search_index_plugin_resource_run_delete(
136+
mock_search_index_helpers, has_content_files
137+
):
122138
"""The plugin function should remove contenfiles from the index and delete the run"""
123139
run = LearningResourceRunFactory.create()
140+
if has_content_files:
141+
ContentFileFactory.create(run=run)
124142
run_id = run.id
125143
SearchIndexPlugin().resource_run_delete(run)
126-
mock_search_index_helpers.mock_remove_contentfiles_immutable_signature.assert_called_once_with(
127-
run_id,
128-
unpublished_only=False,
129-
)
144+
if has_content_files:
145+
mock_search_index_helpers.mock_remove_contentfiles_immutable_signature.assert_called_once_with(
146+
run_id,
147+
unpublished_only=False,
148+
)
149+
else:
150+
mock_search_index_helpers.mock_remove_contentfiles_immutable_signature.assert_not_called()
130151
assert LearningResourceRun.objects.filter(id=run_id).exists() is False
131152

132153

0 commit comments

Comments
 (0)