diff --git a/lib/cmd/start.js b/lib/cmd/start.js index 5d562d6..c5e2188 100644 --- a/lib/cmd/start.js +++ b/lib/cmd/start.js @@ -1,15 +1,17 @@ 'use strict'; const path = require('path'); -const mkdirp = require('mz-modules/mkdirp'); -const sleep = require('mz-modules/sleep'); -const homedir = require('node-homedir'); -const utils = require('egg-utils'); -const fs = require('mz/fs'); + +const Command = require('../command'); +const debug = require('debug')('egg-script:start'); const { exec } = require('mz/child_process'); +const fs = require('mz/fs'); +const homedir = require('node-homedir'); +const mkdirp = require('mz-modules/mkdirp'); const moment = require('moment'); +const sleep = require('mz-modules/sleep'); const spawn = require('child_process').spawn; -const Command = require('../command'); +const utils = require('egg-utils'); class StartCommand extends Command { constructor(rawArgv) { @@ -161,8 +163,27 @@ class StartCommand extends Command { // check start status yield this.checkStatus(argv); } else { - // signal event had been handler at common-bin helper - this.helper.spawn('node', eggArgs, options); + options.stdio = options.stdio || 'inherit'; + debug('Run spawn `node %s`', eggArgs.join(' ')); + const child = this.child = spawn('node', eggArgs, options); + child.once('exit', code => { + if (code !== 0) { + child.emit('error', new Error(`spawn node ${eggArgs.join(' ')} fail, exit code: ${code}`)); + } + }); + + // attach master signal to child + let signal; + [ 'SIGINT', 'SIGQUIT', 'SIGTERM' ].forEach(event => { + process.once(event, () => { + signal = event; + process.exit(0); + }); + }); + process.once('exit', () => { + debug('Kill child %s with %s', child.pid, signal); + child.kill(signal); + }); } } diff --git a/package.json b/package.json index 6f2de64..38818d7 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ }, "dependencies": { "common-bin": "^2.7.1", + "debug": "^3.1.0", "egg-utils": "^2.3.0", "moment": "^2.19.2", "mz": "^2.7.0", diff --git a/test/start.test.js b/test/start.test.js index 9c9a49d..972560e 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -59,6 +59,30 @@ describe('test/start.test.js', () => { }); }); + describe('child exit with 1', () => { + let app; + + before(function* () { + yield utils.cleanup(fixturePath); + }); + + after(function* () { + app.proc.kill('SIGTERM'); + yield utils.cleanup(fixturePath); + }); + + it('should emit spawn error', function* () { + const srv = require('http').createServer(() => {}); + srv.listen(7007); + + app = coffee.fork(eggBin, [ 'start', '--port=7007', '--workers=2', fixturePath ]); + + yield sleep(waitTime); + assert(/Error: spawn node .+ fail, exit code: 1/.test(app.stderr)); + srv.close(); + }); + }); + describe('relative path', () => { let app; @@ -514,6 +538,5 @@ describe('test/start.test.js', () => { .expect('code', 1) .end(); }); - }); });