Merge pull request #4106 from overleaf/ta-sync-user-entitlement-lean

Reduce Memory Footprint of Sync User Entitlements Script

GitOrigin-RevId: 673ea76f8af9e5be6ff99b8e340414a434c203f4
This commit is contained in:
Jakob Ackermann 2021-05-31 10:25:48 +02:00 committed by Copybot
parent b4d80de408
commit 7338560899

View file

@ -23,7 +23,11 @@ syncUserEntitlements(userEntitlements, cachedEntitlements)
async function syncUserEntitlements(userEntitlements, cachedEntitlements) { async function syncUserEntitlements(userEntitlements, cachedEntitlements) {
// check for user entitlements in mongo but not in postgres // 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 // find any email(s) that are linked through sso
for (const email of userEntitlement.emails) { for (const email of userEntitlement.emails) {
if (!email.samlProviderId) { if (!email.samlProviderId) {
@ -83,7 +87,11 @@ async function syncUserEntitlements(userEntitlements, cachedEntitlements) {
} }
// check for user entitlements in postgres but not in mongo // 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) { if (!cachedEntitlment.hasEntitlement) {
continue continue
} }
@ -134,13 +142,15 @@ async function syncUserEntitlement(userId, email, hasEntitlement) {
} }
function loadUserEntitlements(userEntitlementsFilename) { function loadUserEntitlements(userEntitlementsFilename) {
const userEntitlementsFile = fs.readFileSync(userEntitlementsFilename, { const userEntitlementsData = fs
encoding: 'utf8', .readFileSync(userEntitlementsFilename, {
}) encoding: 'utf8',
})
.split('\n')
const userEntitlements = {} const userEntitlements = {}
for (const userEntitlementLine of userEntitlementsFile.split('\n')) { for (const userEntitlementLine of userEntitlementsData) {
if (!userEntitlementLine) { if (!userEntitlementLine) {
continue continue
} }
@ -155,13 +165,15 @@ function loadUserEntitlements(userEntitlementsFilename) {
} }
function loadCachedEntitlements(cachedEntitlementsFilename) { function loadCachedEntitlements(cachedEntitlementsFilename) {
const cachedEntitlementsFile = fs.readFileSync(cachedEntitlementsFilename, { const cachedEntitlementsData = fs
encoding: 'utf8', .readFileSync(cachedEntitlementsFilename, {
}) encoding: 'utf8',
})
.split('\n')
const cachedEntitlements = {} 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 // this is safe because comma is not an allowed value for any column
const [ const [
userId, userId,