@@ -36,14 +36,17 @@ 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 ()
40+ CHECK_INTERVAL = 5 * 60
41+ CHECK_INTERVAL_ON_ERROR = 10
4042
4143@asynccontextmanager
4244async def lifespan (_app : FastAPI ):
4345 set_handlers (
4446 APP ,
4547 enabled_handler , # type: ignore
4648 models_to_fetch = models_to_fetch ,
49+ trigger_handler = trigger_handler
4750 )
4851 nc = NextcloudApp ()
4952 if nc .enabled_state :
@@ -85,14 +88,15 @@ def background_thread_task():
8588 (model , task ) = task_processor_name .split (":" , 1 )
8689 task_type_ids .add (task )
8790
91+ trigger .clear ()
8892 try :
8993 response = nc .providers .task_processing .next_task (list (provider_ids ), list (task_type_ids ))
9094 if not response :
91- sleep ( 5 )
95+ trigger . wait ( timeout = CHECK_INTERVAL )
9296 continue
9397 except (NextcloudException , RequestException , JSONDecodeError ) as e :
9498 log (nc , LogLvl .ERROR , f"Network error fetching the next task { e } " )
95- sleep ( 5 )
99+ trigger . wait ( timeout = CHECK_INTERVAL_ON_ERROR )
96100 continue
97101
98102 task = response ["task" ]
@@ -122,7 +126,6 @@ def background_thread_task():
122126 # Error when reporting the result
123127 exception_info = traceback .format_exception (type (e ), e , e .__traceback__ )
124128 log (nc , LogLvl .ERROR , f"Error: { '' .join (exception_info )} " )
125- sleep (5 )
126129 except Exception as e : # noqa
127130 exception_info = traceback .format_exception (type (e ), e , e .__traceback__ )
128131 log (nc , LogLvl .ERROR , f"Error: { '' .join (exception_info )} " )
@@ -131,8 +134,8 @@ def background_thread_task():
131134 nc .providers .task_processing .report_result (task ["id" ], error_message = str (e ))
132135 except (NextcloudException , RequestException ) as net_err :
133136 log (nc , LogLvl .INFO , f"Network error in reporting the error: { net_err } " )
134-
135- sleep ( 5 )
137+ # if trigger has been set sinec the start of this iteration this will pass right through
138+ trigger . wait ( timeout = CHECK_INTERVAL )
136139
137140
138141def start_bg_task ():
@@ -184,5 +187,9 @@ async def enabled_handler(enabled: bool, nc: AsyncNextcloudApp) -> str:
184187 return ""
185188
186189
190+ def trigger_handler (providerId : str ):
191+ print ('TRIGGER called' )
192+ trigger .set ()
193+
187194if __name__ == "__main__" :
188195 run_app ("main:APP" , log_level = "trace" )
0 commit comments