From c073253cd0fa537ee1dbc699d4ce89b65bdc41ec Mon Sep 17 00:00:00 2001 From: Zachary Golba Date: Mon, 18 Apr 2016 10:24:27 -0400 Subject: [PATCH] fix: listening message dispatched before workers are ready (#34) --- examples/social-network/package.json | 2 +- examples/todo/package.json | 2 +- package.json | 2 +- src/packages/application/index.js | 5 +++-- src/packages/cli/commands/serve.js | 18 ++++++++++++------ src/packages/cli/index.js | 2 +- src/packages/cli/templates/package-json.js | 2 +- 7 files changed, 20 insertions(+), 13 deletions(-) diff --git a/examples/social-network/package.json b/examples/social-network/package.json index 383c1391..daf9a22e 100644 --- a/examples/social-network/package.json +++ b/examples/social-network/package.json @@ -10,7 +10,7 @@ "author": "", "license": "MIT", "dependencies": { - "lux-framework": "0.0.1-beta.1", + "lux-framework": "0.0.1-beta.2", "babel-core": "6.7.4", "babel-eslint": "6.0.2", "babel-plugin-transform-decorators-legacy": "1.3.4", diff --git a/examples/todo/package.json b/examples/todo/package.json index 4dc8b018..b51a7a9c 100644 --- a/examples/todo/package.json +++ b/examples/todo/package.json @@ -10,7 +10,7 @@ "author": "", "license": "MIT", "dependencies": { - "lux-framework": "0.0.1-beta.1", + "lux-framework": "0.0.1-beta.2", "babel-core": "6.7.4", "babel-eslint": "6.0.2", "babel-plugin-transform-decorators-legacy": "1.3.4", diff --git a/package.json b/package.json index b76be94a..16714ff8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lux-framework", - "version": "0.0.1-beta.1", + "version": "0.0.1-beta.2", "description": "A MVC style Node.js framework for building lightning fast JSON APIs", "repository": "https://github.com/postlight/lux", "main": "dist/index.js", diff --git a/src/packages/application/index.js b/src/packages/application/index.js index 3e899343..930369b6 100644 --- a/src/packages/application/index.js +++ b/src/packages/application/index.js @@ -28,7 +28,7 @@ class Application extends Base { } async boot() { - const { root, router, domain } = this; + const { root, router, domain, server, port } = this; const store = Database.create( require(`${root}/config/database.json`) ); @@ -133,7 +133,8 @@ class Application extends Base { routes.get('routes').call(null, router.route, router.resource); - this.server.listen(this.port); + server.instance.once('listening', () => process.send('ready')); + server.listen(port); return this; } diff --git a/src/packages/cli/commands/serve.js b/src/packages/cli/commands/serve.js index 092bdb6a..9e20dc79 100644 --- a/src/packages/cli/commands/serve.js +++ b/src/packages/cli/commands/serve.js @@ -12,18 +12,24 @@ export default async function serve(port = 4000) { const Application = require(`${pwd}/bin/app`); const config = require(`${pwd}/config/environments/${env}.json`); const logger = await Logger.create(); - let workers = 0; if (config.port) { port = config.port; } if (cluster.isMaster) { - for (var i = 0; i < os.cpus().length; i++) { - cluster.fork().once('online', () => { - workers++; - if (i === workers) { - logger.log(`Lux Server listening on port ${cyan(`${port}`)}`); + const total = os.cpus().length; + let current = 0; + + logger.log(`Starting Lux Server with ${cyan(`${total}`)} worker processes`); + + for (let i = 0; i < total; i++) { + cluster.fork().once('message', msg => { + if (msg === 'ready') { + current++; + if (current === total) { + logger.log(`Lux Server listening on port ${cyan(`${port}`)}`); + } } }); } diff --git a/src/packages/cli/index.js b/src/packages/cli/index.js index 7cc9e2b2..697f5c9a 100644 --- a/src/packages/cli/index.js +++ b/src/packages/cli/index.js @@ -8,7 +8,7 @@ import generate from './commands/generate'; import tryCatch from '../../utils/try-catch'; -cli.version('0.0.1-beta.1'); +cli.version('0.0.1-beta.2'); cli .command('n ') diff --git a/src/packages/cli/templates/package-json.js b/src/packages/cli/templates/package-json.js index 43f1e2ed..e44d3c5d 100644 --- a/src/packages/cli/templates/package-json.js +++ b/src/packages/cli/templates/package-json.js @@ -12,7 +12,7 @@ export default (name) => { "author": "", "license": "MIT", "dependencies": { - "lux-framework": "0.0.1-beta.1", + "lux-framework": "0.0.1-beta.2", "babel-core": "6.7.4", "babel-eslint": "6.0.2", "babel-plugin-transform-decorators-legacy": "1.3.4",