Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions lib/cmd/start.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是不是不需要这个配置了

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原来的有什么问题?为什么不在 helper.spawn 加参数实现,而又在这里实现重复的逻辑代码?
背景问题是什么?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fengmk2 要暴露出 child 对象供上层监听使用。我跟天猪讨论后的方案有:

  1. 暴露出 helper 里面的 childs
  2. helper 里面年久失修,重构估计会变成 break,所以直接这一块逻辑替换。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helper 返回 child 不行?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helper 里面是一个 Promise,要等子进程结束后才会被 resolve

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 => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看起来好像这个库不支持 cp.spawn()

process.once(event, () => {
signal = event;
process.exit(0);
});
});
process.once('exit', () => {
debug('Kill child %s with %s', child.pid, signal);
child.kill(signal);
});
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 24 additions & 1 deletion test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -514,6 +538,5 @@ describe('test/start.test.js', () => {
.expect('code', 1)
.end();
});

});
});