From 6be41f858ade66b1cb6aa40e311304d6660e85b0 Mon Sep 17 00:00:00 2001 From: Eric Mc Sween Date: Tue, 30 Jun 2020 08:03:09 -0400 Subject: [PATCH] Merge pull request #2964 from overleaf/em-expire-projects Limit batch size when expiring projects GitOrigin-RevId: 492c318e3f07c08b451d0894ff2754402677f75f --- .../src/Features/Project/ProjectDeleter.js | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/services/web/app/src/Features/Project/ProjectDeleter.js b/services/web/app/src/Features/Project/ProjectDeleter.js index bffa9c57e8..3af65cb34d 100644 --- a/services/web/app/src/Features/Project/ProjectDeleter.js +++ b/services/web/app/src/Features/Project/ProjectDeleter.js @@ -1,3 +1,4 @@ +const _ = require('lodash') const { db, ObjectId } = require('../../infrastructure/mongojs') const { callbackify } = require('util') const { Project } = require('../../models/Project') @@ -18,6 +19,7 @@ const moment = require('moment') const { promiseMapWithLimit } = require('../../util/promises') const EXPIRE_PROJECTS_AFTER_DAYS = 90 +const PROJECT_EXPIRATION_BATCH_SIZE = 10000 module.exports = { markAsDeletedByExternalSource: callbackify(markAsDeletedByExternalSource), @@ -79,14 +81,22 @@ async function deleteUsersProjects(userId) { } async function expireDeletedProjectsAfterDuration() { - const deletedProjects = await DeletedProject.find({ - 'deleterData.deletedAt': { - $lt: new Date(moment().subtract(EXPIRE_PROJECTS_AFTER_DAYS, 'days')) + const deletedProjects = await DeletedProject.find( + { + 'deleterData.deletedAt': { + $lt: new Date(moment().subtract(EXPIRE_PROJECTS_AFTER_DAYS, 'days')) + }, + project: { $ne: null } }, - project: { $ne: null } - }) - for (const deletedProject of deletedProjects) { - await expireDeletedProject(deletedProject.deleterData.deletedProjectId) + { 'deleterData.deletedProjectId': 1 } + ).limit(PROJECT_EXPIRATION_BATCH_SIZE) + const projectIds = _.shuffle( + deletedProjects.map( + deletedProject => deletedProject.deleterData.deletedProjectId + ) + ) + for (const projectId of projectIds) { + await expireDeletedProject(projectId) } }