#!/usr/bin/env python3
"""Auto-generated by AGI Loop cycle #1061
Task: Write a Python function that checks all eden-* systemd services and restarts any that are in failed state
Generated: 2026-02-12T18:44:10.158593
"""

import subprocess

def restart_failed_eden_services():
    # List to hold service names
    service_names = []

    # Get all systemd services with name starting with 'eden-'
    try:
        result = subprocess.run(['systemctl', 'list-units', '--type=service', '--state=failed', '--no-legend'], capture_output=True, text=True)
        lines = result.stdout.strip().splitlines()
        for line in lines:
            if line.startswith('eden-'):
                service_name = line.split()[0]
                service_names.append(service_name)
    except Exception as e:
        print(f"Error fetching service list: {e}")
        return

    # If no failed eden services found
    if not service_names:
        print("No failed eden-* services found.")
        return

    # Restart each failed service
    for service in service_names:
        try:
            subprocess.run(['systemctl', 'restart', service], check=True)
            print(f"Restarted failed service: {service}")
        except subprocess.CalledProcessError as e:
            print(f"Failed to restart service {service}: {e}")

if __name__ == '__main__':
    restart_failed_eden_services()