@@ -37,7 +37,8 @@ def log(nc, level, content):
3737}
3838app_enabled = Event ()
3939trigger = Event ()
40- CHECK_INTERVAL = 5 * 60
40+ CHECK_INTERVAL = 5
41+ CHECK_INTERVAL_WITH_TRIGGER = 5 * 60
4142CHECK_INTERVAL_ON_ERROR = 10
4243
4344@asynccontextmanager
@@ -88,15 +89,14 @@ def background_thread_task():
8889 (model , task ) = task_processor_name .split (":" , 1 )
8990 task_type_ids .add (task )
9091
91- trigger .clear ()
9292 try :
9393 response = nc .providers .task_processing .next_task (list (provider_ids ), list (task_type_ids ))
9494 if not response :
95- trigger . wait ( timeout = CHECK_INTERVAL )
95+ wait_for_tasks ( )
9696 continue
9797 except (NextcloudException , RequestException , JSONDecodeError ) as e :
9898 log (nc , LogLvl .ERROR , f"Network error fetching the next task { e } " )
99- trigger . wait ( timeout = CHECK_INTERVAL_ON_ERROR )
99+ wait_for_tasks ( CHECK_INTERVAL_ON_ERROR )
100100 continue
101101
102102 task = response ["task" ]
@@ -134,8 +134,6 @@ def background_thread_task():
134134 nc .providers .task_processing .report_result (task ["id" ], error_message = str (e ))
135135 except (NextcloudException , RequestException ) as net_err :
136136 log (nc , LogLvl .INFO , f"Network error in reporting the error: { net_err } " )
137- # if trigger has been set sinec the start of this iteration this will pass right through
138- trigger .wait (timeout = CHECK_INTERVAL )
139137
140138
141139def start_bg_task ():
@@ -191,5 +189,14 @@ def trigger_handler(providerId: str):
191189 print ('TRIGGER called' )
192190 trigger .set ()
193191
192+ def wait_for_tasks (interval = None ):
193+ global CHECK_INTERVAL
194+ global CHECK_INTERVAL_WITH_TRIGGER
195+ actual_interval = CHECK_INTERVAL if interval is None else interval
196+ if trigger .wait (timeout = actual_interval ):
197+ CHECK_INTERVAL = CHECK_INTERVAL_WITH_TRIGGER
198+ trigger .clear ()
199+
200+
194201if __name__ == "__main__" :
195202 run_app ("main:APP" , log_level = "trace" )
0 commit comments