From 9dd2a720636461b267b03ed4a8034b283ba8fc21 Mon Sep 17 00:00:00 2001 From: Brad Stein Date: Sat, 17 Jan 2026 02:16:13 -0300 Subject: [PATCH] mailu: retry sync and rerun job --- services/mailu/mailu-sync-job.yaml | 2 +- services/mailu/scripts/mailu_sync.py | 28 +++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/services/mailu/mailu-sync-job.yaml b/services/mailu/mailu-sync-job.yaml index 6da615e..18aef7c 100644 --- a/services/mailu/mailu-sync-job.yaml +++ b/services/mailu/mailu-sync-job.yaml @@ -2,7 +2,7 @@ apiVersion: batch/v1 kind: Job metadata: - name: mailu-sync-6 + name: mailu-sync-7 namespace: mailu-mailserver spec: template: diff --git a/services/mailu/scripts/mailu_sync.py b/services/mailu/scripts/mailu_sync.py index 74b170a..d1754cb 100644 --- a/services/mailu/scripts/mailu_sync.py +++ b/services/mailu/scripts/mailu_sync.py @@ -42,6 +42,28 @@ def log(msg): sys.stdout.flush() +def retry_request(label, func, attempts=10): + for attempt in range(1, attempts + 1): + try: + return func() + except requests.RequestException as exc: + if attempt == attempts: + raise + log(f"{label} failed (attempt {attempt}/{attempts}): {exc}") + time.sleep(attempt * 2) + + +def retry_db_connect(attempts=10): + for attempt in range(1, attempts + 1): + try: + return psycopg2.connect(**DB_CONFIG) + except psycopg2.Error as exc: + if attempt == attempts: + raise + log(f"Database connection failed (attempt {attempt}/{attempts}): {exc}") + time.sleep(attempt * 2) + + def get_kc_token(): resp = SESSION.post( f"{KC_BASE}/realms/{KC_REALM}/protocol/openid-connect/token", @@ -175,13 +197,13 @@ def ensure_mailu_user(cursor, email, password, display_name): def main(): - token = get_kc_token() - users = kc_get_users(token) + token = retry_request("Keycloak token", get_kc_token) + users = retry_request("Keycloak user list", lambda: kc_get_users(token)) if not users: log("No users found; exiting.") return - conn = psycopg2.connect(**DB_CONFIG) + conn = retry_db_connect() conn.autocommit = True cursor = conn.cursor(cursor_factory=RealDictCursor)