@@ -36,14 +36,15 @@ def log(nc, level, content):
3636 "https://huggingface.co/bartowski/Meta-Llama-3.1-8B-Instruct-GGUF/resolve/4f0c246f125fc7594238ebe7beb1435a8335f519/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf" : { "save_path" : os .path .join (persistent_storage (), "Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf" ) },
3737}
3838app_enabled = Event ()
39-
39+ trigger = Event ()
4040
4141@asynccontextmanager
4242async def lifespan (_app : FastAPI ):
4343 set_handlers (
4444 APP ,
4545 enabled_handler , # type: ignore
4646 models_to_fetch = models_to_fetch ,
47+ trigger_handler = trigger_handler
4748 )
4849 nc = NextcloudApp ()
4950 if nc .enabled_state :
@@ -85,14 +86,15 @@ def background_thread_task():
8586 (model , task ) = task_processor_name .split (":" , 1 )
8687 task_type_ids .add (task )
8788
89+ trigger .clear ()
8890 try :
8991 response = nc .providers .task_processing .next_task (list (provider_ids ), list (task_type_ids ))
9092 if not response :
91- sleep ( 5 )
93+ trigger . wait ( timeout = 30 )
9294 continue
9395 except (NextcloudException , RequestException , JSONDecodeError ) as e :
9496 log (nc , LogLvl .ERROR , f"Network error fetching the next task { e } " )
95- sleep ( 5 )
97+ trigger . wait ( timeout = 5 )
9698 continue
9799
98100 task = response ["task" ]
@@ -122,7 +124,6 @@ def background_thread_task():
122124 # Error when reporting the result
123125 exception_info = traceback .format_exception (type (e ), e , e .__traceback__ )
124126 log (nc , LogLvl .ERROR , f"Error: { '' .join (exception_info )} " )
125- sleep (5 )
126127 except Exception as e : # noqa
127128 exception_info = traceback .format_exception (type (e ), e , e .__traceback__ )
128129 log (nc , LogLvl .ERROR , f"Error: { '' .join (exception_info )} " )
@@ -131,8 +132,8 @@ def background_thread_task():
131132 nc .providers .task_processing .report_result (task ["id" ], error_message = str (e ))
132133 except (NextcloudException , RequestException ) as net_err :
133134 log (nc , LogLvl .INFO , f"Network error in reporting the error: { net_err } " )
134-
135- sleep ( 5 )
135+ # if trigger has been set sinec the start of this iteration this will pass right through
136+ trigger . wait ( timeout = 30 )
136137
137138
138139def start_bg_task ():
@@ -184,5 +185,9 @@ async def enabled_handler(enabled: bool, nc: AsyncNextcloudApp) -> str:
184185 return ""
185186
186187
188+ def trigger_handler (providerId : str ):
189+ print ('TRIGGER called' )
190+ trigger .set ()
191+
187192if __name__ == "__main__" :
188193 run_app ("main:APP" , log_level = "trace" )
0 commit comments