Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lib,test,ci):drop support of Node <=6.x and use async function #37

Closed
wants to merge 13 commits into from
Closed
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
sudo: false
language: node_js
node_js:
- '6'
- '8'
- '10'
- '11'
- '12'
os:
- linux
env:
- EGG_VERSION=1
- EGG_VERSION=2
Expand Down
40 changes: 20 additions & 20 deletions lib/cmd/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class StartCommand extends Command {
return 'Start server at prod mode';
}

* run(context) {
async run(context) {
const { argv, env, cwd, execArgv } = context;

const HOME = homedir();
Expand All @@ -91,12 +91,12 @@ class StartCommand extends Command {

const isDaemon = argv.daemon;

argv.framework = yield this.getFrameworkPath({
argv.framework = await this.getFrameworkPath({
framework: argv.framework,
baseDir,
});

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

const pkgInfo = require(path.join(baseDir, 'package.json'));
argv.title = argv.title || `egg-server-${pkgInfo.name}`;
Expand All @@ -121,7 +121,7 @@ class StartCommand extends Command {
// for alinode
env.ENABLE_NODE_LOG = 'YES';
env.NODE_LOG_DIR = env.NODE_LOG_DIR || path.join(logDir, 'alinode');
yield mkdirp(env.NODE_LOG_DIR);
await mkdirp(env.NODE_LOG_DIR);

// cli argv -> process.env.EGG_SERVER_ENV -> `undefined` then egg will use `prod`
if (argv.env) {
Expand Down Expand Up @@ -150,7 +150,7 @@ class StartCommand extends Command {
// whether run in the background.
if (isDaemon) {
this.logger.info(`Save log file to ${logDir}`);
const [ stdout, stderr ] = yield [ getRotatelog(argv.stdout), getRotatelog(argv.stderr) ];
const [ stdout, stderr ] = await Promise.all([ getRotatelog(argv.stdout), getRotatelog(argv.stderr) ]);
options.stdio = [ 'ignore', stdout, stderr, 'ipc' ];
options.detached = true;

Expand All @@ -169,7 +169,7 @@ class StartCommand extends Command {
});

// check start status
yield this.checkStatus(argv);
await this.checkStatus(argv);
} else {
options.stdio = [ 'inherit', 'inherit', 'inherit', 'ipc' ];
debug('Run spawn `%s %s`', command, eggArgs.join(' '));
Expand All @@ -195,11 +195,11 @@ class StartCommand extends Command {
}
}

* getFrameworkPath(params) {
return utils.getFrameworkPath(params);
async getFrameworkPath(params) {
return await utils.getFrameworkPath(params);
}

* getFrameworkName(framework) {
async getFrameworkName(framework) {
const pkgPath = path.join(framework, 'package.json');
let name = 'egg';
try {
Expand All @@ -209,17 +209,17 @@ class StartCommand extends Command {
} catch (_) {
/* istanbul next */
}
return name;
return await name;
}

* checkStatus({ stderr, timeout, 'ignore-stderr': ignoreStdErr }) {
async checkStatus({ stderr, timeout, 'ignore-stderr': ignoreStdErr }) {
let count = 0;
let hasError = false;
let isSuccess = true;
timeout = timeout / 1000;
while (!this.isReady) {
try {
const stat = yield fs.stat(stderr);
const stat = await fs.stat(stderr);
if (stat && stat.size > 0) {
hasError = true;
break;
Expand All @@ -234,15 +234,15 @@ class StartCommand extends Command {
break;
}

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

if (hasError) {
try {
const args = [ '-n', '100', stderr ];
this.logger.error('tail %s', args.join(' '));
const [ stdout ] = yield execFile('tail', args);
const [ stdout ] = await execFile('tail', args);
this.logger.error('Got error when startup: ');
this.logger.error(stdout);
} catch (err) {
Expand All @@ -256,23 +256,23 @@ class StartCommand extends Command {

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

function* getRotatelog(logfile) {
yield mkdirp(path.dirname(logfile));
async function getRotatelog(logfile) {
await mkdirp(path.dirname(logfile));

if (yield fs.exists(logfile)) {
if (await fs.exists(logfile)) {
// format style: .20150602.193100
const timestamp = moment().format('.YYYYMMDD.HHmmss');
// Note: rename last log to next start time, not when last log file created
yield fs.rename(logfile, logfile + timestamp);
await fs.rename(logfile, logfile + timestamp);
}

return yield fs.open(logfile, 'a');
return await fs.open(logfile, 'a');
}

function stringify(obj, ignore) {
Expand Down
8 changes: 4 additions & 4 deletions lib/cmd/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class StopCommand extends Command {
return 'Stop server';
}

* run(context) {
async run(context) {
const { argv } = context;

this.logger.info(`stopping egg application ${argv.title ? `with --title=${argv.title}` : ''}`);

// node /Users/tz/Workspaces/eggjs/egg-scripts/lib/start-cluster {"title":"egg-server","workers":4,"port":7001,"baseDir":"/Users/tz/Workspaces/eggjs/test/showcase","framework":"/Users/tz/Workspaces/eggjs/test/showcase/node_modules/egg"}
let processList = yield this.helper.findNodeProcess(item => {
let processList = await this.helper.findNodeProcess(item => {
const cmd = item.cmd;
return argv.title ?
cmd.includes('start-cluster') && cmd.includes(util.format(osRelated.titleTemplate, argv.title)) :
Expand All @@ -47,15 +47,15 @@ class StopCommand extends Command {
this.logger.info('got master pid %j', pids);
this.helper.kill(pids);
// wait for 5s to confirm whether any worker process did not kill by master
yield sleep('5s');
await sleep('5s');
} else {
this.logger.warn('can\'t detect any running egg process');
}


// node --debug-port=5856 /Users/tz/Workspaces/eggjs/test/showcase/node_modules/[email protected]@egg-cluster/lib/agent_worker.js {"framework":"/Users/tz/Workspaces/eggjs/test/showcase/node_modules/egg","baseDir":"/Users/tz/Workspaces/eggjs/test/showcase","port":7001,"workers":2,"plugins":null,"https":false,"key":"","cert":"","title":"egg-server","clusterPort":52406}
// node /Users/tz/Workspaces/eggjs/test/showcase/node_modules/[email protected]@egg-cluster/lib/app_worker.js {"framework":"/Users/tz/Workspaces/eggjs/test/showcase/node_modules/egg","baseDir":"/Users/tz/Workspaces/eggjs/test/showcase","port":7001,"workers":2,"plugins":null,"https":false,"key":"","cert":"","title":"egg-server","clusterPort":52406}
processList = yield this.helper.findNodeProcess(item => {
processList = await this.helper.findNodeProcess(item => {
const cmd = item.cmd;
return argv.title ?
(cmd.includes(osRelated.appWorkerPath) || cmd.includes(osRelated.agentWorkerPath)) && cmd.includes(util.format(osRelated.titleTemplate, argv.title)) :
Expand Down
4 changes: 2 additions & 2 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const runScript = require('runscript');
const isWin = process.platform === 'win32';
const REGEX = isWin ? /^(.*)\s+(\d+)\s*$/ : /^\s*(\d+)\s+(.*)/;

exports.findNodeProcess = function* (filterFn) {
exports.findNodeProcess = async function(filterFn) {
const command = isWin ?
'wmic Path win32_process Where "Name = \'node.exe\'" Get CommandLine,ProcessId' :
// command, cmd are alias of args, not POSIX standard, so we use args
'ps -eo "pid,args"';
const stdio = yield runScript(command, { stdio: 'pipe' });
const stdio = await runScript(command, { stdio: 'pipe' });
const processList = stdio.stdout.toString().split('\n')
.reduce((arr, line) => {
if (!!line && !line.includes('/bin/sh') && line.includes('node')) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/cluster-config/app/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

module.exports = app => {
app.get('/', function* () {
app.get('/', async function() {
this.body = `hi, ${app.config.framework || 'egg'}`;
});
};
2 changes: 1 addition & 1 deletion test/fixtures/custom-node-dir/app/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

module.exports = app => {
app.get('/', function* () {
app.get('/', async function() {
this.body = `hi, ${process.env.PATH}`;
});
};
6 changes: 3 additions & 3 deletions test/fixtures/example/app/router.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

module.exports = app => {
app.get('/', function* () {
app.get('/', async function() {
this.body = `hi, ${app.config.framework || 'egg'}`;
});

app.get('/env', function* () {
app.get('/env', async function() {
this.body = app.config.env + ', ' + app.config.pre;
});

app.get('/path', function* () {
app.get('/path', async function() {
this.body = process.env.PATH;
});
};
13 changes: 6 additions & 7 deletions test/fixtures/ipc-bin/start.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';

const co = require('co');

const BaseStartCommand = require('../../../lib/cmd/start');

class StartCommand extends BaseStartCommand {
* run(context) {
yield super.run(context);
async run(context) {
await super.run(context);
const child = this.child;
child.on('message', msg => {
if (msg && msg.action === 'egg-ready') {
Expand All @@ -18,8 +16,9 @@ class StartCommand extends BaseStartCommand {

const start = new StartCommand();

co(function* () {
yield start.run({

(async function() {
await start.run({
argv: {
framework: 'custom-framework',
_: [ process.env.BASE_DIR ],
Expand All @@ -32,5 +31,5 @@ co(function* () {
PATH: process.env.PATH,
},
});
});
})();

4 changes: 2 additions & 2 deletions test/fixtures/status/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = app => {
app.logger.error(new Error(process.env.ERROR));
}

app.beforeStart(function* () {
yield sleep(process.env.WAIT_TIME);
app.beforeStart(async function() {
await sleep(process.env.WAIT_TIME);
});
};
6 changes: 3 additions & 3 deletions test/fixtures/subdir-as-basedir/base-dir/app/router.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

module.exports = app => {
app.get('/', function* () {
app.get('/', async function() {
this.body = `hi, ${app.config.framework || 'egg'}`;
});

app.get('/env', function* () {
app.get('/env', async function() {
this.body = app.config.env + ', ' + app.config.pre;
});

app.get('/path', function* () {
app.get('/path', async function() {
this.body = process.env.PATH;
});
};
Loading