# Copyright (c) 2025 Patrick Motsch # All rights reserved. """ Automation Lifecycle Manager. Handles startup and shutdown of scheduled automations. Note: This module is NOT for feature container lifecycle - it only manages the automation scheduler (loading/syncing scheduled automation events). """ import logging from modules.services import getInterface as getServices logger = logging.getLogger(__name__) def start(eventUser) -> bool: """ Start automation scheduler and sync scheduled events. All operations are synchronous (DB access, scheduler registration). Args: eventUser: System-level event user for background operations (provided by app.py) """ if not eventUser: logger.warning("Automation: No event user provided, skipping automation sync") return True try: from modules.workflows.automation import syncAutomationEvents from modules.shared.callbackRegistry import callbackRegistry # Get services for event user (provides access to interfaces) services = getServices(eventUser, None) # Register callback for automation changes def onAutomationChanged(chatInterface): """Callback triggered when automations are created/updated/deleted.""" eventServices = getServices(eventUser, None) syncAutomationEvents(eventServices, eventUser) callbackRegistry.register('automation.changed', onAutomationChanged) logger.info("Automation: Registered change callback") # Initial sync on startup syncAutomationEvents(services, eventUser) logger.info("Automation: Scheduled events synced on startup") except Exception as e: logger.error(f"Automation: Error setting up events on startup: {str(e)}") return False return True def stop(eventUser) -> bool: """ Stop automation scheduler. Args: eventUser: System-level event user (provided by app.py) """ # Callbacks will remain registered (acceptable for shutdown) logger.info("Automation: Scheduler stopped (callbacks cleaned up on shutdown)") return True