1010logger = getLogger (__file__ )
1111
1212
13- def document_indexer_debounce_key (document_id ):
14- """Returns debounce cache key"""
15- return f"doc-indexer-debounce-{ document_id } "
16-
17-
18- def incr_counter (key ):
13+ def indexer_debounce_lock (document_id ):
1914 """Increase or reset counter"""
15+ key = f"doc-indexer-debounce-{ document_id } "
16+
2017 try :
2118 return cache .incr (key )
2219 except ValueError :
2320 cache .set (key , 1 )
2421 return 1
2522
2623
27- def decr_counter ( key ):
24+ def indexer_debounce_release ( document_id ):
2825 """Decrease or reset counter"""
26+ key = f"doc-indexer-debounce-{ document_id } "
27+
2928 try :
3029 return cache .decr (key )
3130 except ValueError :
@@ -36,24 +35,26 @@ def decr_counter(key):
3635@app .task
3736def document_indexer_task (document_id ):
3837 """Celery Task : Sends indexation query for a document."""
39- key = document_indexer_debounce_key (document_id )
38+ # Prevents some circular imports
39+ # pylint: disable=import-outside-toplevel
40+ from core import models # noqa : PLC0415
41+ from core .services .search_indexers import ( # noqa : PLC0415
42+ get_batch_accesses_by_users_and_teams ,
43+ get_document_indexer ,
44+ )
4045
4146 # check if the counter : if still up, skip the task. only the last one
4247 # within the countdown delay will do the query.
43- if decr_counter ( key ) > 0 :
48+ if indexer_debounce_release ( document_id ) > 0 :
4449 logger .info ("Skip document %s indexation" , document_id )
4550 return
4651
47- # Prevents some circular imports
48- # pylint: disable=import-outside-toplevel
49- from core import models # noqa: PLC0415
50- from core .services .search_indexers import ( # noqa: PLC0415
51- get_batch_accesses_by_users_and_teams ,
52- get_document_indexer_class ,
53- )
52+ indexer = get_document_indexer ()
53+
54+ if indexer is None :
55+ return
5456
5557 doc = models .Document .objects .get (pk = document_id )
56- indexer = get_document_indexer_class ()()
5758 accesses = get_batch_accesses_by_users_and_teams ((doc .path ,))
5859
5960 data = indexer .serialize_document (document = doc , accesses = accesses )
@@ -69,11 +70,11 @@ def trigger_document_indexer(document):
6970 Args:
7071 document (Document): The document instance.
7172 """
72- if document .deleted_at or document .ancestors_deleted_at :
73- return
73+ countdown = settings .SEARCH_INDEXER_COUNTDOWN
7474
75- key = document_indexer_debounce_key (document .pk )
76- countdown = getattr (settings , "SEARCH_INDEXER_COUNTDOWN" , 1 )
75+ # DO NOT create a task if indexation if disabled
76+ if not settings .SEARCH_INDEXER_CLASS :
77+ return
7778
7879 logger .info (
7980 "Add task for document %s indexation in %.2f seconds" ,
@@ -83,6 +84,6 @@ def trigger_document_indexer(document):
8384
8485 # Each time this method is called during the countdown, we increment the
8586 # counter and each task decrease it, so the index be run only once.
86- incr_counter ( key )
87+ indexer_debounce_lock ( document . pk )
8788
8889 document_indexer_task .apply_async (args = [document .pk ], countdown = countdown )
0 commit comments