summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAya Morisawa <AyaMorisawa4869@gmail.com>2018-11-20 12:25:58 +0900
committerGitHub <noreply@github.com>2018-11-20 12:25:58 +0900
commit168db6891f45dd2a1b31fbf26cbbc54c124f629a (patch)
tree9a9ea9b6df8205bbb2780ef5ed51621bef93ea81 /src
parentRefactor port checking (#3336) (diff)
downloadsharkey-168db6891f45dd2a1b31fbf26cbbc54c124f629a.tar.gz
sharkey-168db6891f45dd2a1b31fbf26cbbc54c124f629a.tar.bz2
sharkey-168db6891f45dd2a1b31fbf26cbbc54c124f629a.zip
Refactor spawnWorkers (#3338)
Diffstat (limited to 'src')
-rw-r--r--src/index.ts36
1 files changed, 13 insertions, 23 deletions
diff --git a/src/index.ts b/src/index.ts
index 7ad52ccf2a..f5e8adb707 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -219,31 +219,21 @@ function checkMongoDb(config: Config) {
});
}
-function spawnWorkers(limit: number) {
- Logger.info('Starting workers...');
+async function spawnWorkers(limit: number) {
+ const workers = Math.min(limit, os.cpus().length);
+ Logger.info(`Starting ${workers} worker${workers === 1 ? '' : 's'}...`);
+ await Promise.all([...Array(workers)].map(spawnWorker));
+ Logger.succ('All workers started');
+}
+function spawnWorker(): Promise<void> {
return new Promise(res => {
- // Count the machine's CPUs
- const cpuCount = os.cpus().length;
-
- const count = limit || cpuCount;
- let started = 0;
-
- // Create a worker for each CPU
- for (let i = 0; i < count; i++) {
- const worker = cluster.fork();
-
- worker.on('message', message => {
- if (message !== 'ready') return;
- started++;
-
- // When all workers started
- if (started == count) {
- Logger.succ('All workers started');
- res();
- }
- });
- }
+ const worker = cluster.fork();
+ worker.on('message', message => {
+ if (message !== 'ready') return;
+ Logger.succ('A worker started');
+ res();
+ });
});
}