From 7e9199b4e378ce6421a28b038d0a8fb722d4a57c Mon Sep 17 00:00:00 2001 From: Nithin Shekar Kuruba Date: Mon, 9 Dec 2024 16:04:21 -0800 Subject: [PATCH] feat: retrying user fetch if failed with 400 --- docker/kc-cron-job/remove-inactive-idir-users.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docker/kc-cron-job/remove-inactive-idir-users.js b/docker/kc-cron-job/remove-inactive-idir-users.js index b3fef229..3b48a7c4 100644 --- a/docker/kc-cron-job/remove-inactive-idir-users.js +++ b/docker/kc-cron-job/remove-inactive-idir-users.js @@ -144,6 +144,19 @@ async function removeUserFromCssApp(userData, clientData, env) { } } +async function userFetchWithRetry(env, username, first, max) { + const retries = 3; + const adminClient = await getAdminClient(env); + for (let i = 1; i <= retries; i++) { + try { + return await adminClient.users.find({ realm: 'standard', username, first, max }); + } catch (err) { + console.error(`Error fetching users from Keycloak for ${env} environment with retries ${i} of ${retries}`); + if (i === retries) throw err; + } + } +} + async function removeStaleUsersByEnv(env = 'dev', pgClient, runnerName, startFrom, callback) { try { let deletedUserCount = 0; @@ -158,7 +171,7 @@ async function removeStaleUsersByEnv(env = 'dev', pgClient, runnerName, startFro let total = 0; while (true) { - const users = await adminClient.users.find({ realm: 'standard', username: '@idir', first, max }); + const users = await userFetchWithRetry(env, '@idir', first, max); const count = users.length; total += count;