From 7338560899d671507c793de512551d18b286f0cc Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Mon, 31 May 2021 10:25:48 +0200 Subject: [PATCH] Merge pull request #4106 from overleaf/ta-sync-user-entitlement-lean Reduce Memory Footprint of Sync User Entitlements Script GitOrigin-RevId: 673ea76f8af9e5be6ff99b8e340414a434c203f4 --- .../sync-user-entitlements.js | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/services/web/scripts/sync-user-entitlements/sync-user-entitlements.js b/services/web/scripts/sync-user-entitlements/sync-user-entitlements.js index f1acaff9e6..95f925f7ac 100644 --- a/services/web/scripts/sync-user-entitlements/sync-user-entitlements.js +++ b/services/web/scripts/sync-user-entitlements/sync-user-entitlements.js @@ -23,7 +23,11 @@ syncUserEntitlements(userEntitlements, cachedEntitlements) async function syncUserEntitlements(userEntitlements, cachedEntitlements) { // check for user entitlements in mongo but not in postgres - for (const userEntitlement of Object.values(userEntitlements)) { + for (const key of Object.keys(userEntitlements)) { + const userEntitlement = userEntitlements[key] + if (!userEntitlement) { + continue + } // find any email(s) that are linked through sso for (const email of userEntitlement.emails) { if (!email.samlProviderId) { @@ -83,7 +87,11 @@ async function syncUserEntitlements(userEntitlements, cachedEntitlements) { } // check for user entitlements in postgres but not in mongo - for (const cachedEntitlment of Object.values(cachedEntitlements)) { + for (const key of Object.keys(cachedEntitlements)) { + const cachedEntitlment = cachedEntitlements[key] + if (!cachedEntitlment) { + continue + } if (!cachedEntitlment.hasEntitlement) { continue } @@ -134,13 +142,15 @@ async function syncUserEntitlement(userId, email, hasEntitlement) { } function loadUserEntitlements(userEntitlementsFilename) { - const userEntitlementsFile = fs.readFileSync(userEntitlementsFilename, { - encoding: 'utf8', - }) + const userEntitlementsData = fs + .readFileSync(userEntitlementsFilename, { + encoding: 'utf8', + }) + .split('\n') const userEntitlements = {} - for (const userEntitlementLine of userEntitlementsFile.split('\n')) { + for (const userEntitlementLine of userEntitlementsData) { if (!userEntitlementLine) { continue } @@ -155,13 +165,15 @@ function loadUserEntitlements(userEntitlementsFilename) { } function loadCachedEntitlements(cachedEntitlementsFilename) { - const cachedEntitlementsFile = fs.readFileSync(cachedEntitlementsFilename, { - encoding: 'utf8', - }) + const cachedEntitlementsData = fs + .readFileSync(cachedEntitlementsFilename, { + encoding: 'utf8', + }) + .split('\n') const cachedEntitlements = {} - for (const cachedEntitlementLine of cachedEntitlementsFile.split('\n')) { + for (const cachedEntitlementLine of cachedEntitlementsData) { // this is safe because comma is not an allowed value for any column const [ userId,