-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Node.js clustering with Nest application
- Loading branch information
Showing
9 changed files
with
59 additions
and
13 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import cluster from 'cluster'; | ||
import os from 'os'; | ||
|
||
const numCPUs = os.cpus().length; | ||
|
||
/** | ||
* @example | ||
* ```ts | ||
* AppClusterService.clustering(bootstrap); | ||
* ``` | ||
*/ | ||
@Injectable() | ||
export class AppClusterService { | ||
static logger = new Logger(AppClusterService.name); | ||
|
||
static async clustering(callback: () => unknown): Promise<unknown> { | ||
if (cluster.isPrimary) { | ||
this.logger.log(`Master server started on ${process.pid}`); | ||
for (let i = 0; i < numCPUs; i++) { | ||
cluster.fork(); | ||
await this.delay(5000); | ||
} | ||
|
||
cluster.on('exit', (worker, code, signal) => { | ||
this.logger.log( | ||
`Worker ${worker.process.pid} died with code ${code} and signal ${signal}.`, | ||
); | ||
cluster.fork(); | ||
}); | ||
} else { | ||
callback(); | ||
this.logger.log(`Cluster server started on ${process.pid}`); | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
static delay(ms: number) { | ||
return new Promise((res) => setTimeout(res, ms)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './app-cluster.service'; | ||
export * from './helper.module'; | ||
export * from './helper.service'; |
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
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