Skip to content

Commit 0f7ca50

Browse files
popomoreatian25
authored andcommitted
feat: [BREAKING_CHANGE] check the status of app when start on daemon (#9)
1 parent cfd0d2f commit 0f7ca50

File tree

19 files changed

+261
-75
lines changed

19 files changed

+261
-75
lines changed

.eslintignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
test/fixtures
21
coverage

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
logs/
22
npm-debug.log
3-
node_modules/
3+
/node_modules
44
coverage/
55
.idea/
66
run/
77
.DS_Store
88
*.swp
9-
!test/fixtures/example/node_modules
10-

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
deploy tool for egg project.
44

5+
**Note: Windows is not supported**
6+
57
## Install
68

79
```bash
@@ -54,13 +56,11 @@ $ egg-scripts start [options] [baseDir]
5456

5557
Stop egg gracefull.
5658

57-
**Note:** **Windows is not supported yet**, try to kill master process which command contains `start-cluster` or `--title=egg-server` yourself, good luck.
58-
5959
```bash
6060
# stop egg
6161
$ egg-scripts stop [baseDir]
6262
# egg-scripts stop ./server
6363
```
6464

6565
- **Arguments**
66-
- `baseDir` - directory of application, default to `process.cwd()`.
66+
- `baseDir` - directory of application, default to `process.cwd()`.

lib/cmd/start.js

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
const path = require('path');
44
const mkdirp = require('mz-modules/mkdirp');
5+
const sleep = require('mz-modules/sleep');
56
const homedir = require('node-homedir');
67
const utils = require('egg-utils');
78
const fs = require('mz/fs');
9+
const { exec } = require('mz/child_process');
810
const moment = require('moment');
911
const spawn = require('child_process').spawn;
1012
const Command = require('../command');
@@ -33,7 +35,7 @@ class StartCommand extends Command {
3335
default: process.env.PORT,
3436
},
3537
env: {
36-
description: 'egg server env, default to `process.env.EGG_SERVER_ENV`',
38+
description: 'server env, default to `process.env.EGG_SERVER_ENV`',
3739
default: process.env.EGG_SERVER_ENV,
3840
},
3941
framework: {
@@ -52,6 +54,11 @@ class StartCommand extends Command {
5254
description: 'A file that stderr redirect to',
5355
type: 'string',
5456
},
57+
timeout: {
58+
description: 'a timeout for start when daemon',
59+
type: 'number',
60+
default: 300 * 1000,
61+
},
5562
};
5663
}
5764

@@ -78,6 +85,8 @@ class StartCommand extends Command {
7885
baseDir,
7986
});
8087

88+
this.frameworkName = yield this.getFrameworkName(argv.framework);
89+
8190
const pkgInfo = require(path.join(baseDir, 'package.json'));
8291
argv.title = argv.title || `egg-server-${pkgInfo.name}`;
8392

@@ -120,27 +129,32 @@ class StartCommand extends Command {
120129
detached: false,
121130
};
122131

123-
this.logger.info(`starting egg application at ${baseDir}`);
132+
this.logger.info('Starting %s application at %s', this.frameworkName, baseDir);
124133

125134
const eggArgs = [ this.serverBin, JSON.stringify(argv), `--title=${argv.title}` ];
126-
this.logger.info('run node %s', eggArgs.join(' '));
135+
this.logger.info('Run node %s', eggArgs.join(' '));
127136

128137
// whether run in the background.
129138
if (isDaemon) {
130-
this.logger.info(`save log file to ${logDir}`);
139+
this.logger.info(`Save log file to ${logDir}`);
131140
const [ stdout, stderr ] = yield [ getRotatelog(argv.stdout), getRotatelog(argv.stderr) ];
132141
options.stdio = [ 'ignore', stdout, stderr, 'ipc' ];
133142
options.detached = true;
134143

135144
const child = this.child = spawn('node', eggArgs, options);
145+
this.isReady = false;
136146
child.on('message', msg => {
137147
if (msg && msg.action === 'egg-ready') {
138-
this.logger.info(`egg started on ${msg.data.address}`);
148+
this.isReady = true;
149+
this.logger.info('%s started on %s', this.frameworkName, msg.data.address);
139150
child.unref();
140151
child.disconnect();
141152
process.exit(0);
142153
}
143154
});
155+
156+
// check start status
157+
yield this.checkStatus(argv);
144158
} else {
145159
// signal event had been handler at common-bin helper
146160
this.helper.spawn('node', eggArgs, options);
@@ -151,6 +165,52 @@ class StartCommand extends Command {
151165
return utils.getFrameworkPath(params);
152166
}
153167

168+
* getFrameworkName(framework) {
169+
const pkgPath = path.join(framework, 'package.json');
170+
let name = 'egg';
171+
try {
172+
const pkg = require(pkgPath);
173+
if (pkg.name) name = pkg.name;
174+
} catch (_) {
175+
/* istanbul next */
176+
}
177+
return name;
178+
}
179+
180+
* checkStatus({ stderr, timeout }) {
181+
let count = 0;
182+
let isSuccess = true;
183+
timeout = timeout / 1000;
184+
while (!this.isReady) {
185+
try {
186+
const stat = yield fs.stat(stderr);
187+
if (stat && stat.size > 0) {
188+
const [ stdout ] = yield exec('tail -n 100 ' + stderr);
189+
this.logger.error(stdout);
190+
this.logger.error('Start failed, see %s', stderr);
191+
isSuccess = false;
192+
break;
193+
}
194+
} catch (_) {
195+
// nothing
196+
}
197+
198+
if (count >= timeout) {
199+
this.logger.error('Start failed, %ds timeout', timeout);
200+
isSuccess = false;
201+
break;
202+
}
203+
204+
yield sleep(1000);
205+
this.logger.log('Wait Start: %d...', ++count);
206+
}
207+
208+
if (!isSuccess) {
209+
this.child.kill('SIGTERM');
210+
yield sleep(1000);
211+
process.exit(1);
212+
}
213+
}
154214
}
155215

156216
function* getRotatelog(logfile) {

package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@
77
"egg-scripts": "bin/egg-scripts.js"
88
},
99
"dependencies": {
10-
"common-bin": "^2.5.0",
10+
"common-bin": "^2.7.1",
1111
"egg-utils": "^2.2.0",
12-
"moment": "^2.18.1",
13-
"mz": "^2.6.0",
14-
"mz-modules": "^1.0.0",
15-
"node-homedir": "^1.0.0",
12+
"moment": "^2.19.1",
13+
"mz": "^2.7.0",
14+
"mz-modules": "^2.0.0",
15+
"node-homedir": "^1.1.0",
1616
"runscript": "^1.3.0",
1717
"zlogger": "^1.1.0"
1818
},
1919
"devDependencies": {
20-
"autod": "^2.9.0",
20+
"autod": "^2.10.1",
2121
"coffee": "^4.1.0",
22-
"egg": "^1.7.0",
23-
"egg-bin": "^4.1.0",
22+
"egg": "^1.9.0",
23+
"egg-bin": "^4.3.5",
2424
"egg-ci": "^1.8.0",
25-
"eslint": "^4.4.1",
26-
"eslint-config-egg": "^5.0.0",
27-
"mm": "^2.1.0",
28-
"urllib": "^2.24.0",
25+
"eslint": "^4.8.0",
26+
"eslint-config-egg": "^5.1.1",
27+
"mm": "^2.2.0",
28+
"urllib": "^2.25.0",
2929
"webstorm-disable-index": "^1.2.0"
3030
},
3131
"engines": {

test/fixtures/cluster-config/config/config.prod.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
exports.cluster = {
44
listen: {
55
port: 8000,
6-
}
7-
}
6+
},
7+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
exports.keys = '123456';
4+
5+
exports.logger = {
6+
level: 'WARN',
7+
consoleLevel: 'WARN',
8+
};

test/fixtures/egg-app/node_modules/egg/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/egg-app/node_modules/egg/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/egg-app/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "example",
3+
"dependencies": {
4+
"egg": "^1.0.0"
5+
}
6+
}

0 commit comments

Comments
 (0)