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
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
test/fixtures
coverage
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
logs/
npm-debug.log
node_modules/
/node_modules
coverage/
.idea/
run/
.DS_Store
*.swp
!test/fixtures/example/node_modules

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

deploy tool for egg project.

**Note: Windows is not supported**

Copy link
Member

Choose a reason for hiding this comment

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

算不算 break?之前好像有人在 win 使用。

## Install

```bash
Expand Down Expand Up @@ -54,13 +56,11 @@ $ egg-scripts start [options] [baseDir]

Stop egg gracefull.

**Note:** **Windows is not supported yet**, try to kill master process which command contains `start-cluster` or `--title=egg-server` yourself, good luck.

```bash
# stop egg
$ egg-scripts stop [baseDir]
# egg-scripts stop ./server
```

- **Arguments**
- `baseDir` - directory of application, default to `process.cwd()`.
- `baseDir` - directory of application, default to `process.cwd()`.
70 changes: 65 additions & 5 deletions lib/cmd/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

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 { exec } = require('mz/child_process');
const moment = require('moment');
const spawn = require('child_process').spawn;
const Command = require('../command');
Expand Down Expand Up @@ -33,7 +35,7 @@ class StartCommand extends Command {
default: process.env.PORT,
},
env: {
description: 'egg server env, default to `process.env.EGG_SERVER_ENV`',
description: 'server env, default to `process.env.EGG_SERVER_ENV`',
default: process.env.EGG_SERVER_ENV,
},
framework: {
Expand All @@ -52,6 +54,11 @@ class StartCommand extends Command {
description: 'A file that stderr redirect to',
type: 'string',
},
timeout: {
description: 'a timeout for start when daemon',
type: 'number',
default: 300 * 1000,
},
};
}

Expand All @@ -78,6 +85,8 @@ class StartCommand extends Command {
baseDir,
});

this.frameworkName = yield this.getFrameworkName(argv.framework);

const pkgInfo = require(path.join(baseDir, 'package.json'));
argv.title = argv.title || `egg-server-${pkgInfo.name}`;

Expand Down Expand Up @@ -120,27 +129,32 @@ class StartCommand extends Command {
detached: false,
};

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

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

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

const child = this.child = spawn('node', eggArgs, options);
this.isReady = false;
child.on('message', msg => {
if (msg && msg.action === 'egg-ready') {
this.logger.info(`egg started on ${msg.data.address}`);
this.isReady = true;
this.logger.info('%s started on %s', this.frameworkName, msg.data.address);
child.unref();
child.disconnect();
process.exit(0);
}
});

// check start status
yield this.checkStatus(argv);
Copy link
Member

Choose a reason for hiding this comment

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

这里直接用 getRotatelog(argv.stderr) 返回的流?就不用读文件了?

Copy link
Member Author

Choose a reason for hiding this comment

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

哪里读文件了?

} else {
// signal event had been handler at common-bin helper
this.helper.spawn('node', eggArgs, options);
Expand All @@ -151,6 +165,52 @@ class StartCommand extends Command {
return utils.getFrameworkPath(params);
}

* getFrameworkName(framework) {
const pkgPath = path.join(framework, 'package.json');
let name = 'egg';
try {
const pkg = require(pkgPath);
if (pkg.name) name = pkg.name;
} catch (_) {
/* istanbul next */
}
return name;
}

* checkStatus({ stderr, timeout }) {
let count = 0;
let isSuccess = true;
timeout = timeout / 1000;
while (!this.isReady) {
try {
const stat = yield fs.stat(stderr);
if (stat && stat.size > 0) {
const [ stdout ] = yield exec('tail -n 100 ' + stderr);
this.logger.error(stdout);
this.logger.error('Start failed, see %s', stderr);
isSuccess = false;
break;
}
} catch (_) {
// nothing
}

if (count >= timeout) {
this.logger.error('Start failed, %ds timeout', timeout);
isSuccess = false;
break;
}

yield sleep(1000);
this.logger.log('Wait Start: %d...', ++count);
}

if (!isSuccess) {
this.child.kill('SIGTERM');
yield sleep(1000);
process.exit(1);
}
}
}

function* getRotatelog(logfile) {
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
"egg-scripts": "bin/egg-scripts.js"
},
"dependencies": {
"common-bin": "^2.5.0",
"common-bin": "^2.7.1",
"egg-utils": "^2.2.0",
"moment": "^2.18.1",
"mz": "^2.6.0",
"mz-modules": "^1.0.0",
"node-homedir": "^1.0.0",
"moment": "^2.19.1",
"mz": "^2.7.0",
"mz-modules": "^2.0.0",
"node-homedir": "^1.1.0",
"runscript": "^1.3.0",
"zlogger": "^1.1.0"
},
"devDependencies": {
"autod": "^2.9.0",
"autod": "^2.10.1",
"coffee": "^4.1.0",
"egg": "^1.7.0",
"egg-bin": "^4.1.0",
"egg": "^1.9.0",
"egg-bin": "^4.3.5",
"egg-ci": "^1.8.0",
"eslint": "^4.4.1",
"eslint-config-egg": "^5.0.0",
"mm": "^2.1.0",
"urllib": "^2.24.0",
"eslint": "^4.8.0",
"eslint-config-egg": "^5.1.1",
"mm": "^2.2.0",
"urllib": "^2.25.0",
"webstorm-disable-index": "^1.2.0"
},
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/cluster-config/config/config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
exports.cluster = {
listen: {
port: 8000,
}
}
},
};
8 changes: 8 additions & 0 deletions test/fixtures/egg-app/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

exports.keys = '123456';

exports.logger = {
level: 'WARN',
consoleLevel: 'WARN',
};
3 changes: 3 additions & 0 deletions test/fixtures/egg-app/node_modules/egg/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/fixtures/egg-app/node_modules/egg/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/fixtures/egg-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "example",
"dependencies": {
"egg": "^1.0.0"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/fixtures/example/node_modules/yadan/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions test/fixtures/status/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const sleep = require('mz-modules/sleep');

module.exports = app => {
if (process.env.ERROR) {
app.logger.error(new Error(process.env.ERROR));
}

app.beforeStart(function* () {
yield sleep(process.env.WAIT_TIME);
});
};
8 changes: 8 additions & 0 deletions test/fixtures/status/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

exports.keys = '123456';

exports.logger = {
level: 'WARN',
consoleLevel: 'WARN',
};
21 changes: 21 additions & 0 deletions test/fixtures/status/node_modules/custom-framework/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test/fixtures/status/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "example",
"version": "1.0.0",
"dependencies": {
"egg": "^1.0.0"
},
"egg": {
"framework": "custom-framework"
}
}
Loading