run parallely

This commit is contained in:
otofune 2017-11-07 22:08:21 +09:00
parent 6b0db9b877
commit bf7601fa8d
2 changed files with 12 additions and 22 deletions

View file

@ -22,6 +22,7 @@
"format": "gulp format" "format": "gulp format"
}, },
"devDependencies": { "devDependencies": {
"@prezzemolo/zip": "0.0.3",
"@types/bcryptjs": "2.4.1", "@types/bcryptjs": "2.4.1",
"@types/body-parser": "1.16.7", "@types/body-parser": "1.16.7",
"@types/chai": "4.0.4", "@types/chai": "4.0.4",

View file

@ -3,6 +3,7 @@
const { default: db } = require('../../built/db/mongodb') const { default: db } = require('../../built/db/mongodb')
const { default: DriveFile, getGridFSBucket } = require('../../built/api/models/drive-file') const { default: DriveFile, getGridFSBucket } = require('../../built/api/models/drive-file')
const { Duplex } = require('stream') const { Duplex } = require('stream')
const { default: zip } = require('@prezzemolo/zip')
const writeToGridFS = (bucket, buffer, ...rest) => new Promise((resolve, reject) => { const writeToGridFS = (bucket, buffer, ...rest) => new Promise((resolve, reject) => {
const writeStream = bucket.openUploadStreamWithId(...rest) const writeStream = bucket.openUploadStreamWithId(...rest)
@ -45,30 +46,18 @@ const migrateToGridFS = async (doc) => {
} }
async function main() { async function main() {
let i = 0; const count = await DriveFile.count({});
const count = await db.get('drive_files').count({}); const dop = Number.parseInt(process.argv[2]) || 5
const iterate = async () => { return zip(
if (i == count) return true; 1,
console.log(`${i} / ${count}`); async (time) => {
const doc = (await db.get('drive_files').find({}, { limit: 1, skip: i }))[0] const doc = await DriveFile.find({}, { limit: dop, skip: time * dop })
const res = await migrateToGridFS(doc); return Promise.all(doc.map(migrateToGridFS))
if (!res) { },
return false; ((count - (count % dop)) / dop) + 1
} else { )
i++
return await iterate();
}
}
const res = await iterate();
if (res) {
return 'ok';
} else {
throw 'something happened';
}
} }
main().then(console.dir).catch(console.error) main().then(console.dir).catch(console.error)