Skip to content

Commit

Permalink
feat: retrying user fetch if failed with 400
Browse files Browse the repository at this point in the history
  • Loading branch information
NithinKuruba committed Dec 10, 2024
1 parent 0fd93c2 commit 7e9199b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion docker/kc-cron-job/remove-inactive-idir-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down

0 comments on commit 7e9199b

Please sign in to comment.