Last active
June 8, 2019 10:27
-
-
Save jirevwe/f50a2283f00c63a0a4448f9d9467488b to your computer and use it in GitHub Desktop.
Walk through a collection using a child process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const WALLET_REPORT = '../../WalletReport.xlsx'; | |
class GenerateReport { | |
wallets: Wallet[] = []; | |
duration: number = 0; | |
constructor() { | |
writeFileSync(join(__dirname, WALLET_REPORT), ''); | |
} | |
/** | |
* Fetches the wallets using a child process | |
* | |
* @param index start index | |
* @param times number of times it should run | |
* @param callback callback function | |
*/ | |
fetchWallets(index: number, times: number): Promise<FindWallets> { | |
return new Promise<FindWallets>((resolve, reject) => { | |
//NB: child process is invoked using compiled js file | |
const getNWallets = fork(join(__dirname, 'wallet.child.js')); | |
getNWallets.send(index); | |
getNWallets.on('message', (ws: FindWallets) => { | |
this.duration += ws.duration; | |
this.wallets.push(...ws.wallets); | |
if (index < times) getNWallets.send(++index); | |
else { | |
resolve({ wallets: this.wallets, duration: this.duration }); | |
getNWallets.kill(); | |
} | |
}); | |
getNWallets.on('error', err => { | |
reject(err); | |
}); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async function() { | |
try { | |
const generateReports = new GenerateReport(); | |
const foundWallets = await generateReports.fetchWallets(0, 22); | |
console.log(foundWallets.wallets.length, foundWallets.duration); | |
process.exit(0); | |
} catch (error) { | |
throw error; | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type FindWallets = { | |
wallets: Wallet[]; | |
duration: number; | |
}; | |
const fetchWallets = async function(index: number): Promise<FindWallets> { | |
const { WalletModel } = await buildModels(); | |
const start_time = performance.now(); | |
const countPerChildProcess = 1000; | |
const wallets: Wallet[] = await WalletModel.find() | |
.lean() | |
.limit(countPerChildProcess) | |
.skip(index * countPerChildProcess) | |
.sort({ ledger_balance: -1 }); | |
const end_time = performance.now(); | |
const duration = (end_time - start_time) / 1000; | |
logger.message(`✨ Duration: ${duration.toFixed(2)} seconds.`); | |
return { wallets, duration: duration }; | |
}; | |
process.on('message', async message => { | |
try { | |
const data = await fetchWallets(message); | |
process.send({ | |
wallets: data.wallets, | |
duration: data.duration, | |
}); | |
} catch (error) { | |
process.send(error); | |
process.exit(1); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment