run parallely
This commit is contained in:
		
							parent
							
								
									6b0db9b877
								
							
						
					
					
						commit
						bf7601fa8d
					
				|  | @ -22,6 +22,7 @@ | |||
| 		"format": "gulp format" | ||||
| 	}, | ||||
| 	"devDependencies": { | ||||
| 		"@prezzemolo/zip": "0.0.3", | ||||
| 		"@types/bcryptjs": "2.4.1", | ||||
| 		"@types/body-parser": "1.16.7", | ||||
| 		"@types/chai": "4.0.4", | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue