Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 665b144

Browse files
committed
only use async
1 parent 00e49d6 commit 665b144

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

server/migrations/moveBucket.js

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,37 @@ const CHUNK = 1000;
2929
Project.count({})
3030
.exec().then(async (numProjects) => {
3131
console.log(numProjects);
32-
for (let i = 0; i < numProjects; i += CHUNK) {
33-
let projects = await Project.find({}).skip(i).limit(CHUNK).exec();
34-
async.eachSeries(projects, (project, cb) => {
35-
console.log(project.name);
36-
async.eachSeries(project.files, (file, fileCb) => {
37-
if (file.url && file.url.includes('p5.js-webeditor')) {
38-
file.url = file.url.replace('p5.js-webeditor', process.env.S3_BUCKET);
39-
project.save((err, newProject) => {
40-
console.log(`updated file ${file.url}`);
41-
process.exit(0);
32+
let index = 0;
33+
async.whilst(
34+
() => {
35+
return index < numProjects;
36+
},
37+
(whilstCb) => {
38+
let projects = await Project.find({}).skip(index).limit(CHUNK).exec();
39+
async.eachSeries(projects, (project, cb) => {
40+
console.log(project.name);
41+
async.eachSeries(project.files, (file, fileCb) => {
42+
if (file.url && file.url.includes('p5.js-webeditor')) {
43+
file.url = file.url.replace('p5.js-webeditor', process.env.S3_BUCKET);
44+
project.save((err, newProject) => {
45+
console.log(`updated file ${file.url}`);
46+
process.exit(0);
47+
fileCb();
48+
});
49+
} else {
4250
fileCb();
43-
});
44-
} else {
45-
fileCb();
46-
}
47-
}, () => {
48-
cb();
49-
}, () => {
50-
console.log('at ' + i);
51+
}
52+
}, () => {
53+
cb();
54+
}, () => {
55+
index += CHUNK;
56+
whilstCb();
57+
});
5158
});
52-
});
53-
}
59+
},
60+
() => {
61+
console.log('finished processing all documents.')
62+
process.exit(0);
63+
}
64+
);
5465
});
55-
56-
// Project.find({}, (err, projects) => {
57-
// projects.forEach((project, projectIndex) => {
58-
// console.log(project.name);
59-
// project.files.forEach((file, fileIndex) => {
60-
// if (file.url && file.url.includes('p5.js-webeditor')) {
61-
// file.url = file.url.replace('p5.js-webeditor', process.env.S3_BUCKET);
62-
// }
63-
// project.save((err, savedProject) => {
64-
// console.log(`updated file ${file.url}`);
65-
// process.exit(0);
66-
// });
67-
// });
68-
// });
69-
// });

0 commit comments

Comments
 (0)