@@ -49,6 +49,10 @@ def __init__(self, db):
4949 # Format: {type: {'handler': fn(config)->dict, 'guard': fn()->bool or None}}
5050 self ._action_handlers = {}
5151
52+ # Progress tracking callbacks (registered by web_server.py)
53+ self ._progress_init_fn = None
54+ self ._progress_finish_fn = None
55+
5256 # Event trigger cache: trigger_type → [automation_id, ...]
5357 self ._event_automations = {}
5458 self ._event_cache_dirty = True
@@ -73,6 +77,11 @@ def register_action_handler(self, action_type, handler_fn, guard_fn=None):
7377 }
7478 logger .debug (f"Registered action handler: { action_type } " )
7579
80+ def register_progress_callbacks (self , init_fn , finish_fn ):
81+ """Register callbacks for live progress tracking from web_server.py."""
82+ self ._progress_init_fn = init_fn
83+ self ._progress_finish_fn = finish_fn
84+
7685 # --- System Automations ---
7786
7887 def ensure_system_automations (self ):
@@ -278,6 +287,11 @@ def _run_event_automation(self, auto, automation_id, event_data):
278287 action_config = json .loads (auto .get ('action_config' ) or '{}' )
279288 except json .JSONDecodeError :
280289 action_config = {}
290+
291+ # Inject automation identity for progress tracking
292+ action_config ['_automation_id' ] = automation_id
293+ action_config ['_automation_name' ] = auto .get ('name' , '' )
294+
281295 delay_minutes = action_config .get ('delay' , 0 )
282296 if delay_minutes and delay_minutes > 0 :
283297 logger .info (f"Event automation '{ auto .get ('name' )} ' delaying { delay_minutes } m before action" )
@@ -299,12 +313,20 @@ def _run_event_automation(self, auto, automation_id, event_data):
299313 result = {'status' : 'skipped' , 'reason' : f'{ action_type } already running' }
300314 logger .info (f"Event automation '{ auto .get ('name' )} ' skipped — { action_type } busy" )
301315 else :
316+ # Initialize progress tracking
317+ if self ._progress_init_fn :
318+ try : self ._progress_init_fn (automation_id , auto .get ('name' , '' ), action_type )
319+ except Exception : pass
302320 try :
303321 result = handler_info ['handler' ](action_config ) or {}
304322 logger .info (f"Event automation '{ auto .get ('name' )} ' executed: { result .get ('status' , 'ok' )} " )
305323 except Exception as e :
306324 result = {'status' : 'error' , 'error' : str (e )}
307325 logger .error (f"Event automation '{ auto .get ('name' )} ' action failed: { e } " )
326+ # Finalize progress tracking
327+ if self ._progress_finish_fn :
328+ try : self ._progress_finish_fn (automation_id , result )
329+ except Exception : pass
308330
309331 # Merge event data into result for notification variables
310332 merged = {** event_data , ** result }
@@ -353,6 +375,10 @@ def run_automation(self, automation_id, skip_delay=False):
353375 except json .JSONDecodeError :
354376 action_config = {}
355377
378+ # Inject automation identity for progress tracking
379+ action_config ['_automation_id' ] = automation_id
380+ action_config ['_automation_name' ] = auto .get ('name' , '' )
381+
356382 # Action delay (skipped for manual run_now)
357383 delay_minutes = action_config .get ('delay' , 0 )
358384 if not skip_delay and delay_minutes and delay_minutes > 0 :
@@ -369,6 +395,11 @@ def run_automation(self, automation_id, skip_delay=False):
369395 self ._finish_run (auto , automation_id , result , error = None )
370396 return
371397
398+ # Initialize progress tracking
399+ if self ._progress_init_fn :
400+ try : self ._progress_init_fn (automation_id , auto .get ('name' , '' ), action_type )
401+ except Exception : pass
402+
372403 # Execute the action
373404 error = None
374405 result = {}
@@ -380,6 +411,11 @@ def run_automation(self, automation_id, skip_delay=False):
380411 result = {'status' : 'error' , 'error' : error }
381412 logger .error (f"Automation '{ auto ['name' ]} ' (id={ automation_id } ) failed: { e } " )
382413
414+ # Finalize progress tracking
415+ if self ._progress_finish_fn :
416+ try : self ._progress_finish_fn (automation_id , result )
417+ except Exception : pass
418+
383419 # Send notification if configured
384420 try :
385421 self ._send_notification (auto , result )
0 commit comments