#!/usr/bin/env python3 """Auto-pull service: monitors origin/main and pulls new commits automatically.""" import os import subprocess import sys import time INTERVAL_SECONDS = int(os.environ.get("AUTO_PULL_INTERVAL", 30)) REPO_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) def run_git(args): res = subprocess.run(["git"] + args, cwd=REPO_DIR, capture_output=True, text=True) return res.returncode == 0, res.stdout.strip(), res.stderr.strip() def check_and_pull(): # Fetch latest remote changes quietly ok, _, err = run_git(["fetch", "origin", "main"]) if not ok: print(f"[Auto-Pull] Git fetch error: {err}") return _, incoming_commits, _ = run_git(["log", "HEAD..origin/main", "--oneline"]) if incoming_commits: print(f"[Auto-Pull] Incoming commits detected:\n{incoming_commits}") ok, out, err = run_git(["pull", "origin", "main"]) if ok: print(f"[Auto-Pull] Successfully pulled:\n{out}") print("[Auto-Pull] Restarting app services...") restart_script = os.path.join(REPO_DIR, "scripts", "restart_app.sh") subprocess.run(["bash", restart_script], cwd=REPO_DIR) else: print(f"[Auto-Pull] Pull failed:\n{err}") def main(): print(f"🔄 Auto-pull background poller started (Interval: {INTERVAL_SECONDS}s)") while True: try: check_and_pull() except Exception as exc: print(f"[Auto-Pull] Error: {exc}") time.sleep(INTERVAL_SECONDS) if __name__ == "__main__": main()