Mirrored from https://github.com/Camo67/OCIU-npo.git
Synced Nov 21, 06:00 UTC
fix/ociu-remove-placeholder-content
| 1 | import psutil |
| 2 | import os |
| 3 | import time |
| 4 | from backend.services.obsidian import obsidian |
| 5 | from backend.services.redis_cache import redis_cache |
| 6 | |
| 7 | RAM_CRITICAL = 0.85 # 85% |
| 8 | QUEUE_MAX = 20 |
| 9 | CHECK_INTERVAL = 10 # Seconds |
| 10 | |
| 11 | async def hardened_self_heal(): |
| 12 | while True: |
| 13 | ram_usage = psutil.virtual_memory().percent / 100 |
| 14 | queue_size = int(await redis_cache.get("system:queue_size") or "0") |
| 15 | |
| 16 | if ram_usage > RAM_CRITICAL: |
| 17 | # Kill lowest priority jobs |
| 18 | print(f"CRITICAL RAM: {ram_usage*100}%. Throttling heavy agents.") |
| 19 | await redis_cache.set("system:throttle", "true", ttl=300) |
| 20 | obsidian.write_note(f"SelfHeal_RAM_{int(time.time())}", f"RAM at {ram_usage*100}%. Heavy agents throttled.", folder="logs/system") |
| 21 | |
| 22 | if queue_size > QUEUE_MAX: |
| 23 | print(f"QUEUE OVERFLOW: {queue_size} jobs. Rejecting new tasks.") |
| 24 | await redis_cache.set("system:reject_new", "true", ttl=60) |
| 25 | obsidian.write_note(f"SelfHeal_Queue_{int(time.time())}", f"Queue overflow ({queue_size}). New jobs rejected.", folder="logs/system") |
| 26 | |
| 27 | await asyncio.sleep(CHECK_INTERVAL) |
| 28 | |
| 29 | def start_self_heal(): |
| 30 | import asyncio |
| 31 | asyncio.run(hardened_self_heal()) |