Spaces:
Running
Running
| import os | |
| import sqlite3 | |
| from pathlib import Path | |
| # Di Hugging Face, direktori kerja standar adalah /app | |
| BASE_DIR = Path("/app") | |
| DB_PATH = BASE_DIR / "master_hermes.db" | |
| def init_db(): | |
| print("[INIT] Memulai konfigurasi database pusat di Hugging Face...", flush=True) | |
| conn = sqlite3.connect(DB_PATH) | |
| cursor = conn.cursor() | |
| # Tabel memantau 3 Profil Worker / Cron | |
| cursor.execute(""" | |
| CREATE TABLE IF NOT EXISTS workers ( | |
| id TEXT PRIMARY KEY, | |
| name TEXT, | |
| folder_path TEXT, | |
| status TEXT, | |
| last_run INTEGER, | |
| next_run INTEGER | |
| ) | |
| """) | |
| # Tabel mencatat Log Aktivitas & Auto-Healing | |
| cursor.execute(""" | |
| CREATE TABLE IF NOT EXISTS logs ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| worker_id TEXT, | |
| timestamp INTEGER, | |
| level TEXT, | |
| message TEXT | |
| ) | |
| """) | |
| # Data awal untuk 3 profil worker | |
| profils = [ | |
| ('profil_a', 'Worker Cron A (Akun 1-10)', str(BASE_DIR / 'worker_profil_a')), | |
| ('profil_b', 'Worker Cron B (Akun 11-20)', str(BASE_DIR / 'worker_profil_b')), | |
| ('profil_c', 'Worker Cron C (Akun 21-30)', str(BASE_DIR / 'worker_profil_c')) | |
| ] | |
| for w_id, w_name, w_path in profils: | |
| cursor.execute(""" | |
| INSERT OR IGNORE INTO workers (id, name, folder_path, status, last_run, next_run) | |
| VALUES (?, ?, ?, 'READY', 0, 0) | |
| """, (w_id, w_name, w_path)) | |
| conn.commit() | |
| conn.close() | |
| print("β Database Master SQLite berhasil dibuat di server!") | |
| def create_worker_folders(): | |
| print("[INIT] Memulai pembuatan folder Worker terisolasi...", flush=True) | |
| profils = ['profil_a', 'profil_b', 'profil_c'] | |
| for p in profils: | |
| p_path = BASE_DIR / f"worker_{p}" | |
| p_path.mkdir(parents=True, exist_ok=True) | |
| (p_path / "data").mkdir(parents=True, exist_ok=True) | |
| (p_path / "logs").mkdir(parents=True, exist_ok=True) | |
| print(f"π Folder {p_path.name}/data dan /logs siap.") | |
| if __name__ == "__main__": | |
| print("=== START INITIALIZATION ON HUGGING FACE ===") | |
| create_worker_folders() | |
| init_db() | |
| print("=== INITIALIZATION SUCCESS ===") | |