Skip to content

Commit 1d27877

Browse files
committed
🔊(backend) add logs to bootstrap_elasticsearch command
Too few logs were displayed when running bootstrap_elasticsearch command.
1 parent 3c95686 commit 1d27877

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

src/richie/apps/search/index_manager.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
ElasticSearch indices utilities.
33
"""
44

5+
import logging
56
import re
67
from functools import reduce
78

@@ -16,6 +17,8 @@
1617
from .indexers import ES_INDICES
1718
from .text_indexing import ANALYSIS_SETTINGS
1819

20+
logger = logging.getLogger(__name__)
21+
1922

2023
def richie_bulk(actions):
2124
"""Wrap bulk helper to set default parameters."""
@@ -37,16 +40,16 @@ def get_indices_by_alias(existing_indices, alias):
3740
yield index, alias
3841

3942

40-
def perform_create_index(indexable, logger=None):
43+
def perform_create_index(indexable):
4144
"""
4245
Create a new index in ElasticSearch from an indexable instance
4346
"""
47+
logger.info("Creating the index %s...", indexable.index_name)
4448
# Create a new index name, suffixing its name with a timestamp
4549
new_index = f"{indexable.index_name:s}_{timezone.now():%Y-%m-%d-%Hh%Mm%S.%fs}"
4650

4751
# Create the new index
48-
if logger:
49-
logger.info(f'Creating a new Elasticsearch index "{new_index:s}"...')
52+
logger.info("Creating a new Elasticsearch index %s...", new_index)
5053
ES_INDICES_CLIENT.create(index=new_index)
5154

5255
# The index needs to be closed before we set an analyzer
@@ -63,23 +66,23 @@ def perform_create_index(indexable, logger=None):
6366
return new_index
6467

6568

66-
def regenerate_indices(logger):
69+
def regenerate_indices():
6770
"""
6871
Create new indices for our indexables and replace possible existing indices with
6972
a new one only once it has successfully built it.
7073
"""
74+
logger.info("Regenerating ES indices...")
7175
# Get all existing indices once; we'll look up into this list many times
7276
try:
7377
existing_indices = ES_INDICES_CLIENT.get_alias("*")
7478
except NotFoundError:
7579
# Provide a fallback empty list so we don't have to check for its existence later on
7680
existing_indices = []
7781

82+
logger.info("Creating new ES indices...")
7883
# Create a new index for each of those modules
7984
# NB: we're mapping perform_create_index which produces side-effects
80-
indices_to_create = zip(
81-
list(map(lambda ix: perform_create_index(ix, logger), ES_INDICES)), ES_INDICES
82-
)
85+
indices_to_create = [(perform_create_index(ix), ix) for ix in ES_INDICES]
8386

8487
# Prepare to alias them so they can be swapped-in for the previous versions
8588
actions_to_create_aliases = [
@@ -152,13 +155,16 @@ def perform_aliases_update():
152155
ES_INDICES_CLIENT.delete(index=useless_index, ignore=[400, 404])
153156

154157

155-
def store_es_scripts(logger=None):
158+
def store_es_scripts():
156159
"""
157160
Iterate over the indexers listed in the settings, import them, and store the scripts
158161
they define on their "scripts" key in ElasticSearch
159162
"""
160163
for indexer in ES_INDICES:
161164
for script_id, script_body in indexer.scripts.items():
162-
if logger:
163-
logger.info(f'Storing script "{script_id:s}"...')
165+
logger.info(
166+
"Storing script %s for indexer %s...",
167+
script_id,
168+
indexer.__name__,
169+
)
164170
ES_CLIENT.put_script(id=script_id, body=script_body)

src/richie/apps/search/indexers/persons.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
ElasticSearch person document management utilities
33
"""
44

5+
import logging
56
from collections import defaultdict
67

78
from django.conf import settings
@@ -20,6 +21,8 @@
2021
from ..utils.i18n import get_best_field_language
2122
from ..utils.indexers import slice_string_for_completion
2223

24+
logger = logging.getLogger(__name__)
25+
2326

