mailu: retry sync and rerun job
This commit is contained in:
parent
9eedcad520
commit
9dd2a72063
@ -2,7 +2,7 @@
|
|||||||
apiVersion: batch/v1
|
apiVersion: batch/v1
|
||||||
kind: Job
|
kind: Job
|
||||||
metadata:
|
metadata:
|
||||||
name: mailu-sync-6
|
name: mailu-sync-7
|
||||||
namespace: mailu-mailserver
|
namespace: mailu-mailserver
|
||||||
spec:
|
spec:
|
||||||
template:
|
template:
|
||||||
|
|||||||
@ -42,6 +42,28 @@ def log(msg):
|
|||||||
sys.stdout.flush()
|
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():
|
def get_kc_token():
|
||||||
resp = SESSION.post(
|
resp = SESSION.post(
|
||||||
f"{KC_BASE}/realms/{KC_REALM}/protocol/openid-connect/token",
|
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():
|
def main():
|
||||||
token = get_kc_token()
|
token = retry_request("Keycloak token", get_kc_token)
|
||||||
users = kc_get_users(token)
|
users = retry_request("Keycloak user list", lambda: kc_get_users(token))
|
||||||
if not users:
|
if not users:
|
||||||
log("No users found; exiting.")
|
log("No users found; exiting.")
|
||||||
return
|
return
|
||||||
|
|
||||||
conn = psycopg2.connect(**DB_CONFIG)
|
conn = retry_db_connect()
|
||||||
conn.autocommit = True
|
conn.autocommit = True
|
||||||
cursor = conn.cursor(cursor_factory=RealDictCursor)
|
cursor = conn.cursor(cursor_factory=RealDictCursor)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user