Skip to content

Commit e07726c

Browse files
XadillaXpopomore
authored andcommitted
refactor: raw spawn call to instead of helper.spawn in start non-daemon mode (#23)
1 parent 22faa4c commit e07726c

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

lib/cmd/start.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
'use strict';
22

33
const path = require('path');
4-
const mkdirp = require('mz-modules/mkdirp');
5-
const sleep = require('mz-modules/sleep');
6-
const homedir = require('node-homedir');
7-
const utils = require('egg-utils');
8-
const fs = require('mz/fs');
4+
5+
const Command = require('../command');
6+
const debug = require('debug')('egg-script:start');
97
const { exec } = require('mz/child_process');
8+
const fs = require('mz/fs');
9+
const homedir = require('node-homedir');
10+
const mkdirp = require('mz-modules/mkdirp');
1011
const moment = require('moment');
12+
const sleep = require('mz-modules/sleep');
1113
const spawn = require('child_process').spawn;
12-
const Command = require('../command');
14+
const utils = require('egg-utils');
1315

1416
class StartCommand extends Command {
1517
constructor(rawArgv) {
@@ -161,8 +163,27 @@ class StartCommand extends Command {
161163
// check start status
162164
yield this.checkStatus(argv);
163165
} else {
164-
// signal event had been handler at common-bin helper
165-
this.helper.spawn('node', eggArgs, options);
166+
options.stdio = options.stdio || 'inherit';
167+
debug('Run spawn `node %s`', eggArgs.join(' '));
168+
const child = this.child = spawn('node', eggArgs, options);
169+
child.once('exit', code => {
170+
if (code !== 0) {
171+
child.emit('error', new Error(`spawn node ${eggArgs.join(' ')} fail, exit code: ${code}`));
172+
}
173+
});
174+
175+
// attach master signal to child
176+
let signal;
177+
[ 'SIGINT', 'SIGQUIT', 'SIGTERM' ].forEach(event => {
178+
process.once(event, () => {
179+
signal = event;
180+
process.exit(0);
181+
});
182+
});
183+
process.once('exit', () => {
184+
debug('Kill child %s with %s', child.pid, signal);
185+
child.kill(signal);
186+
});
166187
}
167188
}
168189

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"dependencies": {
1111
"common-bin": "^2.7.1",
12+
"debug": "^3.1.0",
1213
"egg-utils": "^2.3.0",
1314
"moment": "^2.19.2",
1415
"mz": "^2.7.0",

test/start.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ describe('test/start.test.js', () => {
5959
});
6060
});
6161

62+
describe('child exit with 1', () => {
63+
let app;
64+
65+
before(function* () {
66+
yield utils.cleanup(fixturePath);
67+
});
68+
69+
after(function* () {
70+
app.proc.kill('SIGTERM');
71+
yield utils.cleanup(fixturePath);
72+
});
73+
74+
it('should emit spawn error', function* () {
75+
const srv = require('http').createServer(() => {});
76+
srv.listen(7007);
77+
78+
app = coffee.fork(eggBin, [ 'start', '--port=7007', '--workers=2', fixturePath ]);
79+
80+
yield sleep(waitTime);
81+
assert(/Error: spawn node .+ fail, exit code: 1/.test(app.stderr));
82+
srv.close();
83+
});
84+
});
85+
6286
describe('relative path', () => {
6387
let app;
6488

@@ -514,6 +538,5 @@ describe('test/start.test.js', () => {
514538
.expect('code', 1)
515539
.end();
516540
});
517-
518541
});
519542
});

0 commit comments

Comments
 (0)