diff options
| author | otofune <otofune@gmail.com> | 2017-11-07 22:08:21 +0900 |
|---|---|---|
| committer | otofune <otofune@gmail.com> | 2017-11-07 22:08:35 +0900 |
| commit | bf7601fa8d2728969823fb5a6ce24ae47cf4a992 (patch) | |
| tree | 880f28ece3d49643c5b17bb838f4c86c02ef27c4 /tools | |
| parent | :v: (diff) | |
| download | sharkey-bf7601fa8d2728969823fb5a6ce24ae47cf4a992.tar.gz sharkey-bf7601fa8d2728969823fb5a6ce24ae47cf4a992.tar.bz2 sharkey-bf7601fa8d2728969823fb5a6ce24ae47cf4a992.zip | |
run parallely
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/migration/use-gridfs.js | 33 |
1 files changed, 11 insertions, 22 deletions
diff --git a/tools/migration/use-gridfs.js b/tools/migration/use-gridfs.js index 106cbd3889..228943f49f 100644 --- a/tools/migration/use-gridfs.js +++ b/tools/migration/use-gridfs.js @@ -3,6 +3,7 @@ const { default: db } = require('../../built/db/mongodb') const { default: DriveFile, getGridFSBucket } = require('../../built/api/models/drive-file') const { Duplex } = require('stream') +const { default: zip } = require('@prezzemolo/zip') const writeToGridFS = (bucket, buffer, ...rest) => new Promise((resolve, reject) => { const writeStream = bucket.openUploadStreamWithId(...rest) @@ -45,30 +46,18 @@ const migrateToGridFS = async (doc) => { } 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 () => { - if (i == count) return true; - console.log(`${i} / ${count}`); - const doc = (await db.get('drive_files').find({}, { limit: 1, skip: i }))[0] - const res = await migrateToGridFS(doc); - if (!res) { - return false; - } else { - i++ - return await iterate(); - } - } - - const res = await iterate(); - - if (res) { - return 'ok'; - } else { - throw 'something happened'; - } + return zip( + 1, + async (time) => { + const doc = await DriveFile.find({}, { limit: dop, skip: time * dop }) + return Promise.all(doc.map(migrateToGridFS)) + }, + ((count - (count % dop)) / dop) + 1 + ) } main().then(console.dir).catch(console.error) |