Skip to content

Commit

Permalink
feat(examples): add nodejs_binary cluster example (#1515)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Wiles authored and alexeagle committed Jan 10, 2020
1 parent 84773e9 commit f217519
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
1 change: 1 addition & 0 deletions examples/nestjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion examples/nestjs/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
Expand All @@ -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(
Expand Down
18 changes: 5 additions & 13 deletions examples/nestjs/src/main.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
31 changes: 30 additions & 1 deletion examples/nestjs/src/main.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -11,7 +13,34 @@ export async function bootstrap(port: number): Promise<INestApplication> {
return app;
}

export async function bootstrapCluster(port: number): Promise<cluster.Worker[]|void> {
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);
}
17 changes: 15 additions & 2 deletions examples/nestjs/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down Expand Up @@ -1388,7 +1388,7 @@ [email protected]:
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==
Expand All @@ -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"

[email protected]:
version "0.1.13"
resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08"
Expand Down

0 comments on commit f217519

Please sign in to comment.