import asyncio from datetime import datetime from modules.agents import MODULES from database import log_activity # ============================================================ # JARVIS SCHEDULER — Auto cronjob tanpa sentuh # ============================================================ class JarvisScheduler: def __init__(self): self.running = False self.jobs = [ {"name": "TrendHijacker", "module": "trend", "interval": 6 * 3600}, {"name": "Writer-Hostinger","module": "writer", "interval": 8 * 3600, "kwargs": {"topic": "hosting murah terbaik Indonesia", "platform": "hostinger"}}, {"name": "Writer-Shopee", "module": "writer", "interval": 10 * 3600, "kwargs": {"topic": "produk viral shopee murah", "platform": "shopee"}}, {"name": "GhostNetwork", "module": "ghost", "interval": 12 * 3600, "kwargs": {"content": "Mau website murah dan profesional? Coba Hostinger! Link: https://hostinger.com?REFERRALCODE=FCIHARY08HBD"}}, {"name": "Scout", "module": "scout", "interval": 24 * 3600, "kwargs": {"target": "hosting murah Indonesia 2025"}}, {"name": "Analyst", "module": "analyst", "interval": 24 * 3600}, ] # Track last run time self.last_run = {job["name"]: 0 for job in self.jobs} async def run_job(self, job: dict): """Jalankan satu job""" module_name = job["module"] kwargs = job.get("kwargs", {}) try: print(f"🤖 Scheduler running: {job['name']} at {datetime.now().strftime('%H:%M:%S')}") result = await MODULES[module_name].run(**kwargs) await log_activity("SCHEDULER", f"{job['name']} completed via {result.get('provider','?')}") print(f"✅ Done: {job['name']}") return result except Exception as e: print(f"❌ Error {job['name']}: {e}") await log_activity("SCHEDULER_ERROR", f"{job['name']}: {str(e)}") return None async def start(self): """Start scheduler loop""" self.running = True print("⏰ Jarvis Scheduler started!") await log_activity("SCHEDULER", "Started") while self.running: now = asyncio.get_event_loop().time() for job in self.jobs: last = self.last_run[job["name"]] if now - last >= job["interval"]: self.last_run[job["name"]] = now asyncio.create_task(self.run_job(job)) await asyncio.sleep(60) # cek setiap 1 menit def stop(self): self.running = False print("⏰ Scheduler stopped.") scheduler = JarvisScheduler()