From f217519253e7468de82c2dfccdfedc7dac4f98be Mon Sep 17 00:00:00 2001 From: Fabian Wiles Date: Fri, 10 Jan 2020 16:44:55 +1300 Subject: [PATCH] feat(examples): add nodejs_binary cluster example (#1515) --- examples/nestjs/package.json | 1 + examples/nestjs/src/BUILD.bazel | 5 ++++- examples/nestjs/src/main.spec.ts | 18 +++++------------- examples/nestjs/src/main.ts | 31 ++++++++++++++++++++++++++++++- examples/nestjs/yarn.lock | 17 +++++++++++++++-- 5 files changed, 55 insertions(+), 17 deletions(-) diff --git a/examples/nestjs/package.json b/examples/nestjs/package.json index f1894fc56f..20ac511e86 100644 --- a/examples/nestjs/package.json +++ b/examples/nestjs/package.json @@ -8,6 +8,7 @@ "@nestjs/core": "6.5.2", "@nestjs/platform-express": "6.5.2", "@types/jasmine": "^3.5.0", + "@types/superagent": "^4.1.4", "@types/supertest": "^2.0.8", "minimist": "1.2.0", "reflect-metadata": "0.1.13", diff --git a/examples/nestjs/src/BUILD.bazel b/examples/nestjs/src/BUILD.bazel index 0b94cf7735..a994db8443 100644 --- a/examples/nestjs/src/BUILD.bazel +++ b/examples/nestjs/src/BUILD.bazel @@ -40,8 +40,10 @@ ts_library( ":app", "@npm//@nestjs/common", "@npm//@types/jasmine", + "@npm//@types/superagent", "@npm//@types/supertest", "@npm//jasmine", + "@npm//superagent", "@npm//supertest", ], ) @@ -56,7 +58,8 @@ nodejs_binary( "@npm//@nestjs/core", "@npm//minimist", ], - entry_point = ":main.ts", + entry_point = ":main", + templated_args = ["--nobazel_patch_module_resolver"], ) jasmine_node_test( diff --git a/examples/nestjs/src/main.spec.ts b/examples/nestjs/src/main.spec.ts index 54d64280ca..0a4cb9cda2 100644 --- a/examples/nestjs/src/main.spec.ts +++ b/examples/nestjs/src/main.spec.ts @@ -1,21 +1,13 @@ -import {INestApplication} from '@nestjs/common'; import * as request from 'supertest'; -import {bootstrap} from './main'; +import {bootstrap, bootstrapCluster} from './main'; describe('App', () => { - let server: INestApplication; - - beforeAll(async () => { - server = await bootstrap(3000); - }); - afterAll(async () => { - await server.close(); - }) - - it(`GET /`, () => { - return request(server.getHttpServer()).get('/hello').expect(200).expect({ + it(`GET /`, async () => { + const server = await bootstrap(3000); + await request(server.getHttpServer()).get('/hello').expect(200).expect({ message: 'Hello world!' }); + await server.close(); }); }); \ No newline at end of file diff --git a/examples/nestjs/src/main.ts b/examples/nestjs/src/main.ts index 55601f4641..c0ee9ac60b 100644 --- a/examples/nestjs/src/main.ts +++ b/examples/nestjs/src/main.ts @@ -1,6 +1,8 @@ import {INestApplication, Logger} from '@nestjs/common'; import {NestFactory} from '@nestjs/core'; import {ExpressAdapter} from '@nestjs/platform-express'; +import * as cluster from 'cluster'; +import * as os from 'os'; import {AppModule} from './app.module'; @@ -11,7 +13,34 @@ export async function bootstrap(port: number): Promise { return app; } +export async function bootstrapCluster(port: number): Promise { + return new Promise((resolve, reject) => { + if (cluster.isMaster) { + const cpuCount = os.cpus().length; + + for (let i = 0; i < cpuCount; i += 1) { + cluster.fork(); + } + + let workers = []; + cluster.on('online', worker => { + Logger.log('Worker ' + worker.process.pid + ' is online.'); + workers.push(worker); + if (workers.length === cpuCount) { + resolve(workers); + } + }); + cluster.on('exit', ({process}, code, signal) => { + Logger.log('worker ' + process.pid + ' died.'); + }); + } else { + bootstrap(port); + resolve(); + } + }) +} + if (require.main === module) { const argv = require('minimist')(process.argv.slice(2)); - bootstrap(argv.port || 3000); + bootstrapCluster(argv.port || 3000); } diff --git a/examples/nestjs/yarn.lock b/examples/nestjs/yarn.lock index 358bdbc1d0..c8700fb4d2 100644 --- a/examples/nestjs/yarn.lock +++ b/examples/nestjs/yarn.lock @@ -180,7 +180,7 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-10.14.21.tgz#4a9db7ef1d1671c0015e632c5fa3d46c86c58c1e" integrity sha512-nuFlRdBiqbF+PJIEVxm2jLFcQWN7q7iWEJGsBV4n7v1dbI9qXB8im2pMMKMCUZe092sQb5SQft2DHfuQGK5hqQ== -"@types/superagent@*": +"@types/superagent@*", "@types/superagent@^4.1.4": version "4.1.4" resolved "https://registry.yarnpkg.com/@types/superagent/-/superagent-4.1.4.tgz#63f74955a28073870cfd9c100bcacb26d72b3764" integrity sha512-SRH2q6/5/nhOkAuLXm3azRGjBYpoKCZWh138Rt1AxSIyE6/1b9uClIH2V+JfyDtjIvgr5yQqYgNUmdpbneJoZQ== @@ -1388,7 +1388,7 @@ readable-stream@1.1.x: isarray "0.0.1" string_decoder "~0.10.x" -readable-stream@^2.2.2, readable-stream@^2.3.5: +readable-stream@^2.2.2: version "2.3.6" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.6.tgz#b11c27d88b8ff1fbe070643cf94b0c79ae1b0aaf" integrity sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw== @@ -1401,6 +1401,19 @@ readable-stream@^2.2.2, readable-stream@^2.3.5: string_decoder "~1.1.1" util-deprecate "~1.0.1" +readable-stream@^2.3.5: + version "2.3.7" + resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.7.tgz#1eca1cf711aef814c04f62252a36a62f6cb23b57" + integrity sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw== + dependencies: + core-util-is "~1.0.0" + inherits "~2.0.3" + isarray "~1.0.0" + process-nextick-args "~2.0.0" + safe-buffer "~5.1.1" + string_decoder "~1.1.1" + util-deprecate "~1.0.1" + reflect-metadata@0.1.13: version "0.1.13" resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08"