From 665b1440d3f1a5392854c4c8fab7a89c8a40a482 Mon Sep 17 00:00:00 2001 From: Cassie Tarakajian Date: Wed, 27 Dec 2017 12:18:23 -0500 Subject: [PATCH] only use async --- server/migrations/moveBucket.js | 66 ++++++++++++++++----------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/server/migrations/moveBucket.js b/server/migrations/moveBucket.js index 9fd70e75..ebf03a5c 100644 --- a/server/migrations/moveBucket.js +++ b/server/migrations/moveBucket.js @@ -29,41 +29,37 @@ const CHUNK = 1000; Project.count({}) .exec().then(async (numProjects) => { console.log(numProjects); - for (let i = 0; i < numProjects; i += CHUNK) { - let projects = await Project.find({}).skip(i).limit(CHUNK).exec(); - async.eachSeries(projects, (project, cb) => { - console.log(project.name); - async.eachSeries(project.files, (file, fileCb) => { - if (file.url && file.url.includes('p5.js-webeditor')) { - file.url = file.url.replace('p5.js-webeditor', process.env.S3_BUCKET); - project.save((err, newProject) => { - console.log(`updated file ${file.url}`); - process.exit(0); + let index = 0; + async.whilst( + () => { + return index < numProjects; + }, + (whilstCb) => { + let projects = await Project.find({}).skip(index).limit(CHUNK).exec(); + async.eachSeries(projects, (project, cb) => { + console.log(project.name); + async.eachSeries(project.files, (file, fileCb) => { + if (file.url && file.url.includes('p5.js-webeditor')) { + file.url = file.url.replace('p5.js-webeditor', process.env.S3_BUCKET); + project.save((err, newProject) => { + console.log(`updated file ${file.url}`); + process.exit(0); + fileCb(); + }); + } else { fileCb(); - }); - } else { - fileCb(); - } - }, () => { - cb(); - }, () => { - console.log('at ' + i); + } + }, () => { + cb(); + }, () => { + index += CHUNK; + whilstCb(); + }); }); - }); - } + }, + () => { + console.log('finished processing all documents.') + process.exit(0); + } + ); }); - -// Project.find({}, (err, projects) => { -// projects.forEach((project, projectIndex) => { -// console.log(project.name); -// project.files.forEach((file, fileIndex) => { -// if (file.url && file.url.includes('p5.js-webeditor')) { -// file.url = file.url.replace('p5.js-webeditor', process.env.S3_BUCKET); -// } -// project.save((err, savedProject) => { -// console.log(`updated file ${file.url}`); -// process.exit(0); -// }); -// }); -// }); -// }); \ No newline at end of file