1010from contextlib import suppress
1111from enum import Enum
1212from threading import Event , Thread
13- from time import sleep , time
13+ from time import sleep
1414from typing import Any
1515
1616import niquests
@@ -69,7 +69,7 @@ class ThreadType(Enum):
6969 REQUEST_PROCESSING = 'request_processing'
7070
7171
72- def files_indexing_thread (app_config : TConfig ) -> None :
72+ def files_indexing_thread (app_config : TConfig , get_enabled_state ) -> None :
7373 try :
7474 network_em = NetworkEmbeddings (app_config )
7575 vectordb_loader = VectorDBLoader (app_config )
@@ -138,18 +138,12 @@ def _load_sources(source_items: Mapping[int, SourceItem | ReceivedFileItem]) ->
138138 LOGGER .info (f'Using { file_parsing_cpu_count } parallel file parsing workers' )
139139
140140 nc = NextcloudApp ()
141- last_enabled_check = time ()
142- enabled_state = nc .enabled_state
143141 while True :
144142 if THREAD_STOP_EVENT .is_set ():
145143 LOGGER .info ('Files indexing thread is stopping due to stop event being set' )
146144 return
147145
148- if time () - last_enabled_check > 30 : # check enabled state every 30 seconds
149- enabled_state = nc .enabled_state
150- last_enabled_check = time ()
151-
152- if not enabled_state :
146+ if not get_enabled_state ():
153147 LOGGER .info ('App is disabled, files indexing thread will sleep until next enabled state check' )
154148 sleep (POLLING_COOLDOWN )
155149 continue
@@ -284,26 +278,20 @@ def _load_sources(source_items: Mapping[int, SourceItem | ReceivedFileItem]) ->
284278
285279
286280
287- def updates_processing_thread (app_config : TConfig ) -> None :
281+ def updates_processing_thread (app_config : TConfig , get_enabled_state ) -> None :
288282 try :
289283 vectordb_loader = VectorDBLoader (app_config )
290284 except LoaderException as e :
291285 LOGGER .error ('Error initializing vector DB loader, files indexing thread will not start:' , exc_info = e )
292286 return
293287
294288 nc = NextcloudApp ()
295- enabled_state = nc .enabled_state
296- last_enabled_check = time ()
297289 while True :
298290 if THREAD_STOP_EVENT .is_set ():
299291 LOGGER .info ('Updates processing thread is stopping due to stop event being set' )
300292 return
301293
302- if time () - last_enabled_check > 30 : # check enabled state every 30 seconds
303- enabled_state = nc .enabled_state
304- last_enabled_check = time ()
305-
306- if not enabled_state :
294+ if not get_enabled_state ():
307295 sleep (POLLING_COOLDOWN )
308296 continue
309297
@@ -475,7 +463,7 @@ def resolve_scope_list(source_ids: list[str], userId: str) -> list[str]:
475463 return ScopeList .model_validate (data ).source_ids
476464
477465
478- def request_processing_thread (app_config : TConfig ) -> None :
466+ def request_processing_thread (app_config : TConfig , get_enabled_state ) -> None :
479467 LOGGER .info ('Starting request processing thread' )
480468
481469 try :
@@ -487,8 +475,6 @@ def request_processing_thread(app_config: TConfig) -> None:
487475 return
488476
489477 nc = NextcloudApp ()
490- enabled_state = nc .enabled_state
491- last_enabled_check = time ()
492478 llm : LLM = llm_loader .load ()
493479
494480 while True :
@@ -500,11 +486,7 @@ def request_processing_thread(app_config: TConfig) -> None:
500486 sleep (POLLING_COOLDOWN )
501487 continue
502488
503- if time () - last_enabled_check > 30 : # check enabled state every 30 seconds
504- enabled_state = nc .enabled_state
505- last_enabled_check = time ()
506-
507- if not enabled_state :
489+ if not get_enabled_state ():
508490 sleep (POLLING_COOLDOWN )
509491 continue
510492
@@ -726,7 +708,7 @@ def process_search_task(
726708 )
727709
728710
729- def start_bg_threads (app_config : TConfig ):
711+ def start_bg_threads (app_config : TConfig , get_enabled_state ):
730712 if APP_ROLE == AppRole .INDEXING or APP_ROLE == AppRole .NORMAL :
731713 if (
732714 ThreadType .FILES_INDEXING in THREADS
@@ -738,12 +720,12 @@ def start_bg_threads(app_config: TConfig):
738720 THREAD_STOP_EVENT .clear ()
739721 THREADS [ThreadType .FILES_INDEXING ] = Thread (
740722 target = files_indexing_thread ,
741- args = (app_config ,),
723+ args = (app_config ,get_enabled_state ),
742724 name = 'FilesIndexingThread' ,
743725 )
744726 THREADS [ThreadType .UPDATES_PROCESSING ] = Thread (
745727 target = updates_processing_thread ,
746- args = (app_config ,),
728+ args = (app_config ,get_enabled_state ),
747729 name = 'UpdatesProcessingThread' ,
748730 )
749731 THREADS [ThreadType .FILES_INDEXING ].start ()
@@ -757,7 +739,7 @@ def start_bg_threads(app_config: TConfig):
757739 THREAD_STOP_EVENT .clear ()
758740 THREADS [ThreadType .REQUEST_PROCESSING ] = Thread (
759741 target = request_processing_thread ,
760- args = (app_config ,),
742+ args = (app_config ,get_enabled_state ),
761743 name = 'RequestProcessingThread' ,
762744 )
763745 THREADS [ThreadType .REQUEST_PROCESSING ].start ()
0 commit comments