better logging

This commit is contained in:
Holden
2026-06-10 18:31:43 -04:00
parent b8445a1bb3
commit a2f18484a6
4 changed files with 203 additions and 37 deletions
+47 -8
View File
@@ -8,6 +8,7 @@ import os
import sys
import subprocess
import time
from pathlib import Path
try:
from croniter import croniter
@@ -16,28 +17,66 @@ except ImportError:
sys.exit(1)
SCHEDULE = os.environ["CRON_SCHEDULE"]
NOW = time.time()
MODELS_DIR = os.environ.get("HF_HOME", "/models/huggingface")
INSIGHTFACE_DIR = os.environ.get("INSIGHTFACE_HOME", "/models/insightface")
# Ensure all subprocess output appears in docker logs
RUN_ENV = {**os.environ, "PYTHONUNBUFFERED": "1"}
def check_models():
"""Log model download status before each run."""
print("📦 Checking models...", flush=True)
buffalo = Path(INSIGHTFACE_DIR) / "models" / "buffalo_l"
if buffalo.exists():
print(" ✅ InsightFace Buffalo_L: downloaded", flush=True)
else:
print(" ⬇️ InsightFace Buffalo_L: downloading (~300MB)...", flush=True)
hf_hub = Path(MODELS_DIR) / "hub"
if hf_hub.exists() and any(hf_hub.iterdir()):
print(" ✅ HuggingFace models: downloaded", flush=True)
else:
print(" ⬇️ HuggingFace models: downloading (SigLIP ~1GB, YOLOv9c ~500MB)...", flush=True)
print("🚀 Starting if-curator...", flush=True)
def calc_time_display(seconds):
"""Convert seconds to human-readable time."""
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
if hours > 0:
return f"{hours}h {minutes}m"
return f"{minutes}m"
NOW = time.time()
cron = croniter(SCHEDULE, NOW)
next_run = cron.get_next(float)
while True:
now = time.time()
if now >= next_run:
print(f"\n▶ [{time.strftime('%Y-%m-%d %H:%M:%S')}] Starting if-curator...")
result = subprocess.run(["uv", "run", "if-curator"])
print(f"\n▶ [{time.strftime('%Y-%m-%d %H:%M:%S')}] Starting if-curator...", flush=True)
check_models()
result = subprocess.run(
["uv", "run", "if-curator"],
env=RUN_ENV,
)
if result.returncode == 0:
print(f"✅ Run complete.")
print("✅ Run complete.", flush=True)
else:
print(f"⚠️ Run exited with code {result.returncode}")
print(f"⚠️ Run exited with code {result.returncode}", flush=True)
next_run = cron.get_next(float)
wait = next_run - time.time()
if wait > 0:
print(f"▶ Next run: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(next_run))} (in {int(wait // 3600)}h {int((wait % 3600) // 60)}m)")
print(f"▶ Next run: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(next_run))} (in {calc_time_display(wait)})", flush=True)
continue
wait = next_run - now
print(f"▶ Next run: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(next_run))} (in {int(wait // 3600)}h {int((wait % 3600) // 60)}m)")
time.sleep(min(wait, 3600)) # Check every hour at most, or sooner if needed
print(f"▶ Next run: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(next_run))} (in {calc_time_display(wait)})", flush=True)
time.sleep(min(wait, 3600))