2427
class PersonsIndexer:
2528
"""
@@ -78,6 +81,11 @@ def get_es_document_for_person(cls, person, index=None, action="index"):
7881
):
7982
language = portrait.cmsplugin_ptr.language
8083
with translation.override(language):
84+
logger.debug(
85+
"Indexing %s portrait for language %s",
86+
person.extended_object.get_absolute_url(language),
87+
language,
88+
)
8189
portrait_images[language] = get_picture_info(portrait, "portrait")
8290

8391
# Get bio texts

src/richie/apps/search/management/commands/bootstrap_elasticsearch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ def handle(self, *args, **options):
2727

2828
# Creates new indices each time, populates them, and atomically replaces
2929
# the old indices once the new ones are ready.
30-
regenerate_indices(logger)
30+
regenerate_indices()
3131

3232
# Confirm operation success through a console log
3333
logger.info("ES indices regenerated.")
3434

3535
logger.info("Starting to store ES scripts...")
3636

3737
# Iterates over indexables to find all the necessary scripts and store them
38-
store_es_scripts(logger)
38+
store_es_scripts()
3939

4040
logger.info("ES scripts stored.")

tests/apps/search/test_index_manager.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ def get_es_documents(self, index, action="index"):
130130
# Make sure our index is empty before we call the function
131131
self.assertEqual(ES_INDICES_CLIENT.get_alias("*"), {})
132132

133-
mock_logger = mock.Mock(spec=["info"])
134-
135-
with mock.patch.object(django_timezone, "now", return_value=now):
136-
new_index = perform_create_index(indexable, mock_logger)
133+
with mock.patch.object(
134+
django_timezone, "now", return_value=now
135+
), self.assertLogs() as mock_logger:
136+
new_index = perform_create_index(indexable)
137137
ES_INDICES_CLIENT.refresh()
138138

139139
self.assertEqual(new_index, "richie_courses_2016-05-04-03h12m33.123456s")
@@ -151,7 +151,7 @@ def get_es_documents(self, index, action="index"):
151151
}
152152
},
153153
)
154-
mock_logger.info.assert_called()
154+
self.assertNotEqual(mock_logger.records, [])
155155

156156
@mock.patch.object(
157157
ES_INDICES.categories,
@@ -201,7 +201,7 @@ def test_index_manager_regenerate_indices(self, *args):
201201
creation1_datetime = datetime(2010, 1, 1, tzinfo=timezone.utc)
202202
creation1_string = creation1_datetime.strftime("%Y-%m-%d-%Hh%Mm%S.%fs")
203203
with mock.patch.object(django_timezone, "now", return_value=creation1_datetime):
204-
regenerate_indices(None)
204+
regenerate_indices()
205205

206206
expected_indices = [
207207
"richie_test_categories",
@@ -223,7 +223,7 @@ def test_index_manager_regenerate_indices(self, *args):
223223
creation2_datetime = datetime(2011, 2, 2, tzinfo=timezone.utc)
224224
creation2_string = creation2_datetime.strftime("%Y-%m-%d-%Hh%Mm%S.%fs")
225225
with mock.patch.object(django_timezone, "now", return_value=creation2_datetime):
226-
regenerate_indices(None)
226+
regenerate_indices()
227227

228228
# All indices were replaced and aliases updated
229229
for alias_name in expected_indices:
@@ -252,7 +252,7 @@ def test_index_manager_regenerate_indices(self, *args):
252252
creation3_datetime = datetime(2012, 3, 3, tzinfo=timezone.utc)
253253
creation3_string = creation3_datetime.strftime("%Y-%m-%d-%Hh%Mm%S.%fs")
254254
with mock.patch.object(django_timezone, "now", return_value=creation3_datetime):
255-
regenerate_indices(None)
255+
regenerate_indices()
256256

257257
# All indices were replaced and had their aliases changed
258258
for index_name in expected_indices:
@@ -314,7 +314,7 @@ def test_index_manager_regenerate_indices_from_broken_state(self, *args):
314314
creation_datetime = datetime(2010, 1, 1, tzinfo=timezone.utc)
315315
creation_string = creation_datetime.strftime("%Y-%m-%d-%Hh%Mm%S.%fs")
316316
with mock.patch.object(django_timezone, "now", return_value=creation_datetime):
317-
regenerate_indices(None)
317+
regenerate_indices()
318318

319319
# No error was thrown, the courses index (like all others) was bootstrapped
320320
self.assertIsNotNone(
@@ -345,7 +345,7 @@ def test_index_manager_store_es_scripts(self, mock_put_script, *args):
345345
Make sure store_es_scripts iterates over all indexers to store their scripts and
346346
does not choke when there are (0 | 1 | 2+) scripts on an indexer.
347347
"""
348-
store_es_scripts(None)
348+
store_es_scripts()
349349
mock_put_script.assert_any_call(body="script body A", id="script_id_A")
350350
mock_put_script.assert_any_call(body="script body B", id="script_id_B")
351351
mock_put_script.assert_any_call(body="script body C", id="script_id_C")

0 commit comments

Comments
 (0)