mailu: retry sync and rerun job
This commit is contained in:
parent
9eedcad520
commit
9dd2a72063
@ -2,7 +2,7 @@
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: mailu-sync-6
|
||||
name: mailu-sync-7
|
||||
namespace: mailu-mailserver
|
||||
spec:
|
||||
template:
|
||||
|
||||
@ -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)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user